Comments and answers for "Angle between two objects' facing direction"
http://answers.unity.com/questions/249047/angle-between-two-objects-facing-direction.html
The latest comments and answers for the question "Angle between two objects' facing direction"Comment by Dorscherl on Dorscherl's answer
http://answers.unity.com/comments/1496443/view.html
Couple questions (I know this is old): Will this calculate the angle for a specific axis? You said it all angles can be returned as a Vector3 via eulerAngles. Is the angle signed? @Mehrdad995Fri, 20 Apr 2018 02:44:52 GMTDorscherlAnswer by Mehrdad995
http://answers.unity.com/answers/1378972/view.html
I personally found this more simple and understandable
public static float CalculateAngle180_v3(Vector3 fromDir, Vector3 toDir)
{
float angle = Quaternion.FromToRotation(fromDir, toDir).eulerAngles.y;
if(angle > 180){return angle - 360f;}
return angle;
}
you can also return all angles as a vector3Fri, 14 Jul 2017 14:24:48 GMTMehrdad995Comment by JoaoBorks on JoaoBorks's answer
http://answers.unity.com/comments/1377431/view.html
[https://docs.unity3d.com/ScriptReference/Vector3.SignedAngle.html][1]
Now it **does** support Clockwise and Counter Clockwise returns
[1]: https://docs.unity3d.com/ScriptReference/Vector3.SignedAngle.htmlTue, 11 Jul 2017 18:30:01 GMTJoaoBorksComment by Gkupce on Gkupce's answer
http://answers.unity.com/comments/1321640/view.html
Just a little tweak to make the function shorter (C#):
float AngleDir(Vector3 fwd, Vector3 targetDir)
{
Vector3 perp = Vector3.Cross(fwd, targetDir);
float dir = Vector3.Dot(perp, Vector3.up);
return Mathf.Sign(dir);
}Sat, 04 Mar 2017 23:17:02 GMTGkupceComment by Daniel G on Daniel G's answer
http://answers.unity.com/comments/611147/view.html
Hey @BobBobson108 Thanks for the C# version, ONE question, I read this whole question, and if im reading it right I could potentially check if my local objects y axis is pointing in the same direction as the Space.World y axis such as just vector3.up (Basically detecting if my gameobject is pointing in the same direction as the world or gobal axises) I only need to check if the two y axises (1.Being my GameObjects y axis) (2.Being a Vector3.Up) are pointing in the same direction. Let me know if im wrong thanks :DTue, 07 Jan 2014 14:11:40 GMTDaniel GComment by MaeniAck on MaeniAck's answer
http://answers.unity.com/comments/413460/view.html
Sorry for not marking you answers a correct sooner.
As the question posted was an analogy for just a part of what I was doing I tried a lot of methods out, including yours and coming to the conclusion I was thinking about it in a wrong fashion. I ended up using a combination of vector calculations and trigonometry to achieve the effect I was looking for.
Nevertheless for the question I posted this was a fulfilling answer. And it did help me.Sat, 09 Mar 2013 02:44:23 GMTMaeniAckComment by BobBobson108 on BobBobson108's answer
http://answers.unity.com/comments/413314/view.html
here it is in C#
float AngleDir (Vector3 fwd, Vector3 targetDir, Vector3 up) {
Vector3 perp = Vector3.Cross(fwd, targetDir);
float dir = Vector3.Dot(perp, up);
if (dir > 0) {
return 1;
} else if (dir < 0) {
return -1;
} else {
return 0;
}
}Fri, 08 Mar 2013 20:55:16 GMTBobBobson108Answer by Piflik
http://answers.unity.com/answers/249057/view.html
Vector3.Angle gives you the angle between two directions. Just put transform.forward of both objects in there, and it should give you the angle you need.
It has one limitation, though, it doesn't distinguish between CW and CCW. I wrote a function for that:
function angleDir(fwd: Vector3, targetDir: Vector3, up: Vector3) {
var perp: Vector3 = Vector3.Cross(fwd, targetDir);
var dir: float = Vector3.Dot(perp, up);
if (dir > 0.0) {
return 1.0;
} else if (dir < 0.0) {
return -1.0;
} else {
return 0.0;
}
}
It returns 1.0 for CW, -1.0 for CCW and 0.0, if the angle between the two objects is 0.Sun, 06 May 2012 18:08:16 GMTPiflik