From af44bd562976bdf451de9dfce9a90b799ecbcfaf Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sat, 18 Apr 2020 20:34:50 -0500 Subject: [PATCH] fix ground sticking thing --- Assets/Scenes/SampleScene.unity | 2 +- Assets/Scripts/MoveController.cs | 5 ++++- Assets/Scripts/MoveControllerDebugDisplay.cs | 22 ++++++++++---------- Assets/Scripts/PlayerController.cs | 20 +++++++++++++----- 4 files changed, 31 insertions(+), 18 deletions(-) diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 2096b36..0a90e3c 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -498,7 +498,7 @@ PrefabInstance: - target: {fileID: 4296878337294020286, guid: 19980a8ce8df64b4cbc7cf51fcc077e2, type: 3} propertyPath: m_Name - value: Left Status Display + value: Player Status Display objectReference: {fileID: 0} - target: {fileID: 6864253409301352422, guid: 19980a8ce8df64b4cbc7cf51fcc077e2, type: 3} diff --git a/Assets/Scripts/MoveController.cs b/Assets/Scripts/MoveController.cs index cadb5d6..5377efc 100644 --- a/Assets/Scripts/MoveController.cs +++ b/Assets/Scripts/MoveController.cs @@ -38,6 +38,7 @@ public class MoveController : MonoBehaviour if (velocity.y != 0) { VerticalCollisions(ref velocity); } + Debug.LogFormat("Moving x: {0} y: {1}", velocity.x, velocity.y); transform.Translate(velocity); } @@ -52,9 +53,11 @@ public class MoveController : MonoBehaviour Debug.DrawRay(rayOrigin, Vector2.up * directionY * rayLength, Color.red); RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.up * directionY, rayLength, collisionMask); if (hit) { + Debug.LogFormat("Vertical Hit with distance: {0}", hit.distance); if (directionY > 0) { collisions.above = true; } else { + Debug.LogFormat("Hit is below: we are now grounded"); collisions.below = true; } velocity.y = (hit.distance - skinWidth) * directionY; @@ -87,7 +90,7 @@ public class MoveController : MonoBehaviour void UpdateRaycastOrigins() { Bounds bounds = collider.bounds; - bounds.Expand(skinWidth * -2); + bounds.Expand(skinWidth * -2f); raycastOrigins.bottomLeft = new Vector2(bounds.min.x, bounds.min.y); raycastOrigins.bottomRight = new Vector2(bounds.max.x, bounds.min.y); diff --git a/Assets/Scripts/MoveControllerDebugDisplay.cs b/Assets/Scripts/MoveControllerDebugDisplay.cs index 852cf83..b9dc650 100644 --- a/Assets/Scripts/MoveControllerDebugDisplay.cs +++ b/Assets/Scripts/MoveControllerDebugDisplay.cs @@ -22,18 +22,18 @@ public class MoveControllerDebugDisplay : MonoBehaviour { // Update is called once per frame void Update() { if (moveController) { - if (moveController.collisions.above) { - aboveDisplay.text = "True"; - } else { - aboveDisplay.text = "False"; - } - - if (moveController.collisions.below) { - belowDisplay.text = "True"; - } else { - belowDisplay.text = "False"; - } + showBool(aboveDisplay, moveController.collisions.above); + showBool(belowDisplay, moveController.collisions.below); + showBool(leftDisplay, moveController.collisions.left); + showBool(rightDisplay, moveController.collisions.right); + } + } + void showBool(Text label, bool value) { + if (value) { + label.text = "True"; + } else { + label.text = "False"; } } } diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index a069872..8dcc0e9 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -16,6 +16,7 @@ public class PlayerController : MonoBehaviour { private Vector3 velocity; private MoveController moveController; private float velocityXSmoothing; + private int frameCount = 0; void Start() { moveController = GetComponent(); @@ -24,22 +25,32 @@ public class PlayerController : MonoBehaviour { } void Update() { + frameCount++; + Debug.LogFormat("Player Update Start {0} ------------------------", frameCount); Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); + Debug.LogFormat("Position x: {0} y: {1}", transform.position.x, transform.position.y); + Debug.LogFormat("Input x: {0} y: {1}", input.x, input.y); float targetX = input.x * moveSpeed; + Debug.LogFormat("Target dx: {0}", targetX); + Debug.LogFormat("Above: {0}", moveController.collisions.above); if (moveController.collisions.above) { velocity.y = 0; } + Debug.LogFormat("Left: {0}", moveController.collisions.left); + Debug.LogFormat("Right: {0}", moveController.collisions.right); if (moveController.collisions.left || moveController.collisions.right) { velocity.x = 0; } + Debug.LogFormat("Grounded: {0}", moveController.isGrounded); if (moveController.isGrounded) { - if (Input.GetButton("Jump")) { + if (Input.GetButtonDown("Jump")) { + Debug.Log("JUMP"); velocity.y = jumpVelocity; } else { - velocity.y = 0; + velocity.y = gravity * Time.deltaTime; } velocity.x = Mathf.SmoothDamp(velocity.x, targetX, ref velocityXSmoothing, timeToMaxRunSpeed); } else { @@ -50,10 +61,9 @@ public class PlayerController : MonoBehaviour { } } + Debug.LogFormat("Velocity x: {0} y: {1}", velocity.x, velocity.y); moveController.Move(velocity * Time.deltaTime); - } - - void FixedUpdate() { + Debug.LogFormat("Grounded: {0}", moveController.isGrounded); } void OnDestroy() {