# Problem with turning tower in players direction (different angle and shaking)

Hello,

I have problem with turning tower in players direction. After some time it stops to look at him and starts to behave in strange ways (looking at him but in some different angle, shaking and constantly turning in one direction). It starts to looks at player after some time but very slowly. I don’t know why this is happening. I’ve spend a lot of time on this problem…

Look at those pictures to see it more clearly:

top:

game:

It happens when player jumps a lot around tower and on top of it. That’s when tower starts to freak out. When player goes away tower have all those described issues. My code looks like this:

#pragma strict
var _player: Transform;
var rotationSpeed: float = 1f;

function Start () {
_player = GameObject.FindWithTag("Player").transform;
}

function Update() {
var forward : Vector3 = transform.TransformDirection(Vector3.forward) * 20;
Debug.DrawRay (transform.position, forward, Color.green);

var forward2: Vector3 = (_player.position - transform.position) * 20;
forward2.z = 0;
Debug.DrawRay (transform.position, forward2, Color.magenta);

var dir = _player.position - transform.position;
dir.y = 0;
var rot = Quaternion.LookRotation(dir);
transform.rotation = Quaternion.Slerp(transform.rotation, rot, rotationSpeed * Time.deltaTime);
}

What I’m doing wrong? Is there any solution?

Thanks a lot for help!

Quaternion rotation = Quaternion.LookRotation(turret.position - new Vector3(player.position.x, turret.position.y, player.position.z));

turret.rotation = Quaternion.Lerp(turret.rotation, rotation, Time.deltaTime * rotationspeed);

you can use something like that if you want it to only rotate in the y axis.