# Enemy Rotation Problem

Hey guys, I have a problem here. I have been trying to fix it but i just couldn’t get it. I am still new in unity, started using unity about 7 weeks already.
Okay, so i have this script of my enemy following my main character, and what i want it to do is to rotate as well, but it seems like the enemy is rotating but in a weird way. here is the code
Thank you so much for helping.

``````var Player : Transform;
var MoveSpeed = 4;
var MaxDist = 100;
var MinDist = 50;
private var tr : int = 180;
private var moveDirection = Vector3.zero;

function Start ()
{

}

function Update ()
{
//transform.LookAt(Player);
var dist: float = Vector3.Distance(transform.position,Player.position);
var dir: Vector3 = Player.position - transform.position;
//Debug.Log(dir);
dir.Normalize();

if(dist > MinDist)
{

transform.position += dir * MoveSpeed * Time.deltaTime;

if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
{
transform.eulerAngles.y += (transform.eulerAngles.y - tr) / 2;
}

if(dir.x>0){
//looking to the right
tr = 180;

} else {
tr = -180;

}

// if(dir.x<0){
//looking to the left
//tr = 0;
//transform.eulerAngles.y += (transform.eulerAngles.y + tr) / 2;

}

}
``````

You say, “I only want it to rotate on the X axis,” but your existing code is rotating the object on the y axis. So I’m going to assume you really want a ‘y’ axis rotation. If so, this is likely what you are looking for:

``````function Update ()  {
var lookPos = Player.position;
lookPos.y = transform.position.y;
transform.LookAt(lookPos);

var dist: float = Vector3.Distance(transform.position,Player.position);
var dir: Vector3 = Player.position - transform.position;

dir.Normalize();

if(dist > MinDist) {
transform.position += dir * MoveSpeed * Time.deltaTime;

if(Vector3.Distance(transform.position,Player.position) <= MaxDist) {
transform.eulerAngles.y += (transform.eulerAngles.y - tr) / 2;
}
}
}
``````

It works by putting the Players position on the same ‘y’ as this object, resulting in a rotation only on the y axis.