Jump using CharacterController and the physics behind it

I use CharacterController.Move to move my character around. In my game world, 1 unit = 1 meter. This works great, for example for speed: if I use a speed of 5, the character has a speed of 5m/s.

I make my character jump using:

if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
...
moveDirection.y -= gravity * Time.deltaTime;

I found this in the docs. I assume with “gravity” they mean the acceleration (so 9.81 on earth), not the force itself.

This works fine too, but since the gravity is pulling my character down, the jumpHeight isn’t actually reached. For example, a jumpHeight of 8 result in a jump of something around 2.4 meters.

How can I change my code so jumpHeight represents the height the character will jump, even with the gravity pulling him down? Basically, with a jumpHeight of 8, I want my player to perform a jump of 8 meters.

According to physics, with an initial velocity of 8, the height would be 1.6 with a gravity of 20 (height = initialvelocity^2/(2*gravity)). If this indeed would be the height I’m seeing in-game, I could simply transform the formula, but I’m seeing a height of around 2.4 meters…

I’ve seen this formula around online for calculating the height of a jump.

float CalculateJumpVerticalSpeed () {
       // From the jump height and gravity we deduce the upwards speed
       // for the character to reach at the apex.
       return Mathf.Sqrt(2 * jumpHeight * gravity);
   }
1 Like

Yes, that’s the same formula as in my question but transformed for initialvelocity. That would work if the physics behave as they should, but since an initialvelocity of 8 results in a jump of 2.4 meters instead of 1.6 with a gravity of 20, there’s something else going on too…