//calculates the velocity increase needed to reach the target height in the given time float ContinuousVelocity() { float heightNeeded = maxJumpHeight - minJumpHeight; float gravityResistance = timeToMaxJump * CentralGravity.GetGravityAcceleration() / 2; return(heightNeeded + gravityResistance); }
//applies the jump force to the player, and adapts if the player's gravity is inverted void JumpForce() { if (!jumpNextFrame) { return; } jumpNextFrame = false; ++jumpCount; JumpSound(); float magnitude = Mathf.Sqrt(2 * CentralGravity.GetGravityAcceleration() * minJumpHeight); if (gravity.reversed) { body.AddForce(transform.up * -magnitude * body.mass, ForceMode2D.Impulse); } else { body.AddForce(transform.up * magnitude * body.mass, ForceMode2D.Impulse); } }
//applies jump force to the player void NewJumpForce() { float magnitude = 0; if (jumpNextFrame) { magnitude = Mathf.Sqrt(2 * CentralGravity.GetGravityAcceleration() * minJumpHeight); jumpNextFrame = false; ++jumpCount; } else if (stillJumping && timer < timeToMaxJump) { float dif = maxJumpHeight - minJumpHeight; if (dif <= 0) { return; } magnitude = ContinuousVelocity() * Time.fixedDeltaTime; timer += Time.fixedDeltaTime; } ApplyForce(magnitude); }