- Home /
Rotate on X axies towards an objects height in 3D
Pretty much what I need to do is simulate an AI looking up and down in the same way that players do (with a separate object). Something like the AIs head looks up and down at the player but doesn't turn because his whole body turns instead. I'm having trouble with this and all my searches have come up with stuff that isn't quite what I need.
I managed to make some code do exactly what I want (including not rotating past 90/-90) but only if the player is standing at a specific side of the AI.
RotationVar = Quaternion.LookRotation(transform.position - Target.position, Vector3.right);
RotationVar.y = 0;
RotationVar.z = 0;
RotationVar.eulerAngles = transform.TransformDirection(new Vector3 (-RotationVar.eulerAngles.x, 0, 0));
transform.rotation = Quaternion.Slerp(transform.rotation, RotationVar, VerticalTurnSpeed * Time.deltaTime);
The transform.TransformDirection doesn't seem to have any effect on this as it still only works correctly in 1 direction (if the player is standing in a different direction it still faces that same direction as if it was doing it in global.
Any help would be appreciated.
Answer by robertbu · Jan 20, 2014 at 04:57 AM
I'm having to guess a bit about what you want to happen. I'm guessing you want the body to rotate around without looking up and down, and you want the head to do the up/down looking. This kind of movement is the same as used for Turrets...like a tank with a turret that goes around and a gun that handles elevation. If so, there are a number of different posts concerning this behavior. If the body is always aligned with world 'up', here is one post that should work:
http://answers.unity3d.com/questions/388185/make-the-turret-automatically-rotate-to-look-at-wh.html
Works perfectly, thanks man! Didn't think to search about a turret.
Your answer
Follow this Question
Related Questions
When applying a 90 degree rotation to Euler Angles, it is over/undershooting sometimes.. 2 Answers
eulerAngles + quaternion + car = help!?! 2 Answers
How to rotate an object around another 60 degrees with a keypress? 0 Answers
Rotating a model with increments 1 Answer
Transform.Rotate producing unexpected results when being used after setting localEulerAngles 0 Answers