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?

1 Answer

1

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.

That worked, thanks a bunch!