Prevent diagonally movement - C# [resolved]

Hello!
I do not want the player walks diagonally.
How do I prevent this using this code?

public class Moviment : MonoBehaviour
{
    private float speedLocomotion = 2.5f;
    public Rigidbody2D player;

    void Update()
    {
        float x = Input.GetAxisRaw("Horizontal");
        float y = Input.GetAxisRaw("Vertical");

        Vector2 direction = new Vector2(x, y).normalized;
        player.velocity = direction * speedLocomotion;      
    }
}

Your problem is going to be that you are putting both the x and the y into the vector. Of course, it will be diagonal if you do that, as x and y are both going to possibly be different from zero. What you need to do it apply one, or the other, but not both. I would use an if statement, decide if x is bigger or y is bigger, and then apply that one, only that one. By adding the if statement, you allow both axes to be moved, and despite that, only move on one of them.

Well observed kburkhart thank you!
I solved my problem using this code.

public class Moviment : MonoBehaviour
{
    public Rigidbody2D player;
    private Vector2 speedPlayerX;
    private Vector2 speedPlayerY;

    void Start()
    {
        player = GetComponent<Rigidbody2D>();
        speedPlayerX = new Vector2(2, 0); // move at position X
        speedPlayerY = new Vector2(0, 2); // move at position Y
    }

    void Update()
    {
        //Mount the condition before inserting this code
        if (Input.GetKey("w"))
        {
            player.MovePosition(player.position + speedPlayerY * Time.deltaTime); // Move Up
        }
        if (Input.GetKey("s"))
        {
            player.MovePosition(player.position + -speedPlayerY * Time.deltaTime); // Move Down
        }
        if (Input.GetKey("a"))
        {
            player.MovePosition(player.position + -speedPlayerX * Time.deltaTime); // Move Left
        }
        if (Input.GetKey("d"))
        {
            player.MovePosition(player.position + speedPlayerX * Time.deltaTime); // Move Right
        }       
    } 
}
}

Uhuuuuu! :smile:

Hmm, you appear to still have the same problem. Unless you want to allow diagonal movement, you need a change there. If you press ‘w’ and ‘d’ at once, it will move diagonal. If that is what you want, OK, but if not, you need to set a variable or do something similar so that once you have either a right or left movement, you don’t allow the up/down, or the other way around.

This would stop moving object when both axis / buttons are pressed, and I’d say that this is much better solution for your problem:

        float x = Input.GetAxisRaw("Horizontal");
        float y = Input.GetAxisRaw("Vertical");

        int isDiagonal = x * y != 0 ? 0 : 1;

        transform.position += new Vector3(x, y, 0) * isDiagonal * Time.deltaTime;

The issue I see with that method is that it totally stops movement. I believe the OP still wants movement, but only in whichever direction is more prominent(more pressed down). Of course, if we had more information on the exact needs, we could be better at giving the answer.

Guys my problem has been solved with the code I mentioned above, if the Player press two buttons at the same time you can not walk diagonally.

My code is hughe I know! but my code also works in case you want to make your character keep walking in the last direction he decided to walk…

example: you press d and your char walks right then you simultaneously press w or a and he keeps walking right…
this code is good bc you dont lock your char to either of the axes and also because you can make your character keep moving instead of stoping him from move!

Hope I helped!

void Update()
{

movex = Input.GetAxis(“Horizontal”);
movey = Input.GetAxis(“Vertical”);
if (Input.GetKey(“w”) && Input.GetKey(“a”) == false && Input.GetKey(“d”) == false)
{

last = 1;
}
else if (Input.GetKey(“s”) && Input.GetKey(“a”) == false && Input.GetKey(“d”) == false)
{

last = 2;
}
else if (Input.GetKey(“a”) && Input.GetKey(“w”) == false && Input.GetKey(“s”) == false)
{

last = 3;
}
else if (Input.GetKey(“d”) && Input.GetKey(“w”) == false && Input.GetKey(“s”) == false)
{

last = 4;
}
else if(Input.GetKey(“d”) == false && Input.GetKey(“w”) == false && Input.GetKey(“s”) == false && Input.GetKey(“a”) == false)
{
last = 5;
}
if (last == 1)
{
rbd.velocity = new Vector2(0, 1 * speed);
}
else if (last == 2)
{
rbd.velocity = new Vector2(0, -1 * speed);
} else if (last == 3)
{
rbd.velocity = new Vector2(-1 * speed, 0);
}
else if (last == 4)
{
rbd.velocity = new Vector2(1 * speed, 0);
}
else if (last == 5)
{
rbd.velocity = new Vector2(0, 0);
}

if (movex < 0 && movey == 0)
{
anim.Play(“alkon2”);
}
else if (movex > 0 && movey == 0)
{
anim.Play(“alkon”);
}
else if (movey < 0 && movex == 0)
{
anim.Play(“downoz”);
}
else if (movey > 0 && movex == 0)
{
anim.Play(“upon”);
}
if (movex == 0 && movey == 0)
{
anim.Play(“Idle”);
}

}

Please note the dates on posts, this was from 2016 so nearly 6 years old. Also, please use code-tags when posting code; plain text isn’t useful.