< Back to Game Dev, Unity

Doing Consistent Jumps in Unity

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.

 

Be Sociable, Share!

Leave a Reply

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