• 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 /
avatar image
1
Question by Adrian · Aug 26, 2011 at 11:15 AM · vector3mathanglevector3.angle

Vector3.Angle returning wrong values for vectors with small components

I tried to get the angle between two Vector3's using Vector3.Angle and got a return value I didn't expect.

The two vectors where (1, 0, 0) and (0.00001, 0, 0) and the angle between the two should obviously be 0. However, Vector3.Angle returns 90.

This isn't related to the length difference of the vectors, Vector3.Angle also reports the angle between (0.00001, 0, 0) and itself as 90. On the other hand, the difference between (1000000, 0, 0) and itself or (1, 0, 0) is reported correctly as 0.

Is this related to the algorithm used by Vector3.Angle or a bug? The documentation doesn't say anything about it expecting only vectors in a specific range or only normalized ones.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Adrian · Aug 26, 2011 at 11:24 AM

Normalizing probably is the issue here. The documentation on Vector3.Normalize states:

If the vector is too small to be normalized a zero vector will be returned.

Vector3.Angle probably normalizes the input vectors, calculating the angle between (1, 0, 0) and (0, 0, 0). Vector3.Angle also seems to return 90 when either of its input vectors has zero length.

Comment
Add comment · Show 3 · 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 $$anonymous$$ · Dec 16, 2013 at 04:42 PM 0
Share

In case anyone else looking for more details I've found out that Unity zeros all vectors shorter than 0.05f.

Just try this: Debug.Log ("Vector2.right 0.049 = " + Vector2.right 0.049f); results in a zeroed vector

avatar image chris.weigle · Dec 16, 2013 at 04:48 PM 1
Share

No, that's just due to the default ToString format for float.

Try:

Debug.Log ("Vector2.right 0.049 = " + (Vector2.right 0.049f).ToString("R"));

avatar image $$anonymous$$ · Dec 16, 2013 at 06:48 PM 0
Share

thanks for that, was so disappointed with unity :)

avatar image
4

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

I ran into this problem as well, so I wrote my own angle functions.

     //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 degress
     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 VirtualSUN · Nov 21, 2018 at 08:23 AM 0
Share

Ok, but something going wrong: A & B is a Vector2. if we do this $$anonymous$$athf.Atan2(B.y - A.y, B.x - A.x) * 180 / $$anonymous$$athf.PI then we got that A (1.0, -1.0) B (-1.0, 1.0) = 135

???

avatar image juanitogan VirtualSUN · Apr 16, 2019 at 11:00 PM 0
Share

$$anonymous$$athf functions return polar coordinates ins$$anonymous$$d of game coordinates.

avatar image VirtualSUN juanitogan · Apr 16, 2019 at 11:57 PM 0
Share

But the angle between A (1.0, -1.0) B (-1.0, 1.0) = 180

Show more comments
avatar image Bunny83 VirtualSUN · Apr 17, 2019 at 11:34 AM 1
Share

This answer may be misleading since it expects two 2d position vectors as inputs, not two direction vectors like Vector3.Angle. This method here also just calculates the mathematical counter clockwise angle between the worldspace x axis and the connection vector between the two points. This is fundamentally something different.


The result of 135 is right for the two point A=(1.0, -1.0) and B=(-1.0, 1.0). The actual direction vector will be (-2, 2) which is 135° away from the x axis.


Of course as i said this answer is providing a solution for a completely different / unrelated problem.

edit
Note that Vector3.Angle actually expects two direction vectors and it's implemented like this

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

How can I calculate the Vector3 b using Vector 3 a pos with an angle? 1 Answer

Result of subtracton vectors 1 Answer

What is the 3rd point of Vector3.Angle ? 1 Answer

Angle between two lines in 2D 3 Answers

How do I find slope on a spherical terrain 2 Answers


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