Smooth Rotation Solution - Quaternion

Hello everybody. I already saw some of us proposed alternative solutions for rotation.
I’ve tried the other solutions I’ve saw but I was not satisfied, so I wanted to try a new way. It is just 2 lines of code, but in my humble opinion it makes the rotation a lot smoother, especially when cliccking fast or behind the character.

Please don’t mind my reorganized code, I put some serialized values because I like them to be in the inspector. I’ve highlighted the 2 interested lines of code. Feel free to try it and let me know what you think about it.


Inspector

5 Likes

Thanks for sharing. This will help others. I like you solution. Awesome job :raised_hands:

1 Like

Can someone pleased provide a technical explanation of the difference between:

transform.forward = Vector3.Slerp(
    transform.forward, moveDirection, rotateSpeed * Time.deltaTime
);

and (from above)

Quaternion targetRotation = Quaternion.LookRotation(
    targetPosition - transform.position
);
transform.rotation = Quaternion.Slerp(
    transform.rotation, targetRotation, rotateSpeed * Time.deltaTime
);

the result looks really similar…

cheers

I believe the only difference is working with Vector3 or Quaternions, that’s it.
When you set the transofrm.forward to rotate an object, Unity converts it into Quaternions in the background to apply the rotation.
So if you’re like me and you find quaternions to be very strange an unintuitive then stick with the Vector3 rotation methods. But if you like Quaternions then go ahead and use them directly.

2 Likes

This is what I ended up discovering after a few hours of tinkering/searching as well.

My units weren’t rotating properly for some reason when I used Vector3.slerp. So I had to try to find an alternative solution for rotating them to face the move direction. Quaternion.slerp ended up working flawlessly for me and fixed my rotation issue.

I don’t know why my units weren’t rotating properly, but maybe it has something to do with gimbal lock.

Privacy & Terms