Search Unity

Question To find out the direction where object is moving in degrees

Discussion in '2D' started by cyberdrunkdev, Feb 10, 2023.

  1. cyberdrunkdev

    cyberdrunkdev

    Joined:
    Dec 6, 2022
    Posts:
    32
    I have a top-down object which is moving on a baked area, and i need to know the direction it's moving in degrees. So the movement's happening with mouse click.

    I've tried like this:
    Code (CSharp):
    1.  
    2. mvp = cam.ScreenToWorldPoint(Input.mousePosition);
    3. transform.LookAt(mvp);
    4. direction = transform.eulerAngles.y;
    5.  
    But it rotates the object in the direction of the click, and gives the angle, but i need the direction where the object is heading presently. I don't even need the object to rotate, i only need the direction it's heading in degrees.

    Possible?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
    You can use the two vectors from your observation point and get the subtended angle between those points.

    - You
    - OldPos
    - NewPos

    OldVector = OldPos - You
    NewVector = NewPos - You

    And that can be fed to Vector3.Angle() to get an angle.
     
    Deleted User and LethalGenes like this.
  3. cyberdrunkdev

    cyberdrunkdev

    Joined:
    Dec 6, 2022
    Posts:
    32
    Thank you for the idea, but my tiny brain can't comprehend.
    Do you mean like:
    Code (CSharp):
    1.  
    2. Vector3 oldPos, newPos;
    3. Void Start()
    4. {
    5.     oldPos = transform.position;
    6. }
    7. void Update()
    8. {
    9.    newPos = transform.position;
    10.     newPos = newPos - transform.position;
    11.     oldPos = oldPos - transform.position;
    12.     Vector3.Angle(newPos, oldPos);
    13. }
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
    Oh my goodness, I apologize, I mis-read your first post!!!!

    My post above is as if you were standing watching a ball go past you from left to right (like a tennis match) and seeing how many degrees it moves each frame.

    Let me try and answer your original question again:

    What you want is to subtract the current from the last (which your script partially does), and from this, use
    Mathf.Atan2()
    to determine angle in radians, then convert it to degrees by multiplying by
    Mathf.Rad2Deg


    So starting from your code above, I would change the guts of Update() to:

    Code (csharp):
    1.         // compute how much we moved this frame
    2.         Vector3 difference = transform.position - oldPos;
    3.  
    4.         // if it is at least a "reasonable" amount, display the angle of movement
    5.         if (difference.magnitude >= 0.001f)
    6.         {
    7.             float radians = Mathf.Atan2( difference.y, difference.x);
    8.  
    9.             float degrees = radians * Mathf.Rad2Deg;
    10.  
    11.             Debug.Log( "Degrees = " + degrees);
    12.         }
    13.  
    14.         // update our old position
    15.         oldPos = transform.position;
    You would not need
    newPos
    in this case.
     
    Last edited: Feb 10, 2023
    cyberdrunkdev and LethalGenes like this.
  5. cyberdrunkdev

    cyberdrunkdev

    Joined:
    Dec 6, 2022
    Posts:
    32
    Thanks for your effort :) I've been trying to make this work for 3 long hours already, and now it works. Thank you. Only one thing i still would like to ask. Now it gives me the results: Right=0, Left=180, Up = 90, Down = -90. Is there any way to make these show from 0 to 360 degrees? Thank you! :)
     
    Kurt-Dekker likes this.
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
    Yaaas, yaas... indeed there is!

    It will always return -180 to +180

    Therefore we can add 360...

    and then modulo 360

    Code (csharp):
    1. degrees = degrees + 360;
    2.  
    3. degrees = degrees % 360;
    Presto!
     
    cyberdrunkdev likes this.