How to lock Y Rotation

Hello, I’m trying to make an ai code that follows and rotates towards the player. The code I’m using is.

var rotation = Quaternion.LookRotation(player.position - transform.position);
	transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damp);

Is there anyway to lock the Y axis or make sure it doesn’t rotate on the Y axis? thanks!

Make a new empty Object with a box collider and set the size of the collider for the distance you want to start the script on colliding with the Player. place the empty game object in the Enemy you made as a children
and try this on the EmptyObject it will work.

var Myself : Transform; 
var WalkSpeed : float;
var Player : Transform;
var PlayerAlert : int;
function Start () {

}

function Update () {
       if(PlayerAlert == 1){
            Myself.rotation = Quaternion.LookRotation(Vector3(Player.position.x, Myself.position.y, Player.position.z) - Myself.position);
            Myself.transform.position += Myself.transform.forward * WalkSpeed * Time.deltaTime;  
}

function OnTriggerEnter(other : Collider){
       if(other.gameObject.CompareTag("Player")){

              PlayerAlert = 1;
       }
}

it will follow the player…set the myself to the main enemy object.
and put a tag “player” on the object you want this to follow.

If you want to lock the y-axis rotation for all the duration of the game, look at the Inspector of your object on which that script is attached. Go under Rigidbody->Constraints->Freeze Rotation. At that point, just check the “Y” box.

Well I had written thislast night and deleted because it felt out-of-place. Its still not clear if its what you’re looking but… here. What I often do is create a helper function that returns any position with my own Y, so my actors keep their feet on the ground, JS example:

function CleanPosition(position:Vector3):Vector3
{
    position.y = transform.position.y;
    return position;
}

Then anytime you LookAt() or Move(), you should send the vector3 through this method. It might not blend so well into your already written AI, but perhaps it will. Its simple and worked well for me.