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:
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:
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!!!