Answers for "Angle between two vectors"
http://answers.unity.com/questions/317648/angle-between-two-vectors.html
The latest answers for the question "Angle between two vectors"Answer by elliselkins
http://answers.unity.com/answers/1174920/view.html
Ran into trouble with the Vector3.Angle function, it was returning incorrect values. Found out there is a problem with this function when the vectors are too small, as seen in [this question and answer][1]. So I wrote my own angle functions that use Mathf.Atan2:
//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 degrees
public static float AngleInDeg(Vector3 vec1, Vector3 vec2) {
return AngleInRad(vec1, vec2) * 180 / Mathf.PI;
}
See [Atan2][2] for more details.
[1]: http://answers.unity3d.com/questions/160514/vector3angle-returning-wrong-values-for-vectors-wi.html#answer-1174912
[2]: http://docs.unity3d.com/ScriptReference/Mathf.Atan2.htmlThu, 21 Apr 2016 20:31:17 GMTelliselkinsAnswer by Skibur
http://answers.unity.com/answers/523066/view.html
Since you are using two dimensional grid axis, I use Vector2. using Vector3 is nothing different than the one I wrote;
void Update(){
Vector2 PointA = new Vector2(z, y);
Vector2 PointB = new Vector2(z, y);
float Angle = Vector2.Angle(PointA, PointB); //If the angle isn't correctly at 0, you can subtract this value by the offset degree
Debug.Log("Angle of PointA to PointB is " + Angle);
}
This is done via C#.Sat, 24 Aug 2013 06:20:26 GMTSkiburAnswer by fafase
http://answers.unity.com/answers/317703/view.html
@DarkMatter's answer is the solution for fast and easy solution with no possibility of error.
Now here the mathematics solution that lies under the Angle function in case you would need to stop on the way for any reason OR you want to make it hard on you:
var vec1=Vector3(2,3,4);
var vec2= Vector3(1,-2,3);
//Get the dot product
var dot:float = Vector3.Dot(vec1,vec2);
// Divide the dot by the product of the magnitudes of the vectors
dot = dot/(vec1.magnitude*vec2.magnitude);
//Get the arc cosin of the angle, you now have your angle in radians
var acos = Mathf.Acos(dot);
//Multiply by 180/Mathf.PI to convert to degrees
var angle = acos*180/Mathf.PI;
//Congrats, you made it really hard on yourself.
print(angle);Fri, 14 Sep 2012 05:55:54 GMTfafaseAnswer by DarkMatter
http://answers.unity.com/answers/317677/view.html
You can get the angle between two vectors using [Vector3.Angle][1](v3A, v3B)
http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Angle.html
[1]: http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Angle.htmlFri, 14 Sep 2012 04:55:54 GMTDarkMatter