- Home /
How to jump always with the same height
Hello guys, i have a problem. I'm using physics 2D, and i'm trying to make a character jump always the same height with Addforce, but in vain.
I alreaddy have tryed this:
acceleration = (rigidbody2D.velocity - lastVelocity) / Time.fixedDeltaTime;
lastVelocity = rigidbody2D.velocity;
force = rigidbody2D.mass * Mathf.Abs(acceleration.y);
rigidbody2D.AddForce(Vector2.up*force);
and have already tryed make velocity zero when clicked before addforce but nothing:
rigidbody2D.velocity = new Vector2(0,0);
Can sound easy but is tricky. Any ideas?
Answer by nesis · Feb 13, 2014 at 04:35 PM
You can set rigidbody2D.velocity's y component to some value that'll reach the height you want. You just need to calculate what that y value needs to be in order to counter-act gravity for long enough that the apex of the jump occurs at the height you want.
Luckily, Unity's CharacterController package comes with an example of how to do this in Javascript, which I've appropriated to 2D for you:
function CalculateJumpVerticalSpeed (targetJumpHeight : float)
{
// From the jump height and gravity we deduce the upwards speed
// for the character to reach at the apex.
return Mathf.Sqrt(2 * targetJumpHeight * Physics2D.gravity.y);
}
And in C#, it'd be:
public static float CalculateJumpVerticalSpeed(float targetJumpHeight)
{
// From the jump height and gravity we deduce the upwards speed
// for the character to reach at the apex.
return Mathf.Sqrt(2f * targetJumpHeight * Physics2D.gravity.y);
}
Then, to set the velocity of your Rigidbody2D, you'd use this in Javascript:
var velocity : Vector2 = rigidbody2D.velocity;
velocity.y = CalculateJumpVerticalSpeed(myJumpHeight);
rigidbody2D.velocity = velocity;
or in C#, it'd be:
Vector2 velocity = rigidbody2D.velocity;
velocity.y = CalculateJumpVerticalSpeed(myJumpHeight);
rigidbody2D.velocity = velocity;
Hi! Thanks, but that doesnt work for me, my rigidbody doesnt have always the same height, so with your solution is harder to jump when the character is bellow his start position and easialy when he is abobe his initial position.
Any ideas?
If you're changing the height of your rigidbody, then take that into account by getting the height of the collider and subtracting post of it from the jump height.
Rather than giving you a code example, I'll ask a simple question:
If you want the top of your head to reach the bottom of a pole that stands directly above you, and it is 4 units above the ground, and you are 2 units tall, how high must you jump to reach that pole with the top of your head?
The answer should be at LEAST 2 units, correct?
For the sake of simplicity, let's assume we want to reach it EXACTLY, and not overshoot the jump.
If the pole is 4 units off the ground, and the top of our head is 2 units off the ground (because we are 2 units tall), then the distance between the two positions is 2 units because 4 - 2 = 2. If the pole were 5 units off the ground, and our height were still 2 units, then we would need to jump 3 units off the ground, since the pole would be 3 units above our head.
It's a simple 1-dimensional vector problem: Subtract the height off the ground of the target height from the height off the ground of the original height to get the difference between the two, or target - original = difference
What nesis stated is that if your height is changing, simply keep track of that height in a variable within the class and find the difference between the target height you want to reach with the jump and the current height of the object jumping.