Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 /
avatar image
0
Question by cengizt95 · Aug 15, 2015 at 03:48 AM · unity 2dangle

How can i calculate angle ?

alt text

in 2D space how can i calculate angle which between 2 vector?

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

Answer by Bunny83 · Aug 15, 2015 at 09:21 AM

Unity already has a method that calculates the angle between two vectors (Vector2.Angle). An alternative is to calculate the dot product between the two directions. If both vectors are normalized the result is the cosine of your angle:

 Vector2 forward;
 Vector2 vector1;
 Vector2 vector2;
 
 Vector2 dir = vector2 - vector1;
 
 // first solution
 float angle = Vector2.Angle(forward, dir);
 
 // second solution
 float angle = Mathf.Acos(Vector2.Dot(forward.normalized, dir.normalized));

Keep in mind that the angle is always positive. If you need an angle based on the orientation of the two vectors you have to determine the order of the two direction vectors. The cross product is the easiest way:

 Vector tmp = Vector3.Cross(forward, dir);
 if (tmp.z > 0)
     // dir is above forward
 else
     // dir is below forward

That's all ^^.

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 Marquetes2 · Aug 30, 2018 at 12:15 PM 0
Share

Thank you so much, that was super usefull for a project I'm doing.

avatar image lslu · Oct 06, 2020 at 06:45 PM 0
Share

super good example thanks! I was just wondering how to use the cross product to check the rotation direction.

avatar image Bunny83 lslu · Oct 06, 2020 at 07:12 PM 0
Share

Actually Unity now has a SignedAngle method that does this internally and returns an angle between -180 and 180. You just have to provide a reference axis which is used to deter$$anonymous$$e the sign. For 2d you would just use Vector3.forward as axis.

avatar image
0

Answer by JoshuaStrunk · Aug 15, 2015 at 04:32 AM

Trig. This method only works if when you refer to forward you actually mean the Vector2.right or equivalent Vector3.right. In other words the angle between a vector and the positive x-axis

If you start at Vec1, and Vec2 is the direction you want the angle first

 //First you have to translate you Vec2 to be a direction vector in world space
 //You can do this by subtracting its start point from its end point.
 Vector2 vecD= vec2 - vec1;
 
 //This will get you the answer in radians 
 float b = Mathf.Acos( vecD.x/ vecD.magnitude);
 //This converts to degrees
 float bDegrees = Mathf.Deg2Rad * b;
 
 //Optional if you want your angles going below the X-axis to be negative
 //This only really makes sense when working in degrees
 if(vecD.y <0) {
    float bDegrees -= 360;
 }

Trig Functions
Finding an angle in a right angled triangle

Comment
Add comment · Show 2 · 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 Bunny83 · Aug 15, 2015 at 09:09 AM 1
Share

Uhm, that doesn't make much sense in this case. First of all we don't have a right triangle. For an arbitrary triangle you would have to use the "Law of cosines". However using the dot product is actually much simpler.

avatar image JoshuaStrunk · Aug 15, 2015 at 07:07 PM 0
Share

You were 100 percent correct I have gone ahead and updated to correct my answer for when forward is actually the positive x-axis.

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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Angles invert after flipping player by 180 on Y-axis in 2D. 0 Answers

Getting x and y from an angle. (Unity 2D) 1 Answer

How to calculate angle in the triangle formed by screen touches 1 Answer

Is there anyway i can get the scene back????? 0 Answers

URP lighting 2d not working on Android,URP 2D lighting not working on Android 0 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