- Home /
Jumping a specific height using Velocity & Gravity
Hello,
I am trying to get my character to jump a specific height using velocity and gravity.
Gravity is currently set to X:0,Y:-20
But, what is gravity? does the -20 mean it is applying a -20 unit velocity per second? This is all I can assume.
Anyway, to keep it simple, lets say I want to jump up 10.0 units. By this, I mean, from the character's position on the ground, to the peak of his jump, he should have moved 10.0 units.
However, I am pretty bad at the maths. So, gravity is -20 units, I need to peak a jump at 10 units. What velocity would I require?
rigidbody2D.velocity = new Vector2 (0, What goes Here?);
Of course, I am after the equation required, not just the number for 10 units. The character would need to jump 2 units, 5.5 units, 34.44 units, etc.
Always ways round not being good at maths. I take it you can get your character to jump so stack 10 cubes (or one and scale so it's 10 units in size).
Place that near your character and a camera to view side on your character. Disable any Camera setup you currently have whilst doing this and just view from the side on camera. Hmmm your 2D so you may already have a perfect camera for this.
Run the game and make your character jump. Then keep adding extra force until you get what you want. Should only take a few goes and you'll get a feel for how the velocity works as well.
EDIT: Oh your 2D so it might not be cubes, but just add something that's the height you want your character to jump so you can see it on screen.
Answer by Fonserbc · Dec 23, 2014 at 10:18 AM
From: http://en.wikipedia.org/wiki/Acceleration#Uniform_acceleration
Using the 3rd formula you can allienate v0 when v is equal to 0, when the object is on its maximum height.
v0 = sqrt(-2 * a * s)
Which in unity would be something like:
float jumpHeight = 10.0f;
rigidbody2D.velocity = new Vector2(0, Mathf.Sqrt(-2.0f * Physics2D.gravity.y * jumpHeight));
This will make the object reach quite close to the jumpHeight, but you have to understand that the physics simulation is not deterministic and will not exactly make the object reach your height, but it will be pretty aproximate.
That is always that you have no drag.
Thanks, this is what I was after. It isn't exact as you say, but it gives me a variable jump height that fairly accurately measures my touch position.
Cheers! :)
Your answer
Follow this Question
Related Questions
Anti-Gravity isn't working 1 Answer
Trying to limit air velocity 2 Answers
Gravity and jumping is wierd 1 Answer
Jumping script WONT WORK!!!!!!!! for 2D 1 Answer
Why when i press space button nothing was happened 0 Answers