Skip to content

Commit

Permalink
CurrentStation, Exit Vehicle, Debug and Drag
Browse files Browse the repository at this point in the history
Current station removal on player is simplified (so it works), but may not be robust.
Finding current station fixed. Before it wasn't checking parents, and the collider was on the TileMap.

Exiting a vehicle on a station has been fixed. The player wasn't able to determine if it was in a station because it's rigidbody wasn't simulated. Now the vehicle has "currentstation" and the player checks that

Debug checks added to annoying debug statements that are often used.

Drag implemented for entering/exiting stations on entities.
Still need to implement ship locking when exiting the vehicle.
  • Loading branch information
pog7776 committed Mar 10, 2023
1 parent 6f5f217 commit 259b045
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 23 deletions.
6 changes: 6 additions & 0 deletions .vsconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"version": "1.0",
"components": [
"Microsoft.VisualStudio.Workload.ManagedGame"
]
}
20 changes: 15 additions & 5 deletions Assets/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -3760,6 +3760,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 10fb806d393482e43b540cd3b9d1f5ac, type: 3}
m_Name:
m_EditorClassIdentifier:
debug: 0
_onInteract:
m_PersistentCalls:
m_Calls:
Expand Down Expand Up @@ -3816,9 +3817,9 @@ MonoBehaviour:
m_EditorClassIdentifier:
moveSpeed: 20
lookatOffset: {x: 0, y: 0, z: -90}
rb: {fileID: 0}
turnSpeed: 3
dismountAnchor: {fileID: 1031673585}
currentStation: {fileID: 0}
--- !u!60 &143589115
PolygonCollider2D:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -3916,9 +3917,11 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: d4b25a13b3b589a44aa2945dbb9af0d3, type: 3}
m_Name:
m_EditorClassIdentifier:
rb: {fileID: 0}
grounded: 1
groundedDrag: 2
flightDrag: 0
groundedAngularDrag: 1
flightAngularDrag: 0
--- !u!1 &145654670
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -4497,9 +4500,11 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: d4b25a13b3b589a44aa2945dbb9af0d3, type: 3}
m_Name:
m_EditorClassIdentifier:
rb: {fileID: 0}
grounded: 1
groundedDrag: 10
flightDrag: 0
groundedAngularDrag: 1
flightAngularDrag: 0
--- !u!1 &159061383
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -8273,6 +8278,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
inverseSquare: 1
g: 1
debug: 0
--- !u!58 &383183500
CircleCollider2D:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -16506,9 +16512,11 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: d4b25a13b3b589a44aa2945dbb9af0d3, type: 3}
m_Name:
m_EditorClassIdentifier:
rb: {fileID: 0}
grounded: 0
groundedDrag: 1
flightDrag: 0
groundedAngularDrag: 1
flightAngularDrag: 0
--- !u!114 &826203411
MonoBehaviour:
m_ObjectHideFlags: 0
Expand All @@ -16521,8 +16529,8 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 3e324d1c67a616a4e96c285c02205388, type: 3}
m_Name:
m_EditorClassIdentifier:
rb: {fileID: 0}
initialImpulse: {x: 0, y: 800, z: 0}
debug: 0
--- !u!50 &826203412
Rigidbody2D:
serializedVersion: 4
Expand Down Expand Up @@ -24149,6 +24157,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
inverseSquare: 1
g: 2.5
debug: 0
--- !u!58 &1043146800
CircleCollider2D:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -30478,6 +30487,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
inverseSquare: 1
g: 0.5
debug: 0
--- !u!58 &1340137743
CircleCollider2D:
m_ObjectHideFlags: 0
Expand Down
15 changes: 14 additions & 1 deletion Assets/Scripts/Entity/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,22 @@ public class Entity : MonoBehaviour

[SerializeField]
private float groundedDrag = 1;
[SerializeField]
private float flightDrag = 0;

[SerializeField]
private float groundedAngularDrag = 1f;
[SerializeField]
private float flightAngularDrag = 0.00f;

private PlayerController playerController;

