Help! GetButton works but GetButtonDown doesn't?

Hi guys. I’ve been trying to get a simple jump method working for awhile now, and for some reason it’s only half working? When I used GetKey, the jump method worked, just endlessly adding force as expected. When I switched to GetKeyDown, the jump method no longer added any force. I used Debug.Log on the Jump method and it returns a value, yet the Jump velocity is never applied? Here’s my code so far:
public class MovementScript : MonoBehaviour
{
public Rigidbody2D rb;
public int moveSpeed = 10;
public float jumpHeight = 12.0f;
void Awake()
{
{
rb = GetComponent();
}
}
private void Update()
{
if (Input.GetKey(KeyCode.Space))
{
Debug.Log(“Jumped!”);
rb.velocity = new Vector2(0, jumpHeight);
}
}
void FixedUpdate()
{

if (Input.GetKey(KeyCode.A))
{
rb.velocity = new Vector2(-moveSpeed, 0);
}

else if (Input.GetKey(KeyCode.D))
{
rb.velocity = new Vector2(moveSpeed, 0);
}
else
{
rb.velocity = new Vector2(0, 0);
}

}
}

Here, there’s a few things I tried. I tried wrapping the if statements into methods like Move() and Jump(), and calling those methods in Update() because I read that FixedUpdate() didn’t work very well with GetKeyDown(). I’m not sure if this is weird code, so if I’m writing this terribly please let me know. Like I mentioned at the beginning, earlier I had a GetKey() jump method that worked, and I went to switch to GetKeyDown(), and for some reason switching to GetKeyDown made the jump method no longer work. I Debug.Logged, and it still returns a value, just for some reason it stopped giving the rigidbody velocity. I’m at an utter loss, I’ve read for hours and this is my last stop. Thanks for any advice in advance guys.

Also sorry about the code formatting weird, not really sure how to change it to where it’s not so ugly to look at.

Hey @ToastedShrimp ,

Your code ends up executing the last “else” statement when you don’t press any keys, that’s why your object doesn’t move:

if (Input.GetKey(KeyCode.A))
{
    // This code is executed only if you press and hold the 'A' key
    rb.velocity = new Vector2(-moveSpeed, 0);
}
else if (Input.GetKey(KeyCode.D))
{
    // This code is executed only if the 'D' key is down and the 'A' key is up
    rb.velocity = new Vector2(moveSpeed, 0);
}
else
{
    // This code is executed when we don't hold neither 'A' nor 'D'
    rb.velocity = new Vector2(0, 0);
}

You are also directly modifying the velocity, so that when you don’t press any keys, the velocity is always set to 0. What you could do instead, is changing your code to the following:

if (Input.GetKey(KeyCode.A))
{
    rb.velocity += new Vector2(-moveSpeed, 0);
}
else if (Input.GetKey(KeyCode.D))
{
    rb.velocity += new Vector2(moveSpeed, 0);
}

The above code, is going to add the velocity to object on every physics update. If you were to use Input.GetKeyDown instead, it would add the velocity only once on every key press.

There are different methods to control your rigidbodies, sometimes it might be easier to use AddForce method instead of directly modifying the velocity.

If you want to learn more how to create 2D games, you can go to the Unity Learn and search for beginner tutorials and projects.

To make your code more readable for other users you can click on the “Insert Code” or “Inline Code” buttons and paste your code there.

2 Likes

This has helped immensely. Thank you so much.

1 Like