private Vector3 ApplyGravityAndJumping( Vector3 velocity )
{
if (!inputJump || !canControl)
{
jumping.holdingJumpButton = false;
jumping.lastButtonDownTime = -100f;
}
if (inputJump && jumping.lastButtonDownTime < 0 && canControl)
jumping.lastButtonDownTime = Time.time;
if (grounded)
velocity.y = Mathf.Min(0, velocity.y) - gravity * Time.deltaTime;
else
{
velocity.y = movVelocity.y - gravity * Time.deltaTime;
// When jumping up we don't apply gravity for some time when the user is holding the jump button.
// This gives more control over jump height by pressing the button longer.
if (jumping.jumping && jumping.holdingJumpButton)
{
// Calculate the duration that the extra jump force should have effect.
// If we're still less than that duration after the jumping time, apply the force.
if (Time.time < jumping.lastStartTime + jumping.extraHeight / CalculateJumpVerticalSpeed(jumping.baseHeight))
{
// Negate the gravity we just applied, except we push in jumpDir rather than jump upwards.
velocity += jumping.jumpDir * gravity * Time.deltaTime;
}
}
// Make sure we don't fall any faster than maxFallSpeed. This gives our character a terminal velocity.
velocity.y = Mathf.Max (velocity.y, -maxFallSpeed);
}
if (grounded)
{
// Jump only if the jump button was pressed down in the last 0.2 seconds.
// We use this check instead of checking if it's pressed down right now
// because players will often try to jump in the exact moment when hitting the ground after a jump
// and if they hit the button a fraction of a second too soon and no new jump happens as a consequence,
// it's confusing and it feels like the game is buggy.
if (jumping.enabled && canControl && (Time.time - jumping.lastButtonDownTime < 0.2))
{
grounded = false;
jumping.jumping = true;
jumping.lastStartTime = Time.time;
jumping.lastButtonDownTime = -100;
jumping.holdingJumpButton = true;
// Calculate the jumping direction
if (TooSteep())
jumping.jumpDir = Vector3.Slerp(Vector3.up, groundNormal, jumping.steepPerpAmount);
else
jumping.jumpDir = Vector3.Slerp(Vector3.up, groundNormal, jumping.perpAmount);
// Apply the jumping force to the velocity. Cancel any vertical velocity first.
velocity.y = 0;
velocity += jumping.jumpDir * CalculateJumpVerticalSpeed (jumping.baseHeight);
SendMessage("OnJump", SendMessageOptions.DontRequireReceiver);
}
else
{
jumping.holdingJumpButton = false;
}
}
return velocity;
}