I’m trying to write a method that calculates initial jump speed using the height of the jump and the gravity acceleration, for the purposes of a 2D raycast-based character controller script. I’ve written a method for this, but the resulting jumps only get to about 95% of their intended height. I’m not very good at physics, and I could use some help.
Start speed method:
public static float CalculateStartSpeed(float distance, float endSpeed, float acceleration)
{
return Mathf.Sqrt(endSpeed * endSpeed - 2.0f * acceleration * distance);
}
Which I wrote by using the formula: v² = u² + 2as
Which can be rewritten to: u² = v² - 2as
Which in turn can be rewritten to: u = sqrt(v² - 2as)
My speed update method looks like this:
public static float Accelerate(float currentSpeed, float acceleration, float deltaTime)
{
return currentSpeed + acceleration * deltaTime;
}
Which gets called every FixedUpdate of the CharacterController script.
However, when I use these methods with a jump height of 7, my character only manages to reach a height of about 6.6 before falling again. Does anyone have an idea why this might be? Am I overlooking something obvious?
Edit: Oh, and if you don’t know what the problem is, but you can confirm that the methods I posted are correct, please let me know, because then at least I know the problem is somewhere else.