- Home /
Determining Look Rotation As > A Number?
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?
Answer by aldonaletto · Nov 22, 2012 at 01:43 AM
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.
Your answer
Follow this Question
Related Questions
A little help with this camera script please? 3 Answers
Finding A Rigidbody's Rotation Speed And Direction 2 Answers
Lerp with raycast hit.normal? 2 Answers
Damping Rotation Reset 1 Answer
Rotate object following mouse movement. Object jumps/ flips 1 Answer