• 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 nark · Jan 05, 2017 at 11:59 AM · angle

How Vector.Angle works internally in Unity?

Is it using the way this page shows? http://www.calculator.net/triangle-calculator.html

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
3
Best Answer

Answer by Bunny83 · Jan 05, 2017 at 12:41 PM

Vector3.Angle does this:

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

To break it further down:

  • Both vectors are normalized first. So their lengths is 1.0.

  • The dot product between two vectors is simply a.x*b.x + a.y*b.y + a.z*b.z This gives you a single float value.

  • The dot product is also defined as |a| * |b| * cos(alpha) where |a| is the length of vector a and "alpha" the angle between the two vectors.

  • Since both vectors have a length of 1.0 the result of the dot product is just the cosine of the angle.

  • Mathf.Clamp simply keeps the value in the range -1 to 1 to avoid errors as Mathf.Acos only accepts values in that range.

  • Mathf.Acos calculates the "inverse" cosine in radians.

  • The magic number 57.29578f is just 180f / PI to convert radians to degree

Keep in mind that Vector3.Angle returns an angle in the range 0° to 180° as 180 is the largest possible angle between two vectors.

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 nark · Jan 06, 2017 at 02:34 AM 0
Share

Thank you very much! But could you tell me where do you get the source code?

avatar image Eric5h5 nark · Jan 06, 2017 at 05:39 AM 1
Share

The source code is from UnityEngine.dll. And yes the clamp appears to be unnecessary.

avatar image nark · Jan 06, 2017 at 02:49 AM 0
Share

And another question: The result of Vector.Dot should between -1 and 1 as the two parameters are already normalized, so why need the $$anonymous$$athf.Clamp operation?

avatar image Bunny83 nark · Jan 06, 2017 at 05:50 PM 0
Share

Well, it's probably just an additional "safety net". Theoretically the value should never be larger than 1.0 but in some cases it might be due to rounding errors. $$anonymous$$athf.Acos is just a wrapper for $$anonymous$$ath.Acos and it will return NaN if the value is larger than 1 or smaller than -1. Even when it's just 1.0000001 larger.

avatar image
1

Answer by mikelortega · Jan 05, 2017 at 12:05 PM

Vector3.Angle returns the angle in degrees between two input vectors.

About the page you share. If you represent the triangle's sides with vectors, with this method you can get the angles on the corners.

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

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Get the angle of a ray in respect to a camera 1 Answer

Check if rotation (only on Y axis) of some object toward another is finshed? 0 Answers

How to calculate mobile device tilt/yaw/rotation angles? 0 Answers

Rotating 2D sprite to match surface. 0 Answers

By-pass the 'shortest route' aspect of Quaternion.Slerp 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