From c5a3810042fe393f1f4b240691b956d88313474c Mon Sep 17 00:00:00 2001 From: Jens Pitkanen Date: Sat, 8 Aug 2020 02:57:12 +0300 Subject: [PATCH] Fix player jumping, coyote time, add coyote ping bias --- Assets/Scripts/Players/Player.cs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/Assets/Scripts/Players/Player.cs b/Assets/Scripts/Players/Player.cs index de4f6f5..6423c5d 100644 --- a/Assets/Scripts/Players/Player.cs +++ b/Assets/Scripts/Players/Player.cs @@ -8,8 +8,9 @@ namespace NeonTea.Quakeball.Players { [RequireComponent(typeof(CharacterController))] public class Player : MonoBehaviour { - /// How long after running off a cliff should the player be considered "on ground"? + /// The duration after running off a cliff, during which the player should still be considered grounded. public float CoyoteTime; + public float CoyoteTimePingBias; /// How often should the player's movement be updated? Used by Local and Remote -Player classes. public float UpdateFrequency = 1f; @@ -50,6 +51,8 @@ namespace NeonTea.Quakeball.Players { /// The timestamp of when the player was last on the ground. public float GroundedTime; + public float LatestGroundedY; + [Header("Misc. technical knobs")] public float GroundCastLength = 0.2f; public LayerMask GroundLayer; @@ -109,8 +112,12 @@ namespace NeonTea.Quakeball.Players { } public bool Jump() { - if (IsGrounded()) { + bool IsCoyoteTime = Time.time - GroundedTime <= CoyoteTime + CoyoteTimePingBias; + if (IsCoyoteTime || IsGrounded()) { GravitationalVelocity = Vector3.up * MoveStyle.JumpVelocity; + Vector3 Pos = transform.position; + Pos.y = LatestGroundedY; + transform.position = Pos; return true; } else { return false; @@ -118,7 +125,7 @@ namespace NeonTea.Quakeball.Players { } public bool IsGrounded() { - return Time.time - GroundedTime <= CoyoteTime && Vector3.Dot(GravitationalVelocity, Vector3.down) >= 0; + return CharacterController.isGrounded && Vector3.Dot(GravitationalVelocity, Vector3.down) >= 0; } private void Awake() { @@ -174,6 +181,11 @@ namespace NeonTea.Quakeball.Players { CharacterController.Move(FinalMoveVector * Time.deltaTime); if (CharacterController.isGrounded) { GroundedTime = Time.time; + LatestGroundedY = transform.position.y; + } + if (GravitationalVelocity.y > 0.1 && Mathf.Abs(CharacterController.velocity.y) < 0.1) { + // Hit a roof while jumping + GravitationalVelocity.y = 0; } if (ShowMoveVector) { Debug.DrawLine(