Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Aug 24, 2013 at 06:21 AM by fafase for the following reason:

The question is answered, right answer was accepted

avatar image
13
Question by aljndrrr · Sep 14, 2012 at 12:06 AM · mathanglevectors

Angle between two vectors

How can I obtain the angle between two vectors, for example I have the following: alt text

I know that the angle (in degrees) between A and B1 is 0, but how can I know the angle between A and B2, considering the axis orientation of the gameobject...

Any suggestions?

angle-vectors.jpg (17.1 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image andrew_pearce · Jul 05, 2020 at 03:56 PM 0
Share

Why we simply not check is degree above or bellow X axis and simply add 360?

 float AngleBetweenVectors(Vector2 a, Vector2 b) {
   float degree = $$anonymous$$athf.Atan2(b.y - a.y, b.x - a.x) * $$anonymous$$athf.Rad2Deg;
   return (degree > 0) ? degree : 360 + degree;
 }

This works fine without extra calculations or even use top as zero degree and then negative degree will work just find with Unity:

   private float AngleBetweenVectors(Vector2 a, Vector2 b) {
     return -$$anonymous$$athf.Atan2(b.x - a.x, b.y - a.y) * $$anonymous$$athf.Rad2Deg;
   }

4 Replies

  • Sort: 
avatar image
29
Best Answer

Answer by DarkMatter · Sep 14, 2012 at 04:55 AM

You can get the angle between two vectors using Vector3.Angle(v3A, v3B)

http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Angle.html

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fermmmm · Jun 14, 2016 at 07:01 PM 7
Share

This is my solution that does not have the problem of the never negative angle and is useful to understand how to use Vector2.Angle.

 private float AngleBetweenVector2(Vector2 vec1, Vector2 vec2)
 {
         Vector2 diference = vec2 - vec1;
         float sign = (vec2.y < vec1.y)? -1.0f : 1.0f;
         return Vector2.Angle(Vector2.right, diference) * sign;
     }

avatar image SquadMc fermmmm · Jul 07, 2017 at 06:00 PM 0
Share

fermmmm's solution worked for me as it was important to have the positive and negative values for differentiation. Thanks for it!

avatar image AntzyP fermmmm · Jul 11, 2017 at 09:31 AM 2
Share

How is fermmmm's script correct? It returns the angle between difference of the vectors and right vector! Assu$$anonymous$$g vec1 is Vector2.right and vec2 is Vecor2.up, the angle between them should be +90 degrees or -270 degrees(if anti-clockwise is positive rotation, as in case of 2D unity rotations about Z axis). But this function will give 135 degrees which is clearly incorrect.

The correct script is:

 float AngleBetweenVector2(Vector2 vec1, Vector2 vec2)
 {
     Vector2 vec1Rotated90 = new Vector2(-vec1.y, vec1.x);
     float sign = (Vector2.Dot(vec1Rotated90, vec2) < 0) ? -1.0f : 1.0f;
     return Vector2.Angle(vec1, vec2) * sign;
 }

avatar image Lance_JZ AntzyP · Jul 11, 2017 at 08:32 PM 1
Share

Whatever works, is correct. You can do the same thing in different ways. I'm using it in my game AntzyP so yeah it works. The top one is for the tank barrel. The bottom one is for the tank turret.

 private float AngleTo(Vector2 pos, Vector2 target)
 {
     Vector2 diference = Vector2.zero;

     if (target.x > pos.x)
         diference = target - pos;
     else
         diference = pos - target;

     return Vector2.Angle(Vector2.right, diference);
 }

 private float AngleTo(Vector2 pos, Vector2 target)
 {
     Vector2 diference = target - pos;
     float sign = (target.y < pos.y) ? -1.0f : 1.0f;
     return Vector2.Angle(Vector2.right, diference) * sign;
 }
Show more comments
avatar image Lance_JZ · Jun 29, 2017 at 11:08 PM 1
Share

fermmmm's solution is best. He should have posted that as a separate post. His works perfectly, whereas the one above does not work at all. I had the same issue.

avatar image
18

Answer by fafase · Sep 14, 2012 at 05:55 AM

@DarkMatter's answer is the solution for fast and easy solution with no possibility of error.

Now here the mathematics solution that lies under the Angle function in case you would need to stop on the way for any reason OR you want to make it hard on you:

 var vec1=Vector3(2,3,4); 
 var vec2= Vector3(1,-2,3);
 //Get the dot product
 var dot:float = Vector3.Dot(vec1,vec2);
 // Divide the dot by the product of the magnitudes of the vectors
 dot = dot/(vec1.magnitude*vec2.magnitude);
 //Get the arc cosin of the angle, you now have your angle in radians 
 var acos = Mathf.Acos(dot);
 //Multiply by 180/Mathf.PI to convert to degrees
 var angle = acos*180/Mathf.PI;
 //Congrats, you made it really hard on yourself.
 print(angle);


Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nicloay · Aug 24, 2013 at 03:52 AM 0
Share

looks like is that result is exactly the same as in Vector3.Angle and it doesn't show negative values

avatar image aldonaletto · Aug 24, 2013 at 04:49 AM 6
Share

Yes, the result is exactly the same - that's similar to what Unity does internally to implement Vector3.Angle:

 public static float Angle(Vector3 from, Vector3 to)
 {
     return $$anonymous$$athf.Acos($$anonymous$$athf.Clamp(Vector3.Dot(from.normalized, to.normalized), -1f, 1f)) * 57.29578f;
 }

Since the cosine is symmetrical about 0, the angle is the same no matter if "to the left" or "to the right". If you want to know the side, the cross product may help you. Supposing that both vectors are roughly in the horizontal plane (XZ), the Y component of their cross product may define the sign - for instance:

 function SignedAngle(a: Vector3, b: Vector3){
   var angle = Vector3.Angle(a, b); // calculate angle
   // assume the sign of the cross product's Y component:
   return angle * $$anonymous$$athf.Sign(Vector3.Cross(a, b).y);
 }
avatar image fafase · Aug 24, 2013 at 06:01 AM 0
Share

You never get negative value because it is based on the cosine of the angle and looking at your trig circle, you will notice there is no -cos, it actually goes cos, sin, cos, -sin because cos = -cos. Rings a bell from high school? :)

