Quaternion.Angle returns the absolute angle between two transform.rotation. Is there a technic to get it signed, I am currently using cross products and vector comparison to check. It seems to me very cumbersome.
Is there a better technic or something built in actually?
To get the unsigned angle between two quaternions, you can use the Quaternion.Angle function:
var angle = Quaternion.Angle( rotationA, rotationB );
However to get a signed angle doesn't really make a lot of sense in 3D space, because the rotation could be in any 3d direction (not just in one of two 'flat' directions).
Unity does however provide the function "Mathf.DeltaAngle" to get the signed difference between two angles (not rotations), so perhaps what you want to do is convert your 3D rotations into 2D angles relative to a certain direction on a certain plane (using Atan2). For example, to compare the angle of rotationA with rotationB, projected on the X-Z plane, you could do this:
// get a "forward vector" for each rotation
var forwardA = rotationA * Vector3.forward;
var forwardB = rotationB * Vector3.forward;
// get a numeric angle for each vector, on the X-Z plane (relative to world forward)
var angleA = Mathf.Atan2(forwardA.x, forwardA.z) * Mathf.Rad2Deg;
var angleB = Mathf.Atan2(forwardB.x, forwardB.z) * Mathf.Rad2Deg;
// get the signed difference in these angles
var angleDiff = Mathf.DeltaAngle( angleA, angleB );
Angle differences on the X-Z plane would be suitable for "top down" angle differences - eg, determining the directional differences between cars on a terrain. For other situations, you might need to use a different plane such as X-Y or Y-Z.
Hope this solves your problem!
Thanks Duck, yes, signed angle make sense only against a plane or an axis, that's what I am am after. The code is as involving as is mine currently.
Super old answer, but it was the solution to getting AngularSpeed into Mecanim so that a NavMesh can control the movement, Mecanim can still show the proper animations. (This is because the "Stealth" tutorial 4.3 is not out yet, so I had to figure it out myself)
Thanks Duck, yes, signed angle make sense only against a plane or an axis, that's what I am am after. The code is as involving as is mine currently.
– Jean-FabreI love you. Seriously, thanks a lot!
– SaturnixSuper old answer, but it was the solution to getting AngularSpeed into Mecanim so that a NavMesh can control the movement, Mecanim can still show the proper animations. (This is because the "Stealth" tutorial 4.3 is not out yet, so I had to figure it out myself)
– infinitypbrMarvellous!
– KarwochHey, Duck, sorry to bother you, but for this example, is it the Z rotation? And are you assuming that the rotation is in eulerAngles?
– david.struverson