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

Checking One Direction at a Time

Discussion in 'Scripting' started by aljstevens, Apr 7, 2020.

  1. aljstevens

    aljstevens

    Joined:
    Mar 15, 2019
    Posts:
    16
    I'm currently working on a top down character controller. I want the way the player animates when they face different directions. So if they face front they'll play the animation run forward or if they face left play the animation run forward. I want to do this to avoid the player looking like they're moon walking.

    I have this code right now that finds which way the player is facing. However it reads two positions at the same time. So if I'm facing front it'll tell me I'm facing front and left.

    Code (CSharp):
    1.    public Transform Othertransform;
    2.     // Update is called once per frame
    3.     void Update()
    4.     {
    5.         if (Vector3.Dot(transform.right, Othertransform.position) > 0)
    6.         {
    7.             Debug.Log("Is right");
    8.         }
    9.  
    10.         if (Vector3.Dot(transform.right, Othertransform.position) < 0)
    11.         {
    12.             Debug.Log("Is left");
    13.         }
    14.  
    15.         if (Vector3.Dot(transform.forward, Othertransform.position) > 0)
    16.         {
    17.             Debug.Log("Is front");
    18.         }
    19.  
    20.         if (Vector3.Dot(transform.right, Othertransform.position) < 0)
    21.         {
    22.             Debug.Log("Is back");
    23.         }
    24.     }
    The Othertransform is just an object I have off the level to the North of the player. Does anyone know how to help me with this issue?

    Thank you I really appreciate it!
     
  2. jasonatkaruna

    jasonatkaruna

    Joined:
    Feb 26, 2019
    Posts:
    64
    Your current code is checking for the case when the two vectors are within 90 degrees of eachother. The dot product returns 0 at 90 degrees. You're actually looking for the case when the two vectors are within 45 degrees of eachother. You should use
    Vector3.Angle
    to find the angle, then test if it's 45 or larger. Your back is using
    transform.right
    which might be a bug. You can do something like the following:

    Code (CSharp):
    1. var angle = Vector3.SignedAngle(transform.forward, North.position, North.up); // unity uses LHR for positivity
    2.  
    3. if (angle >= 135f || angle < -135f); // facing South
    4. else if (angle >= 45f); // facing East
    5. else if (angle < -45f); // facing West
    6. else; // facing North
    7.  
    8.  
    Your code and this function also accounts for the height differences between the two vectors. If your player can look up and down, you might want to project their vector onto the xz plane of your compass.
     
    Last edited: Apr 7, 2020
    aljstevens likes this.
  3. aljstevens

    aljstevens

    Joined:
    Mar 15, 2019
    Posts:
    16
    Wow thanks so much! I really really really appreciate this. This problem has been stumping me all day and last night!
     
    jasonatkaruna likes this.