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 /
avatar image
11
Question by Ashkan_gc · Aug 14, 2010 at 05:59 PM · vector3mathvector

how to calculate the angle between two vectors?

i need to calculate the angle between two vectors. the result should be something between 180 and -180 or 0 and 360. i mean Vector3.Angle is not useful because the result is between 0 and 180.

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

6 Replies

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

Answer by Peter G · Aug 14, 2010 at 06:46 PM

I would reccomend these threads.

http://forum.unity3d.com/viewtopic.php?t=56577&highlight=vector3+angle http://forum.unity3d.com/viewtopic.php?t=33313

The only way I've found is to determine if the objects are left or right of each other then subtract from 360 accordingly. It isn't real clean, but it works fine.

Comment
Add comment · Show 6 · 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 Ashkan_gc · Aug 15, 2010 at 02:50 AM 0
Share

thank you so much. i should really read some math books again.

avatar image TakuanDaikon · Apr 30, 2012 at 02:05 PM 2
Share

This turned out to be exactly the solution I was looking for, thank you!

EDIT - Now that Unity has changed their forum software, both links are dead. That is very unfortunate :(

avatar image Ben-BearFish · Jun 19, 2014 at 11:18 PM 1
Share

@Peter-G, I was wondering if you could update the links in your answer. They both seem to be dead.

avatar image konsnos · Aug 26, 2014 at 10:38 AM 0
Share

@Peter-G As Ben said both answers are dead :( It would be helpful if your answer is updated.

avatar image demid · Apr 22, 2015 at 10:04 PM 1
Share

I've found and used myself a robust solution from this answer: http://forum.unity3d.com/threads/need-vector3-angle-to-return-a-negtive-or-relative-value.51092/#post-324018

Show more comments
avatar image
16

Answer by insominx · Jun 04, 2012 at 09:00 PM

Here is both an easily understandable and computationally fast method for getting an angle from -180 to +180.

 // the vector that we want to measure an angle from
 Vector3 referenceForward = /* some vector that is not Vector3.up */

 // the vector perpendicular to referenceForward (90 degrees clockwise)
 // (used to determine if angle is positive or negative)
 Vector3 referenceRight= Vector3.Cross(Vector3.up, referenceForward);

 // the vector of interest
 Vector3 newDirection = /* some vector that we're interested in */

 // Get the angle in degrees between 0 and 180
 float angle = Vector3.Angle(newDirection, referenceForward);

 // Determine if the degree value should be negative.  Here, a positive value
 // from the dot product means that our vector is on the right of the reference vector   
 // whereas a negative value means we're on the left.
 float sign = Mathf.Sign(Vector3.Dot(newDirection, referenceRight));

 float finalAngle = sign * angle;

You can switch the 1.0f and -1.0f around above if you want the left side to be positive and right side negative instead.

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 Simon 9 · Jun 17, 2012 at 05:12 PM 2
Share

Thanks so much for this! It's extremely helpful.

BUT you have a mistake in there. You call it referenceDirection in the first line, and referenceForward in the second line. Should be the same name.

avatar image insominx · Jun 17, 2012 at 07:44 PM 0
Share

Fixed. Thanks for the feedback.

avatar image flamy · Nov 07, 2013 at 09:34 AM 0
Share

cool solution thanks

avatar image monoRAIL · Feb 13, 2014 at 12:10 PM 0
Share

Very helpful solution! You could simplify line 17 using $$anonymous$$athf.Sign like this:

 float sign = $$anonymous$$athf.Sign (Vector3.Dot(newDirection, referenceRight));
avatar image insominx · Feb 13, 2014 at 04:24 PM 0
Share

Yes, that reads better. Thanks! (change incorporated)

Show more comments
avatar image
8

Answer by green-coder · Nov 18, 2014 at 06:00 AM

This is my solution for getting the directed angle between 2D vectors :

 float angle = Mathf.DeltaAngle(Mathf.Atan2(source.y, source.x) * Mathf.Rad2Deg,
                                Mathf.Atan2(target.y, target.x) * Mathf.Rad2Deg);


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
avatar image
1

Answer by dhurstdev · May 06, 2016 at 11:08 PM

Everyone... I have wasted too much time on this, trying to calculate the angle and solve the BS... Apparently Quaternion.LookRotation, does this without having to work about correcting angles, or flipping! Amazing! http://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html Hope this helps: private void SetRotationFromWorldPoint(Transform trans, Vector3 worldPoint) { Quaternion currentRotation = trans.rotation; currentRotation = Quaternion.LookRotation(worldPoint); trans.rotation = currentRotation; }

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 NoseKills · May 07, 2016 at 10:44 AM 0
Share

This does not answer the question of how to get an angle between 2 vectors... and the question has been answered 6 years ago.

avatar image sebastianjimenez100 · Aug 03, 2020 at 07:31 AM 0
Share

Thank you, this is just what I needed!

avatar image
0

Answer by Lloyd_Christmas · Mar 24, 2012 at 06:43 PM

I have a question on this topic and the answer above does not seem to address it. My problem is this:

I have two spheres where are moving along a path using iTween (they represent two points along a cylinder - the middle and the top). The paths that the spheres travel on was determined through motion capture data of a person swinging a bat like object. I am trying to have a cylinder object follow a path that essentially goes through both these points. The two spheres are essentially markers on the cylinder object.

Right now I have a line renderer object that is drawing a line between both spheres. I want to replace the line renderer with a solid cylinder. For example, if the bottom marker is at (0,0,0) and the top marker is at (0,1,0) I would like my cylinder object to be perfectly upright along the y-axis. Similarly, if the bottom marker is at (0,0,0) and the top marker is at (0,0,1) I'd like the cylinder object to lie horizontal along the z-axis.

My approach is to have the cylinder follow one of the marker's path and find the angle made between both markers to have the cylinder object orient itself at that angle. This is proving to be quite difficult and I could use some help. Has anyone come across a similar problem?

Any help would be greatly appreciated. I've tried just about everything I could find to accomplish my goal but it just simply isn't working.

I would also like to do this via a script component because the two markers (and their paths) will be changing based on motion capture data.

Thanks for your help in advance.

Comment
Add comment · Show 1 · 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 monoRAIL · Feb 13, 2014 at 12:16 PM 0
Share

You could use Transform.LookAt

Place your object on one of the markers, and have it LookAt the other marker to get the correct rotation. You will need to create your object carefully so that its base is on the origin, and its forward Z axis is the LookAt direction.

  • 1
  • 2
  • ›

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

12 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

Related Questions

Non physics determinism of floating point math across platforms 1 Answer

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

Question about Vector creation 1 Answer

Find a point in space using Vector3 angles and Raycast 1 Answer

Applying direction into transform position 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