- Home /
Fixed character controller jump height
Hello
I am currently working on a player controller using Unity's built-in character controller and I would like the jump height to be fixed. Right now I am simply doing "moveDirection.y = jumpHeight;" and it works but it is not really what I want. The jump height is set to 10 and that results in the player jumping ~2 meters up in the air. But how do I make it so, for example, I set it to 3, the player jumps meters into the air?
Any help is appreciated!
If you set to 3 then how much meter you want to jump?
If I set it to 3, I want to jump 3 meters. If I set it to 5.2, I want to jump 5.2 meters. Currently, if I set it to 3, I only jump ~0.4 meters.
in that case you can multiply the jumpheight by the input jump value(jumpheight * input value)
Sorry about replying to the wrong comment but it's the only way for me to actually reply. >_>
But how do I know what I should multiply it by?
Looks like you need to play with gravity scale a bit. You wrote that the player will jump 2m if the jumpheight is 10 but what is the gravity scale? change the gravity scale so that when you press 10 it will jump 10meter. After you find exact gravity scale multiply it with jumpheight. Example you reached 10m in 1 gravity scale then to reach 20 you do 20* 1;
Ah, I see. I will experiment around with this and see if I can find a working solution! Thanks for the help so far!
Answer by DavidWatts · Aug 26, 2016 at 06:13 PM
i believe the calculation you want is
Mathf.sqrt( 2 * jumpHeight * -Physics.gravity.y );
I'm a bit confused about that 2. $$anonymous$$ind explaining?
he means: $$anonymous$$athf.sqrt(2 * jumpHeight - Physics.gravity.y); i guess
I figured so, eventually. But I can't actually get it to work with this equation. I do get some value but it is so small the player just lifts like 0.05 meters above the ground.
sorry i was having issues with answers not working on firefox so i had to get chrome. yea i meant $$anonymous$$athf.sqrt( 2 jumpHeight - Physics.gravity ) and set it to the velocity.y not AddForce
I tried doing this and it kinda worked, I guess. $$anonymous$$y problem is that I use a custom gravity parameter and if I change it, the jumping will be off. I also needed to change the 2 to 10 for it to work in the first place.
that should work with custom gravity just to be clear it is
$$anonymous$$athf.sqrt( 2 * jumpHeight * -gravity.y )