Simple GetButton Movement

Hi guys,

I’m having trouble understanding how to move the player using GetButton. I’m used to doing it with GetKey but I wanted to learn this way this time around. I looked into the Input Axes and see how Vertical has a positive and negative button (w and s respectively). I’m having difficulty writing code with GetButton to make my character move forward without adding an if statement with GetKey which is wrong and redundant.

Additionally, I don’t know how using GetButton(“Vertical”) will know the difference of w or s key without me stating so with GetKey.

I’m sure this is all very basic to you guys but I can’t figure it out and am not having much luck understanding the docs and google searches I’ve encountered.

Any help would be much appreciated

For clarity I understand I could do something like:

  • if(Input.GetButton(“Vertical”))
  • {
  • //move player here
  • }

What I don’t get really is how to discern whether it is the w or s (backwards) key being pressed. Also is there a better way of setting up movement than the simple if statement way I presented?

Input.GetButton(“Vertical”) returns a float, this float is > 0 for up and < 0 for down.

You can take this and multiply it by your characters speed and apply it to the x.velocity to get your character to move.

To set up movement you can do something like:

void Update(){
move = Input.GetDirection("Vertical");
}

void FixedUpdate(){
myRB.velocity = new Vector2( myRB.velocity.x, (move * speed));
}

Where myRB is the GameObjects RigidBody.

Thank you for the reply. I will give it a whirl.

Thank you the pseudo code example always helps me greatly. Much appreciated :slight_smile: