- Home /
This question was
closed May 28, 2014 at 09:23 PM by
meat5000 for the following reason:
Duplicate, extremely well documented Question!
Question by
KaiserMagnus · May 28, 2014 at 08:50 PM ·
rotationquaternionanglescalculations
Calculating angles to a target and turning towards it
I'm making a game where my object needs to rotate to face towards a target. At the moment I'm using trigonometry to determine whether to turn left or right, like so
Vector3 TargetPosition;
Update(){
Vector3 relativePosition = transform.rotation * (transform.position - TargetPosition);
float tanA = relativePosition.x / relativePosition.z;
if (tanA > 0f) rotationVector.y = 1f; //turn right
else rotationVector.y = -1f; //turn left
However the Object always seems to rotate 45 degrees of the quadrant.
Comment
Follow this Question
Related Questions
Rotate on one axis and check if angle is inside desired range 2 Answers
Set rotation based on 1,1,1 style vector? How to convert vector3 to quaternion? 1 Answer
How to Find an Object close to where the Player is Looking 1 Answer
How do I rotate a game object in 45 degree steps per keystroke? 2 Answers
Rotation like in editor 2 Answers