Fix player jumping, coyote time, add coyote ping bias

This commit is contained in:
Jens Pitkänen 2020-08-08 02:57:12 +03:00
parent 6e878cd488
commit c5a3810042

View File

@ -8,8 +8,9 @@ namespace NeonTea.Quakeball.Players {
[RequireComponent(typeof(CharacterController))] [RequireComponent(typeof(CharacterController))]
public class Player : MonoBehaviour { public class Player : MonoBehaviour {
/// <summary>How long after running off a cliff should the player be considered "on ground"?</summary> /// <summary>The duration after running off a cliff, during which the player should still be considered grounded.</summary>
public float CoyoteTime; public float CoyoteTime;
public float CoyoteTimePingBias;
/// <summary>How often should the player's movement be updated? Used by Local and Remote -Player classes.</summary> /// <summary>How often should the player's movement be updated? Used by Local and Remote -Player classes.</summary>
public float UpdateFrequency = 1f; public float UpdateFrequency = 1f;
@ -50,6 +51,8 @@ namespace NeonTea.Quakeball.Players {
/// <summary>The timestamp of when the player was last on the ground.</summary> /// <summary>The timestamp of when the player was last on the ground.</summary>
public float GroundedTime; public float GroundedTime;
public float LatestGroundedY;
[Header("Misc. technical knobs")] [Header("Misc. technical knobs")]
public float GroundCastLength = 0.2f; public float GroundCastLength = 0.2f;
public LayerMask GroundLayer; public LayerMask GroundLayer;
@ -109,8 +112,12 @@ namespace NeonTea.Quakeball.Players {
} }
public bool Jump() { public bool Jump() {
if (IsGrounded()) { bool IsCoyoteTime = Time.time - GroundedTime <= CoyoteTime + CoyoteTimePingBias;
if (IsCoyoteTime || IsGrounded()) {
GravitationalVelocity = Vector3.up * MoveStyle.JumpVelocity; GravitationalVelocity = Vector3.up * MoveStyle.JumpVelocity;
Vector3 Pos = transform.position;
Pos.y = LatestGroundedY;
transform.position = Pos;
return true; return true;
} else { } else {
return false; return false;
@ -118,7 +125,7 @@ namespace NeonTea.Quakeball.Players {
} }
public bool IsGrounded() { 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() { private void Awake() {
@ -174,6 +181,11 @@ namespace NeonTea.Quakeball.Players {
CharacterController.Move(FinalMoveVector * Time.deltaTime); CharacterController.Move(FinalMoveVector * Time.deltaTime);
if (CharacterController.isGrounded) { if (CharacterController.isGrounded) {
GroundedTime = Time.time; 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) { if (ShowMoveVector) {
Debug.DrawLine( Debug.DrawLine(