So yep, going with the Cross product, you can see if the result vector goes up (pos) or down(neg) (conventional way to say as it does not necessarily goes up OR down, just one direction or the opposite). Then checking the direction tells you if the angle is pos or neg.

avatar image spiritLink · Aug 08, 2018 at 09:39 AM 0
Share

Thank you !

avatar image
8

Answer by elliselkins · Apr 21, 2016 at 08:31 PM

Ran into trouble with the Vector3.Angle function, it was returning incorrect values. Found out there is a problem with this function when the vectors are too small, as seen in this question and answer. So I wrote my own angle functions that use Mathf.Atan2:

 //This returns the angle in radians
 public static float AngleInRad(Vector3 vec1, Vector3 vec2) {
     return Mathf.Atan2(vec2.y - vec1.y, vec2.x - vec1.x);
 }
 
 //This returns the angle in degrees
 public static float AngleInDeg(Vector3 vec1, Vector3 vec2) {
     return AngleInRad(vec1, vec2) * 180 / Mathf.PI;
 }

See Atan2 for more details.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tokars · May 12, 2017 at 07:11 AM 0
Share

This is the best solution that I have found, very convenient. Thanks!
bullet.transform.rotation = Quaternion.Euler(0, 0, AngleInDeg(bullet.transform.position, target.transform.position));

avatar image FedericaVigg · Oct 13, 2017 at 01:29 PM 0
Share

Hello, I tried to use the code of @elliselkins, and I tested it with some vectors values, but is returning an incorrect value in this case: float test2 = CalculateAngleBetweenVectors (new Vector3 (1f, 0, 0), Vector3.up); return 135 ins$$anonymous$$d of 90 and float test3 = CalculateAngleBetweenVectors (new Vector3 (-1f, 0, 0), Vector3.up); return 45 ins$$anonymous$$d of -90. Any idea? Thanks

avatar image elliselkins FedericaVigg · Oct 16, 2017 at 02:31 PM 1
Share

Threw me for a loop for a $$anonymous$$ute too :).

This is because these functions measure the angle between the points, not the angle of 0,0 to these points. So take your first example, the first point is at 1,0, and the second point is at 0,1. Draw a line from the first point to the second. What angle did you have to draw that line? 135 degrees. If you drew 90 degrees then you'd be drawing straight up from the first point, but that's not where the second point is. If you want an answer of 90 degrees then this would do it: first point at 0,0, second point at 0,1.

$$anonymous$$ake sense?

avatar image bendangelo · Jun 27, 2018 at 06:48 PM 0
Share

This worked best for me.

avatar image Space_Cowboy27 · Jun 17, 2021 at 06:27 PM 0
Share

Thanks a lot, that works great!

avatar image
5

Answer by Skibur · Aug 24, 2013 at 06:20 AM

Since you are using two dimensional grid axis, I use Vector2. using Vector3 is nothing different than the one I wrote;

 void Update(){
    Vector2 PointA = new Vector2(z, y);
    Vector2 PointB = new Vector2(z, y);
    float Angle = Vector2.Angle(PointA, PointB);  //If the angle isn't correctly at 0, you can subtract this value by the offset degree
    Debug.Log("Angle of PointA to PointB is " + Angle);
 }

This is done via C#.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Need to place an object at a certain angle form the camera and at a certaine distance 0 Answers

How to get an object to turn to point to a direction using the shortest path. E.g. 15° to 345° in 30° instead of 330°. 0 Answers

Calculating Relative angles 1 Answer

How to calculate vector perpendicular to a direction vector? 2 Answers

Predicting the hit point on X axis, based on vector direction 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges