- Home /
Question by
AlejandroGorgal · Nov 25, 2013 at 04:40 AM ·
rotationcontrolleraxisjava
How to lock character controller rotation axis? (Java)
Hi, I have a script on my enemy AI script which is in charge of making sure the enemy always looks at the player, it works but the problem is that if the player is above or below the enemy the latter will rotate on the z and x axis to look up or down, which doesnt look good at all. Im trying to find a way to lock the rotation to the y axis only but I cant figure it out, here's the code I have handling the rotation:
var Damping:float = 6.0; // Rotation damping speed
function lookAt ()
{
var rotation = Quaternion.LookRotation(Target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}
Any pointers?
Comment
Best Answer
Answer by diegzumillo · Nov 25, 2013 at 04:55 AM
Instead of using "Target.position" you could simply create a new vector like:
var LookHere : Vector3 = Vector3(Target.position.x, transform.position.y Target.position.z);
and use it on your lookat function.
Your answer
![](https://koobas.hobune.stream/wayback/20220613122700im_/https://answers.unity.com/themes/thub/images/avi.jpg)