Doing Consistent Jumps in Unity

Posted on

in

,

So in Demonfall I wanted the guy to fall, hit the platform, jump back up to the top and then fall again. I wanted the same height jump each time. This doesn’t work by default when you turn the global gravity on. The guy’s first jump would be very short (because he fell from the top which means more gravity force was applied) and then 2nd jump would be very high (fell a much shorter distance). To fix this simply set the velocity of the guy’s rigidbody to 0 first:

Rigidbody2D guyBody = GetComponent<Rigidbody2D> ();
Vector2 v2 = guyBody.velocity;
v2.y = 0;
guyBody.velocity = v2;
//now apply force
guyBody.AddForce(new Vector2(0, 600));

To other thing I found was when I made walls and the guy touched them, there was friction and the guy wouldn’t jump as high. To fix this, go to Assets / Create / Physics2D Material. Then set it’s friction to 0. Now drag this material into the material slot of the wall’s collider. Now the walls have no friction and the guy jumps to his full potential.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

About me

Mark Wong is a front end developer with 10+ years experience. Most of his knowledge of HTML5, CSS and Js is self taught.

Calendar

April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930