From a7c2f9c270b21d19f077577aebab197dc44cb61a Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sat, 18 Apr 2020 12:06:52 -0500 Subject: [PATCH] clean up grounding logic --- Assets/Scripts/PlayerController.cs | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index 354fdcc..a069872 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -4,7 +4,6 @@ using UnityEngine; [RequireComponent(typeof(MoveController))] public class PlayerController : MonoBehaviour { - public float jumpHeight = 4f; public float timeToJumpApex = 0.4f; public float moveSpeed = 6f; @@ -25,29 +24,32 @@ public class PlayerController : MonoBehaviour { } void Update() { - if (moveController.collisions.below || moveController.collisions.above) { + Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); + float targetX = input.x * moveSpeed; + + if (moveController.collisions.above) { velocity.y = 0; } + if (moveController.collisions.left || moveController.collisions.right) { velocity.x = 0; } - Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); - if (Input.GetKeyDown(KeyCode.Space) && moveController.collisions.below) { - velocity.y = jumpVelocity; - } - - float targetX = input.x * moveSpeed; if (moveController.isGrounded) { + if (Input.GetButton("Jump")) { + velocity.y = jumpVelocity; + } else { + velocity.y = 0; + } velocity.x = Mathf.SmoothDamp(velocity.x, targetX, ref velocityXSmoothing, timeToMaxRunSpeed); } else { velocity.x = Mathf.SmoothDamp(velocity.x, targetX, ref velocityXSmoothing, timeToMaxAirmoveSpeed); + velocity.y += gravity * Time.deltaTime; + if (velocity.y < maxFallSpeed) { + velocity.y = maxFallSpeed; + } } - velocity.y += gravity * Time.deltaTime; - if (velocity.y < maxFallSpeed) { - velocity.y = maxFallSpeed; - } moveController.Move(velocity * Time.deltaTime); }