The question is answered, right answer was accepted
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:
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!!!
Answer by infilrtrator_55 · Oct 19, 2015 at 08:59 AM
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.
Answer by toromano · Oct 19, 2015 at 08:48 AM
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
Hah, I was trying something stupid which got copy pasted. Yes, dummy parent thing worked!
Answer by hexagonius · Oct 19, 2015 at 08:19 AM
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.
@hexagonius Thanks for replying. Do you think that make the turret child of a empty object and applying the script to that empty would solve this problem?
Follow this Question
Related Questions
Auto level an object 0 Answers
Flip 3D Character Rotation 180 on Y axis 1 Answer
How can I set rotation properly? 0 Answers
How to rotate a camera slowly along a tween? 1 Answer
Rotation problem 0 Answers