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

Question How to detect Diagonal movement in 2D

Discussion in '2D' started by erinisepic9, Jul 17, 2023.

  1. erinisepic9

    erinisepic9

    Joined:
    Jun 15, 2020
    Posts:
    1
    So as part of a game I'm creating, I want to detect diagonal movement so I can change the sprite to suite it. This is the code I have so far in unity:

    Code (CSharp):
    1.  
    2. if (moveInput == Vector2.up && (moveInput == Vector2.right || moveInput == Vector2.left))
    3.         {
    4.             anim.SetBool("UpSideRun", true);
    5.         } else if (moveInput == Vector2.down && (moveInput == Vector2.right || moveInput == Vector2.left))
    6.         {
    7.             anim.SetBool("DownSideRun", true);
    8.         } else
    9.         {
    10.             anim.SetBool("DownSideRun", false);
    11.             anim.SetBool("UpSideRun", false);
    12.         }

    I at first tried to detect the diagonal movement by seeing if the Side Run bool and Up Run bool had been detected in the animator, but only one was able to be detected at a time.
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,342
    This looks useful: upload_2023-7-17_10-53-23.png
     
  3. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    your code will never work, the moveInput cannot be equal to 2 different vectors at the same time

    you should check for (1,1) for example


    what you have right now is - if moveinput == 0,1 && moveInput ==1,0, obv it can only be one of them