Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Prevent diagonally movement - C# [resolved]

Discussion in 'Scripting' started by LTLeonardo, May 13, 2016.

  1. LTLeonardo

    LTLeonardo

    Joined:
    Nov 13, 2015
    Posts:
    20
    Hello!
    I do not want the player walks diagonally.
    How do I prevent this using this code?

    Code (CSharp):
    1. public class Moviment : MonoBehaviour
    2. {
    3.     private float speedLocomotion = 2.5f;
    4.     public Rigidbody2D player;
    5.  
    6.     void Update()
    7.     {
    8.         float x = Input.GetAxisRaw("Horizontal");
    9.         float y = Input.GetAxisRaw("Vertical");
    10.  
    11.         Vector2 direction = new Vector2(x, y).normalized;
    12.         player.velocity = direction * speedLocomotion;      
    13.     }
    14. }
     
  2. kburkhart84

    kburkhart84

    Joined:
    Apr 28, 2012
    Posts:
    910
    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.
     
  3. LTLeonardo

    LTLeonardo

    Joined:
    Nov 13, 2015
    Posts:
    20

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

    Code (CSharp):
    1. public class Moviment : MonoBehaviour
    2. {
    3.     public Rigidbody2D player;
    4.     private Vector2 speedPlayerX;
    5.     private Vector2 speedPlayerY;
    6.  
    7.     void Start()
    8.     {
    9.         player = GetComponent<Rigidbody2D>();
    10.         speedPlayerX = new Vector2(2, 0); // move at position X
    11.         speedPlayerY = new Vector2(0, 2); // move at position Y
    12.     }
    13.  
    14.     void Update()
    15.     {
    16.         //Mount the condition before inserting this code
    17.         if (Input.GetKey("w"))
    18.         {
    19.             player.MovePosition(player.position + speedPlayerY * Time.deltaTime); // Move Up
    20.         }
    21.         if (Input.GetKey("s"))
    22.         {
    23.             player.MovePosition(player.position + -speedPlayerY * Time.deltaTime); // Move Down
    24.         }
    25.         if (Input.GetKey("a"))
    26.         {
    27.             player.MovePosition(player.position + -speedPlayerX * Time.deltaTime); // Move Left
    28.         }
    29.         if (Input.GetKey("d"))
    30.         {
    31.             player.MovePosition(player.position + speedPlayerX * Time.deltaTime); // Move Right
    32.         }      
    33.     }
    34. }
    35. }

    Uhuuuuu! :D
     
  4. kburkhart84

    kburkhart84

    Joined:
    Apr 28, 2012
    Posts:
    910
    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.
     
  5. Nitugard

    Nitugard

    Joined:
    May 10, 2015
    Posts:
    340
    This would stop moving object when both axis / buttons are pressed, and I'd say that this is much better solution for your problem:

    Code (CSharp):
    1.         float x = Input.GetAxisRaw("Horizontal");
    2.         float y = Input.GetAxisRaw("Vertical");
    3.  
    4.         int isDiagonal = x * y != 0 ? 0 : 1;
    5.  
    6.         transform.position += new Vector3(x, y, 0) * isDiagonal * Time.deltaTime;
    7.  
     
  6. kburkhart84

    kburkhart84

    Joined:
    Apr 28, 2012
    Posts:
    910
    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.
     
  7. LTLeonardo

    LTLeonardo

    Joined:
    Nov 13, 2015
    Posts:
    20
    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.
     
  8. miguedog

    miguedog

    Joined:
    Jan 1, 2022
    Posts:
    6
    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");
    }



    }
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,369
    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.