# Quaternion LookRotation

Hi guys - I’m making a tower defence game, yet I’ve come across a problem.

I have a Trigger Sphere Collider on my turrent, and when the enemy object comes in range, the tower follows and shoots. However, for some reason, the tower rotates in the X axis, which shouldn’t happen - it should only rotate on the Y.

Any clues?

``````function Update() {
if (myTarget) {
if (Time.time >= nextMoveTime) {
CalculateAimPosition(myTarget.position);
turretBall.rotation = Quaternion.Lerp(turretBall.rotation, desiredRotation, Time.deltaTime * turnSpeed);
}

if (Time.time >= nextFireTime) {
FireProjectile();
}
}
}

function CalculateAimPosition(targetPos : Vector3) {
var aimPoint = Vector3(targetPos.x + aimError, targetPos.y + aimError, targetPos.z + aimError);
desiredRotation = Quaternion.LookRotation(aimPoint);
}

function CalculateAimError() {
aimError = Random.Range(-errorAmount, errorAmount);
}
``````

You say “shouldn’t”, but I don’t see any code here that is intended to make that happen. LookRotation() will look at whatever position you specify. The typical way to solve this problem is to bring the y value of the target down to the level of the turret.
But you have a second problem here. LookRotation() is looking for a zero based vector, not a position in world space. The code you have here will only work if your turret is at the origin.

So to fix the two problems you would have something like this:

``````targetPos = targetPos - transform.position;
var aimPoint = Vector3(targetPos.x + aimError, transform.position.y, targetPos.z + aimError);
``````

Okay, figured it out.

Instead of Lerd, it is Slerp… and that’s it - one letter difference