- Home /
How to get the same jump height each time
I've been trying to make a 2.5D game in Unity and I need the characters to jump similar to characters in BlazBlue. I've been trying to make it so that no matter the direction of the jump that the jump always reaches the same height:
http://gyazo.com/5974028acf46a1941ec7abca2d12938b http://gyazo.com/7de2ea2cf6a9d64650ac49a242dec9b1 http://gyazo.com/af951e967d3bfce231166f7c273b0cab
I've been using rigidbodies that have everything except their x and y positions frozen and have apply gravity checked whenever I change the y velocity of the rigidbody. The problem is that changing the jump force makes the jump heights really inconsistent and sometimes doesn't really make much change at all:
void Jump() {
if (Input.GetKeyDown(moveRight)) {
Vector3 vel = rigidbody.velocity;
vel.y = jumpHeight * jumpHeight;
rigidbody.velocity = vel;
isFalling = true;
airAction--;
}
if (Input.GetKeyDown(moveLeft)) {
Vector3 vel = rigidbody.velocity;
vel.y = jumpHeight * jumpHeight;
rigidbody.velocity = vel;
isFalling = true;
airAction--;
} else {
Vector3 vel = rigidbody.velocity;
vel.y = jumpHeight;
rigidbody.velocity = vel;
//rigidbody.AddRelativeForce (transform.up * jumpHeight, ForceMode.Force);
isFalling = true;
airAction--;
}
}
I've tried applying force already and using CharacterControllers seems to only make it worse. Is there any way I can acheive this effect?
Not for nothing, but using real rigidbody physics and expecting to get the exact same results in different scenarios isn't feasible. Not without some major foresight, case-planning, and prototyping. There are a ton of tricks and quirks involved in making a great rigidbody controller; frankly too much to expect to be taught. You've gotta learn them as you go.
You can either jump by adding a one-time impulse force (or velocity change) whose magnitude is the same each time, which is the conventional, widely-appreciated method. Or you can create your own system to govern jump force based on whatever methodology you want.
edit: having just checked out your example screenshot, I'm not convinced you should be using non-kinematic rigidbodies at all.
Answer by Jaidan · Feb 02, 2015 at 04:02 PM
Hello, i think i might have the answer, i asked this same question a couple of days ago and got a brilliant reply from a guy, here is the answer page: http://answers.unity3d.com/questions/890058/fixed-height-jumping-2d-c.html
If you have any questions then ask the guy who answered the question. :D
It didn't work perfectly, but with a few adjustments I was able to get it really close. :) Thanks!
Answer by Nemox · Feb 02, 2015 at 05:32 PM
For one, you're setting vel.y to different things. When moving it's jumpHeight^2, but when not moving it's just jumpHeight.
Also double-check that the function isn't accidentally getting called multiple times. It could accidentally be checking that ground is under the character's feet, even though the character is moving away from it.