- Home /
Fixed height jumping? 2D C#
Hello, I was wondering if anyone new how to keep all the jumps in a 2d game the same height. My character is never grounded and is always in the air and I want to be able to jump as many times as I want, but I need the height of the jumps to all be the same. I am also using the rigid body to move it as I need collisions. Thanks and I hope I make sense
You could simply set the y velocity of your rigidbody to some constant, that would ensure the same height jump. Or set the y component of velocity to 0 and then use AddForce with some constant. If you want to do it mathematically it is a little more complicated but you only have to solve a couple of equations, google 'SUVAT equations'.
Okay, well here is my code, how would i do that would i do that? Sorry for the trouble im a bit of a noob (I just have the grounded part, but im not using it)
using UnityEngine; using System.Collections;
public class Jumping : $$anonymous$$onoBehaviour {
// Player Jump
public int jumpHeight;
public bool isGrounded;
// Update is called once per frame
void OnCollisionStay (Collision collisionInfo)
{
isGrounded = true;
}
void OnCollisionExit (Collision collisionInfo)
{
isGrounded = false;
}
void CheckIfGrounded (){
}
void Update () {
// Left Control
if (Input.Get$$anonymous$$ey("up")){
rigidbody.AddForce(new Vector3(0, jumpHeight,0));
}
}
}
perhaps the following:
public int jumpHeight;
float jumpSpeed;
Vector3 velocity;
void Start(){
/*
s = jumpHeight
u = u
v = 0
a = Physics.gravity.y
t = ~
Equation without t is:
u^2 = v^2 - 2as
hence as v = 0 (we want speed to be 0 so that its at the 'top' of the jump)
u = sqrt(-2as)
*/
jumpSpeed = $$anonymous$$athf.Sqrt(-2*Physics.gravity.y*jumpHeight) + 0.1f;
}
void Update(){
if(Input.Get$$anonymous$$eyDown("up")){
velocity = rigidbody.velocity;
velocity.y = jumpSpeed;
rigidbody.velocity = velocity;
}
}
The 0.1 I added in start is just some buffer value as unity physics takes into account a few more things than SUVAT equations do, so its probably not quite right due to drag or similar. Either way it should jump the same height each time, so you can mess around with the values to get the correct 'same' height.
O$$anonymous$$G O$$anonymous$$G O$$anonymous$$G, I literally dont know how to thank you, the code is amazing, thank you soooo much. You literally dont know how happy i am right now, ive been trying to do this for ages. Thank you so much, i will definetely give you credit when i finally release my game. Thank you
Answer by DESIMA · Jan 31, 2015 at 04:53 PM
If you just wanted to controll height you can do that with Damping and mass for the rigidbody, and when you add and rigidbody.AddForce(); you can decide on a certain number for velocity
Your answer
Follow this Question
Related Questions
How can I limit jump height in unity? 3 Answers
Player Inconsistent Jumps 0 Answers
Adding a jump feature help? 2 Answers
Jumping in only the direction of the last key pressed 0 Answers
Hey Guys I have a problem with a single jump script. 1 Answer