I’m trying to detect if the look rotation for an object is > a number, but the code doesn’t work:
var rotation = Quaternion.LookRotation(lookTarget.position - transform.position);
if (rotation.y > 30 || rotation.y < -30)
{
turning = true;
}
else
{
turning = false;
}
Theoretically it should, right?
No: rotation is a quaternion, and its components have nothing to do with those nice angles we see in the Inspector (they are actually transform.localEulerAngles).
If you want to get the angle between the player forward direction and the target direction, use this:
// get target direction:
var targetDir = lookTarget.position - transform.position;
// ignore any height difference:
targetDir.y = 0;
// turning is true if the angle is > 30, false otherwise:
turning = Vector3.Angle(targetDir, transform.forward) > 30;
Vector3.Angle returns an absolute value, thus it makes no difference if the target is to the right or to the left of the player.