Comments and answers for "how to calculate the angle between two vectors?"
http://answers.unity.com/questions/24983/how-to-calculate-the-angle-between-two-vectors.html
The latest comments and answers for the question "how to calculate the angle between two vectors?"Answer by Necronomicron
http://answers.unity.com/answers/1410812/view.html
This is pretty simple task, just use cross product.
public static float Angle360(Vector2 v1, Vector2 v2)
{
Vector2 n1 = v1.normalized;
Vector2 n2 = v2.normalized;
float angle = Vector2.Angle(n1, n2);
return Mathf.Sign(Vector3.Cross(n1, n2).z) < 0 ? (360 - angle) % 360 : angle;
}Sat, 23 Sep 2017 01:54:39 GMTNecronomicronComment by NoseKills on NoseKills's answer
http://answers.unity.com/comments/1182818/view.html
This does not answer the question of how to get an angle between 2 vectors... and the question has been answered 6 years ago.Sat, 07 May 2016 10:44:15 GMTNoseKillsAnswer by dhurstdev
http://answers.unity.com/answers/1182637/view.html
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;
}Fri, 06 May 2016 23:08:44 GMTdhurstdevComment by SwirlyBitz on SwirlyBitz's answer
http://answers.unity.com/comments/1064642/view.html
The link is not available anymore :(Thu, 10 Sep 2015 14:22:22 GMTSwirlyBitzComment by demid on demid's answer
http://answers.unity.com/comments/952779/view.html
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][1]
[1]: http://forum.unity3d.com/threads/need-vector3-angle-to-return-a-negtive-or-relative-value.51092/#post-324018Wed, 22 Apr 2015 22:04:35 GMTdemidAnswer by green-coder
http://answers.unity.com/answers/834473/view.html
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);Tue, 18 Nov 2014 06:00:11 GMTgreen-coderComment by konsnos on konsnos's answer
http://answers.unity.com/comments/778829/view.html
@Peter-G As Ben said both answers are dead :( It would be helpful if your answer is updated.Tue, 26 Aug 2014 10:38:32 GMTkonsnosComment by Ben-BearFish on Ben-BearFish's answer
http://answers.unity.com/comments/731128/view.html
@Peter-G, I was wondering if you could update the links in your answer. They both seem to be dead.Thu, 19 Jun 2014 23:18:50 GMTBen-BearFishComment by Chris_E on Chris_E's answer
http://answers.unity.com/comments/716624/view.html
You mention that this won't work if the reference is Vector3.forward, but it should also be noted that it also can't be -Vector3.forward.Wed, 28 May 2014 17:34:38 GMTChris_EComment by insominx on insominx's answer
http://answers.unity.com/comments/638827/view.html
Yes, that reads better. Thanks! (change incorporated)Thu, 13 Feb 2014 16:24:17 GMTinsominxComment by monoRAIL on monoRAIL's answer
http://answers.unity.com/comments/638669/view.html
You could use [Transform.LookAt][1]
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]: https://docs.unity3d.com/Documentation/ScriptReference/Transform.LookAt.htmlThu, 13 Feb 2014 12:16:32 GMTmonoRAILComment by monoRAIL on monoRAIL's answer
http://answers.unity.com/comments/638664/view.html
Very helpful solution! You could simplify line 17 using Mathf.Sign like this:
float sign = Mathf.Sign (Vector3.Dot(newDirection, referenceRight));Thu, 13 Feb 2014 12:10:51 GMTmonoRAILComment by flamy on flamy's answer
http://answers.unity.com/comments/570770/view.html
cool solution thanksThu, 07 Nov 2013 09:34:41 GMTflamyComment by insominx on insominx's answer
http://answers.unity.com/comments/268830/view.html
Fixed. Thanks for the feedback.Sun, 17 Jun 2012 19:44:12 GMTinsominxComment by Simon 9 on Simon 9's answer
http://answers.unity.com/comments/268805/view.html
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.Sun, 17 Jun 2012 17:12:59 GMTSimon 9Answer by insominx
http://answers.unity.com/answers/262476/view.html
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.Mon, 04 Jun 2012 21:00:13 GMTinsominxComment by TakuanDaikon on TakuanDaikon's answer
http://answers.unity.com/comments/246398/view.html
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 :(Mon, 30 Apr 2012 14:05:13 GMTTakuanDaikonAnswer by Lloyd_Christmas
http://answers.unity.com/answers/231548/view.html
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.Sat, 24 Mar 2012 18:43:27 GMTLloyd_ChristmasComment by Ashkan_gc on Ashkan_gc's answer
http://answers.unity.com/comments/80563/view.html
thank you so much. i should really read some math books again.Sun, 15 Aug 2010 02:50:16 GMTAshkan_gcAnswer by Peter G
http://answers.unity.com/answers/24985/view.html
<p>I would reccomend these threads.</p>
<p><a href="http://forum.unity3d.com/viewtopic.php?t=56577&highlight=vector3+angle" rel="nofollow">http://forum.unity3d.com/viewtopic.php?t=56577&highlight=vector3+angle</a>
<a href="http://forum.unity3d.com/viewtopic.php?t=33313" rel="nofollow">http://forum.unity3d.com/viewtopic.php?t=33313</a></p>
<p>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. </p>Sat, 14 Aug 2010 18:46:36 GMTPeter G