How do I make my player jump at a fixed height, and only once until he touches the ground again?
I have been trying all day and all yesterday trying to make my character jump at a fixed height for this 2D platformer I am making, I used this code (it's in C#)
if (Input.GetKeyDown(KeyCode.Space)){ transform.position += new Vector3(0, jumpHeight * Time.deltaTime, 0);
and he will jump a little bit at random heights even though I'm using a public float to change his jump height. I have been browsing the web trying and using the code in different tutorial videos and other people's posts but it doesn't seem to work. Also I've been trying to figure out how to make him jump once.
I hate to ask for help, but I'm still very new to all of this and I'm just not getting anywhere. Any help or suggestions would be greatly appreciated.
Answer by van der Merwe · Nov 02, 2015 at 09:10 PM
Well first off you would want to add a rigid body to your character ( this helps with physics such as gravity and so on)
Then the code would look something like this
public float moveSpeed;
public float jumpHeight;
public float moveVelocity;
bool grounded;
//This is for 2D not 3D, I am just taking a wild guess this is a 2D game
private RigidBody2D rigid;
void Start()
{
//Get the rigidbody of character
rigid = GetComponent<RigidBody2D>();
//Set grounded to true or false (Depending if your character starts in the
//air
grounded = true;
}
void Update()
{
moveVelocity = 0;
// If the character moves right
if (Input.GetKeyDown(KeyCode.D))
{
moveVelocity = moveSpeed;
}
// If the character moves left;
if (Input.GetKeyDown(KeyCode.A))
{
moveVelocity = -moveSpeed;
}
//If you press spacebar and your are on the ground then you can jump
if (Input.GetKeyDown(KeyCode.Space) && grounded == true)
{
//I am not to sure about the rigid.velocity code play around with it
//I think in 2D you can set it to a Vector2 instead of Vector3 otherwise just
//change it
rigid.velocity = new Vector2 (moveVelocity, jumpHeight);
//After your character jumps grounded has to change to false since the
//character is not on the ground anymore
grounded = false;
}
// If your character is not falling nor jumping then change grounded true
//Since he has landed.
if (rigid.velocity.y == 0)
{
grounded = true;
}
}
Since I am not near unity I am not sure of the spelling of all the code I written but it should be more or less correct. I added comments in the code to explain each part.
Also place the your movement script onto your character (Otherwise RigidBody2D won't know what your trying to code)
Hope this help
thank you so much ive been looking for an answer to my problem for a long time!!
Your answer
![](https://koobas.hobune.stream/wayback/20220612043555im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make it so I can't use the jetpack/jump while slider value is 0? 0 Answers
Pressing Vs Jumping 1 Answer
Still constant jumping even with raycasting. 0 Answers
Anyone have any ideas on how I could improve my character jump to make it more realistic? 1 Answer
How can I make my Player jump when I press a UI button? 0 Answers