# How can I constrain the player's movement to horizontal or vertical?

This is one of my first coding projects, so apologies for beginner questions....

My player controller script uses the below code, but I want to limit movement to horizontal and vertical only. No diagonal movement.

Here's how it looks:

public class PlayerController : MonoBehaviour
{
//I’m moving the rigid body to move the player
public Rigidbody2D theRB;

//lets me change player's move speed in unity editor
public float moveSpeed;

// Update is called once per frame
void Update()
{
theRB.velocity = new Vector2( Input.GetAxisRaw("Horizontal")
, Input.GetAxisRaw("Vertical")) * moveSpeed;


I've tried the below code to only accept one command at a time by using an if statement, however, with this new code, the player doesn't stop walking once the key is no longer pressed.

Just wondering if someone could point me in the direction of what I'm doing wrong?

// Update is called once per frame
void Update()
{
if (Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1)
{
theRB.velocity = new Vector2(Input.GetAxisRaw("Horizontal"), 0) * moveSpeed;
}

if (Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Vertical") == -1)
{
theRB.velocity = new Vector2(0, Input.GetAxisRaw("Vertical")) * moveSpeed;
}


thanks!

• You may consider adjusting your tab settings in your IDE to only use spaces. – Vaillancourt Jul 31 '19 at 16:26
• @AlexandreVaillancourt I’m using Microsoft Visual Studio on Mac with default settings, so I’ll have a hunt for how to do this. Is there an advantage to using spaces over tabs? – JJOS20XX Jul 31 '19 at 22:49
• "Is there an advantage to using spaces over tabs?" Yes: you get the same spacing with any program you use. But that's not really an issue: your code had inconsistencies: it had mixed usage of spaces and tabs and that's going to cause you trouble when you open a file that has not the same tab settings. – Vaillancourt Aug 1 '19 at 0:49
• Ahh gotcha. Thanks for the tip! – JJOS20XX Aug 1 '19 at 1:11

Your second code doesn't work, because velocity doesn't decrease, when you don't hold anything. Change it to an if...else and set the velocity to zero, when nothing is held down:

    if (Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1)
{
theRB.velocity = new Vector2(Input.GetAxisRaw("Horizontal"), 0) * moveSpeed;
}
else if (Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Vertical") == -1)
{
theRB.velocity = new Vector2(0, Input.GetAxisRaw("Vertical")) * moveSpeed
}
else
{
theRB.velocity = Vector2.zero;
}

• Thanks, this fixed it! I ended up making the else statement a third ‘if’ statement as the ‘else if’ was giving me weird behaviour if I pressed a direction while already pressing another direction. But it works! Thanks! – JJOS20XX Jul 31 '19 at 22:46
• @JJOS20XX if it solved your problem, please mark it as correct – Bálint Aug 1 '19 at 12:26

I've tested the code your shared and its working just fine. But I think you'v attached the physics material to one of the object it might the player or might be the ground and that physics material has 0 fraction.

I am sorry for totally guessing because you have not mention anything about physic material and nothing else is coming to my mind right now. hehe...

Plz let me know if i am Right....

Cheers!

• Thank you for taking the time to test the code! I don’t have any friction set that I can see. I added a third statement to set 0 movement when no key is pressed and this fixed my issue. Cheers! – JJOS20XX Jul 31 '19 at 22:47