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))]
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 CoyoteTimePingBias;
/// <summary>How often should the player's movement be updated? Used by Local and Remote -Player classes.</summary>
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>
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(