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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Angle between rotations (ignoring longitudinal axis)

Discussion in 'Scripting' started by Epimolophant, Feb 10, 2023.

  1. Epimolophant

    Epimolophant

    Joined:
    Dec 6, 2014
    Posts:
    33
    To make it simple, I need to check if a spaceship is moving approximately in the same direction as another spaceship, and get the angle between them.

    Have tried Quaternion.Angle, but it also adds if the ship is rotated on its own longitudinal axis. For example, two ships facing the same direction in 3D space, but one is upside down relative to the other, will result in a 180° angle, and I needed 0°.

    How to properly calculate this? Thanks!
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    Think you can just use some math after you turn their direction into a Vector



    You can just get the Vector they're moving by doing
    transform.forward
    I believe, which you can just plug into this thing above. Technically, if you don't need the angle and just how much they're going in the same direction, you can just use the dot product which returns a value between 1 and -1 of how parallel they are.
     
    chemicalcrux likes this.
  3. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    This is a perfect case to use the dot product!

    Code (CSharp):
    1. Vector3 myShip = transform.forward;
    2. Vector3 theirShip = target.transform.forward;
    3.  
    4. float product = Vector3.Dot(myShip, theirShip);
    The dot product is 1 if they're aligned and -1 if they're going in opposite directions. It's 0 if they're perpendicular.
     
    halley and Epimolophant like this.
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    chemicalcrux likes this.
  5. Epimolophant

    Epimolophant

    Joined:
    Dec 6, 2014
    Posts:
    33
    Thank you for all answers. This one worked perfectly for what I needed!
     
  6. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,925
    Dot-product is generally seen as an optimization, since it gives the cosin of the angle, while being faster than computing the angle. If you want to check for "facing away from me", than as you note, dotP<0 works great. Of if you want to check for "within 45 degrees" you can use dotP<0.707 (since most adults have the cosin of 45 memorized). But as you tinker with it you'll be mentally converting back and forth and it's often faster (for the coder) and less error-prone to just use Vector3.Angle(v1,v2)<45.

    Don't get me wrong -- I can use dot-product just fine, the same way I can climb into my car through the window, and yet oddly, almost never do either.
     
    chemicalcrux and Bunny83 like this.
  7. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    Yeah, given that it's just as easy to call Angle, that's what you should generally be using. I think I just use it out of force-of-habit (and then wind up having to figure out what the cosine of the angle I want is...)