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 Calculate angle between 2 vectors on any axis

Discussion in 'Scripting' started by RoeeHerzovich, Dec 6, 2020.

  1. RoeeHerzovich

    RoeeHerzovich

    Joined:
    Apr 12, 2020
    Posts:
    103
    I easily managed to retrieve the angle between 2 Vectors on the Z-X axis, but when I tried to calculate the angle between 2 vectors on their Y-axis (the Vectors are above each other and I'd like to calculate the angle between them).

    Instead of making it like that, I wonder if there is any way to calculate the angle between vectors from any axis/plane.

    thank you for your help :3
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Can you draw a picture of what you're trying to accomplish? Your description is a little confusing. Especially this part:
    For the purpose of calculating an angle between them, vectors can always be considered to start at the origin, and point in some direction. I'm not sure what you mean by them being above one another?

    In general, you can calculate the angle between two vectors using these functions in Unity:
    https://docs.unity3d.com/ScriptReference/Vector3.Angle.html
    https://docs.unity3d.com/ScriptReference/Vector3.SignedAngle.html
     
  3. RoeeHerzovich

    RoeeHerzovich

    Joined:
    Apr 12, 2020
    Posts:
    103

    I understand I was unclear. There is a center point both vectors come out of. By above each other I mean they have the same X and Z direction, but one is pointing down and the other up at certain degrees, which I'd like to find


    Perhaps the image can help undersatnd.. I want to calculate the angle between the green lines
     

    Attached Files:

    • IMG.JPG
      IMG.JPG
      File size:
      34.8 KB
      Views:
      351
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Vector3.Angle
    should do it for you.
     
  5. RoeeHerzovich

    RoeeHerzovich

    Joined:
    Apr 12, 2020
    Posts:
    103
    Then why people did things with Atan2 and etc just to find it if Vector3.Angle should've done that...
    oof..

    Anyways, I'll check that out and thank you so much

    EDIT:

    Vector3.Angle(from, to) does not return the same result as:


    Code (CSharp):
    1.         public static float AngleFromDirection(Vector3 position, Vector3 dir)
    2.         {
    3.             Vector3 forward = position + dir;
    4.             return Mathf.Rad2Deg * Mathf.Atan2(forward.z - position.z, forward.x - position.x);
    5.         }
    Which gives an accurate angle between two vectors but only on a flat circle (X-Z) axis.
     
    Last edited: Dec 6, 2020
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    The code you shared is doing something completely different from what I can tell. In fact, it doesn't really make a lot of sense at all. First it adds the "position" and "dir" vectors together, but then in the next line, the "position" vector is essentially removed again from the calculation and run through ATAN2. So I think this code would be simpler and equivalent to what you shared:
    Code (CSharp):
    1.         public static float AngleFromDirection(Vector3 dir)
    2.         {
    3.             return Mathf.Rad2Deg * Mathf.Atan2(dir.z, dir.x);
    4.         }
    And all that code is doing is calculating the degree of the angle between the positive X or Z axis (not sure which) and the "dir" vector, but ignoring the y component of the vector. In other words, your "position" vector is being ignored entirely.

    I'm not sure where you got that code from, but unless I'm mistaken it does not calculate the angle between two vectors.
     
  7. RoeeHerzovich

    RoeeHerzovich

    Joined:
    Apr 12, 2020
    Posts:
    103

    Looking at what you say it makes sense, I just don't understand how it works then...