public Rigidbody2D RigidBody { get => rigidBody; private set => rigidBody = value; }
public bool Grounded { get => grounded; set => grounded = value; }
public float GroundedDrag { get => groundedDrag; set => groundedDrag = value; }
public float FlightDrag { get => flightDrag; set => flightDrag = value; }
public float FlightAngularDrag { get => flightAngularDrag; set => flightAngularDrag = value; }
public float GroundedAngularDrag { get => groundedAngularDrag; set => groundedAngularDrag = value; }
public PlayerController PlayerController { get => playerController; set => playerController = value; }

//[SerializeField]
Expand All @@ -38,15 +48,18 @@ private void OnTriggerEnter2D(Collider2D other) {
if(other.tag == "Gravity") {
Grounded = true;
//rb.velocity = Vector3.zero;

RigidBody.drag = GroundedDrag;
RigidBody.angularDrag = GroundedAngularDrag;
}
}

private void OnTriggerExit2D(Collider2D other) {
if(other.tag == "Gravity") {
Grounded = false;
//rb.AddForce();
RigidBody.drag = 0;
RigidBody.drag = FlightDrag;
RigidBody.angularDrag = FlightAngularDrag;

if(PlayerController != null && RigidBody.velocity.magnitude <= 5) {
//Vector3 force = new Vector3(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")) * (PlayerController.moveSpeed * (PlayerController.walkSpeedMod * 10f));
Expand Down
8 changes: 4 additions & 4 deletions Assets/Scripts/Entity/GravityWell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public class GravityWell : MonoBehaviour
//private Collider2D gravityInfluence;
[SerializeField]
private bool inverseSquare = true;

public float g = 0.5f;

[SerializeField]
private bool debug = false;
private List<Entity> entities = new List<Entity>();

private void FixedUpdate() {
Expand Down Expand Up @@ -44,7 +44,7 @@ private void OnTriggerEnter2D(Collider2D other) {
}
}

Debug.Log("Entered influence of " + gameObject.name);
if(debug) Debug.Log("Entered influence of " + gameObject.name);
}

private void OnTriggerExit2D(Collider2D other) {
Expand All @@ -56,6 +56,6 @@ private void OnTriggerExit2D(Collider2D other) {
entity = entities.Find(element => element.gameObject == other.gameObject);
entities.Remove(entity);

Debug.Log("Left influence of " + gameObject.name);
if(debug) Debug.Log("Left influence of " + gameObject.name);
}
}
11 changes: 7 additions & 4 deletions Assets/Scripts/Interactable/Interactable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
using UnityEngine.Events;

public class Interactable : MonoBehaviour {
[SerializeField]
private bool debug = false;

[SerializeField]
private UnityEvent _onInteract;
public UnityEvent OnInteract
Expand Down Expand Up @@ -33,22 +36,22 @@ public UnityEvent OnHoverExit
}

public void Interact() {
Debug.Log(gameObject.name + " Interact");
if(debug) Debug.Log(gameObject.name + " Interact");
OnInteract?.Invoke();
}

public void InvokeOnEnter() {
Debug.Log(gameObject.name + " OnEnter");
if(debug) Debug.Log(gameObject.name + " OnEnter");
OnHoverEnter?.Invoke();
}

public void InvokeOnExit() {
Debug.Log(gameObject.name + " OnExit");
if(debug) Debug.Log(gameObject.name + " OnExit");
OnHoverExit?.Invoke();
}

public void InvokeOnHover() {
//Debug.Log(gameObject.name + " OnHover");
//if(debug) Debug.Log(gameObject.name + " OnHover");
OnHover?.Invoke();
}
}
15 changes: 11 additions & 4 deletions Assets/Scripts/Player/PlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public void EnterVehicle(Vehicle vehicle)
{
if(currentVehicle == null) {
currentVehicle = vehicle;
// TODO seat anchor position on the vehicle
transform.position = vehicle.transform.position;
transform.parent = vehicle.transform;
RigidBody.simulated = false;
Expand All @@ -91,8 +92,8 @@ public void ExitVehicle()
transform.position = currentVehicle.dismountAnchor.position;
transform.parent = null;
RigidBody.simulated = true;
if(currentStation) {
RigidBody.velocity = currentVehicle.RigidBody.velocity + currentStation.RigidBody.velocity;
if(currentVehicle.currentStation) {
RigidBody.velocity = currentVehicle.currentStation.RigidBody.velocity;
} else {
RigidBody.velocity = currentVehicle.RigidBody.velocity;
}
Expand All @@ -109,7 +110,10 @@ public void ExitVehicle()
private void OnTriggerEnter2D(Collider2D other) {
if(other.tag == "Gravity") {
grounded = true;
currentStation = other.gameObject.GetComponent<Station>();

if(!other.gameObject.TryGetComponent<Station>(out currentStation)) {
currentStation = other.gameObject.GetComponentInParent<Station>();
}

// if(other.gameObject.TryGetComponent<Station>(out currentStation)) {

Expand All @@ -127,7 +131,10 @@ private void OnTriggerExit2D(Collider2D other) {
// rb.AddForce(rb.velocity);
// }

if(currentStation != null && currentStation.gameObject == other.gameObject) {
// TODO Really need to figure this out
// If a station is within another station
// There was another issue i saw but i cant think of it right now
if(currentStation != null) { //&& currentStation.gameObject == other.gameObject) {
currentStation = null;
}
}
Expand Down
15 changes: 10 additions & 5 deletions Assets/Scripts/Structure/Station.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ public class Station : MonoBehaviour
private List<Entity> entities;

private Vector3 lastPosition;
private Vector3 _localVelocity;

[SerializeField]
private Vector3 initialImpulse;
[SerializeField]
private bool debug = false;

public Rigidbody2D RigidBody { get => _rigidBody; protected set => _rigidBody = value; }
public Vector3 LocalVelocity { get => _localVelocity; set => _localVelocity = value; }

// Start is called before the first frame update
void Start()
Expand All @@ -29,15 +33,16 @@ void Start()
}

RigidBody.AddForce(initialImpulse);
LocalVelocity = transform.position - lastPosition;
}

// Update is called once per frame
void Update()
{
Vector3 velocity = transform.position - lastPosition;
LocalVelocity = transform.position - lastPosition;
foreach(Entity entity in entities) {
entity.transform.Translate(velocity, transform);
//entity.transform.position += velocity;
entity.transform.Translate(LocalVelocity, transform);
//entity.transform.position += LocalVelocity;
}

lastPosition = transform.position;
Expand All @@ -51,7 +56,7 @@ private void OnTriggerEnter2D(Collider2D other) {
entity.RigidBody.velocity = entity.RigidBody.velocity - RigidBody.velocity;
}

Debug.Log("Entered influence of " + gameObject.name);
if(debug) Debug.Log("Entered influence of " + gameObject.name);
}

private void OnTriggerExit2D(Collider2D other) {
Expand All @@ -61,6 +66,6 @@ private void OnTriggerExit2D(Collider2D other) {
entity.RigidBody.velocity = entity.RigidBody.velocity + RigidBody.velocity;
}

Debug.Log("Left influence of " + gameObject.name);
if(debug) Debug.Log("Left influence of " + gameObject.name);
}
}
19 changes: 19 additions & 0 deletions Assets/Scripts/Vehicle/Vehicle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public class Vehicle : Controllable

public Transform dismountAnchor;

[HideInInspector]
public Station currentStation;

// Start is called before the first frame update
protected override void Start() {
base.Start();
Expand Down Expand Up @@ -83,4 +86,20 @@ protected override void LookAt(Vector3 position)
//rb.MoveRotation(targetRotation);
//rb.MoveRotation(Quaternion.Lerp(Quaternion.Euler(0,0,rb.rotation), targetRotation, Time.fixedDeltaTime * turnSpeed));
}

private void OnTriggerEnter2D(Collider2D other) {
if(other.tag == "Gravity") {
if(!other.gameObject.TryGetComponent<Station>(out currentStation)) {
currentStation = other.gameObject.GetComponentInParent<Station>();
}
}
}

private void OnTriggerExit2D(Collider2D other) {
if(other.tag == "Gravity") {
if(currentStation != null) {
currentStation = null;
}
}
}
}

0 comments on commit 259b045

Please sign in to comment.