Weird rotation when using Slerp!!?

I have a gun turret, and I want it to smoothly rotate on global Y axis to face an near by enemy. I’m using Quaternion.Slerp() for this, here is the code:

                target = col.transform;
                direct = target.position - transform.position;
                direct.y = 0;
                rot = Quaternion.LookRotation(direct);
                rot = Quaternion.AngleAxis(rot.y, Vector3.up);
                transform.rotation = Quaternion.Slerp(transform.rotation, rot, Time.deltaTime * 2.0f);

The problem is even after giving planar coords. Slerp is rotating in all the directions. I’m using a simple low poly model to demonstrate:

alt text

I want the turret to rotate, and face the blue cube so that I can play fire animations and particles. But the turret object is rotating weirdly in all 3 directions like this:

alt text

As you can see the turret rotates to face upwards and not the way I wanted. Locally Z-axis is the up and maybe that is the problem? I still don’t get how quaternions work. I came across many similar problems but nothing worked for me. I’m smashing my head against the wall for 2 hours. Please help!!!

Why are you using Quaternion.AngleAxis ?

This should work:

target = col.transform;
var direct = target.position - transform.position;
direct.y = 0;
var rot = Quaternion.LookRotation(direct);
//rot = Quaternion.AngleAxis(rot.y, Vector3.up);
transform.rotation =  Quaternion.Slerp(transform.rotation, rot, Time.deltaTime * 2.0f);

if the axis is not right, you can use a dummy parent

you are correct, that is the problem. if you cannot rotate it any other way, wrap your turret in another gameobject which is setup correctly. LookRotation uses z as forward.

The problem was that the imported model for 3ds Max has messed up axis. So locally and globally model has different axes for a same direction like Z axis was up locally. This was messing up the rotations. I parented the turret to a empty and applied the script to it. Works flawlessly now.