# Rotation Problem

Hi, I'm trying to make an object chase another object by moving and rotating towards it. It works, for the most part, but it also rotates 90 degrees around the x axis so that it's flying essentially head first towards the object. Any ideas?

The code:

``````var player : GameObject;
var speed : float=6f;
var range : float=10f;
var hitRange : float=5f;
var enemyDamage : float=10f;
var rotationSpeed : float=5f;
var damageTimer : float=0f;

function Start()
{
player = GameObject.FindGameObjectWithTag("1Player");
}

function Update()
{
//move towards player
var distance = Vector3.Distance(transform.position, player.transform.position);
if(distance<=range)
{
var delta = player.transform.position - transform.position;
delta.Normalize();
delta.y = 0;
var moveSpeed = speed * Time.deltaTime;
transform.position = transform.position + (delta * moveSpeed);

transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(delta),
rotationSpeed * Time.deltaTime);
transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0);

damageTimer+=Time.deltaTime;
if (distance < hitRange && damageTimer>=1.5)
{
damageTimer=0f;
player.SendMessageUpwards
}
}
}

``````

Okay, look for this line in your script:

``````transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0);

``````

And change it to either:

``````transform.eulerAngles = Vector3(transform.eulerAngles.y, 0, 0);

``````

or

``````transform.eulerAngles = Vector3(0, 0, transform.eulerAngles.y);

``````

See if that works

A small note: you might want to use

`transform.rotation = Quaternion.RotateTowards (transform.rotation, Quaternion.LookRotation(delta), rotationSpeed);`

`transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(delta), rotationSpeed * Time.deltaTime);`

It should do the same thing, but is prettier to look at. A few additional notes, it might be unnecessary to zero out the x and z rotations, because you never try to move the rotation to point anywhere out of the xz plane. That is, it only ever should have a y-rotate component because you set delta.y=0;. And now that I think about it, another way to "level out" your rotation would be to use

`transform.rotation = Quaternion.Euler(0, transform.eulerAngles.y, 0);`

Of course, I don't see what the exact problem with your code is, so I can't guarantee that this will fix it. If it doesn't, you might want to point your monkeys at Quaternion.FromToAngle because I really feel like there is an answer in that function, and I don't see what it is.

edit:

another thing we could do is project a "carrot" in front of this object, match the y position of the carrot to our y position, and then use transform.LookAt. That is:

``````var carrot : Transform = transform;
carrot.position = carrot.position + carrot.forward;
transform.LookAt(carrot);

``````

Possibly a cumbersome workaroundish approach, but that seems to be what I'm best at.

I am so sorry to all of you trying to help me! I can't believe I overlooked something so simple! Thank you for pointing me in the direction Mike Stampone, it was the fact that I had to rotate my model 270 degrees around the x to get him stood up right in Unity, so when transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0); was called it set it back to 0. Just changed that to transform.eulerAngles = Vector3(270, transform.eulerAngles.y, 0); and it works fine. Again, I sincerely apologise for being so idiotic!

Thanks again for all of your help, I still learn a lot! Kira