Problems With AI LookAt Script

MAIN ISSUE SOLVED, I HAVE ANOTHER, BUT I AM WORKING ON IT :slight_smile:

I am trying to use an AI following script where my enemy follows the player when it is within a certain distance of him, and “look at” him while it is moving.
I have the enemy following the player at all times, but when the enemy is close it leans forward and to the side (on the x and zed axis’)
i only want the enemy to rotate on the y axis.
I have already tried locking the rotation in the rigid body, but it does not work.

My code:

var target : Transform; //the enemy's target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
 
var myTransform : Transform; //current transform data of this enemy
 
function Awake()
{
    myTransform = transform; //cache transform data for easy access/preformance
}
 
function Start()
{
     target = GameObject.FindWithTag("Player").transform; //target the player
 
}
 
function Update () {
    //rotate to look at the player
    myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
    Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
Quaternion.LookRotation(Vector3(myTransform.position.x, target.position.y, myTransform.position.z) - myTransform.position);
    //move towards the player
    myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
 
 
}

Thank you in advance for your help.
-JStaples

Just an update, i tried adding this to my code, but it causes me to get a run time error that crashes unity.

var lookPos = target.position - transform.position;
lookPos.y = 0;
var rotation = Quaternion.LookRotation(lookPos);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);

MAIN PROBLEM SOLVED!

With this code i managed to kimit the rotation to the y axis.
i am now trying to stop translation on the y-axis (up and down)

var target : Transform; //the enemy's target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning

var myTransform : Transform; //current transform data of this enemy

function Awake()
{
    myTransform = transform; //cache transform data for easy access/preformance
}

function Start()
{
     target = GameObject.FindWithTag("Player").transform; //target the player
}

function Update () {
    //rotate to look at the player
    myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
   Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.rotation = Quaternion.LookRotation(Vector3(target.position.x, myTransform.position.y, target.position.z) - myTransform.position);
//the line above is the changed code, it is limiting it only to rotate on the y axis.
    //move towards the player
    myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}

okay, so after a few trie i have come up with this. it is doing everything i want it too.

var target : Transform; //the enemy's target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning

var myTransform : Transform; //current transform data of this enemy

function Awake()
{
    myTransform = transform; //cache transform data for easy access/preformance
}

function Start()
{
     target = GameObject.FindWithTag("Player").transform; //target the player
}

function Update () {
    //rotate to look at the player
    myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
   Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.rotation = Quaternion.LookRotation(Vector3(target.position.x, myTransform.position.y, target.position.z) - myTransform.position);
    
    
    //check distance to target
    if (Vector3.Distance(target.transform.position, transform.position) > 5) {

	//move towards the player
    myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}    
}