- Home /
Rotate object in specific direction
I have gameobject that rotates (only by y axis) with compass from Input.compass.trueHeading
. For smoothing my rotation I use Mathf.SmoothDamp()
and it works perfect, but only when I'm rotating in clockwise direction. If I rotate my phone in counterclockwise direction and passing 0° (eg. from 12° to 350° in counterclockwise direction) my gameobject start to rotate in clockwise direction until it reach desired degree.
Before that I used Mathf.SmoothDampAngle()
for calculating my rotation, but than I had problem with short-way rotation (when I want to rotate my object from 12° to 350° in clockwise direction it starts to rotate in short-way direction, which is 22° by counterclockwise
So the question is: how I can improve my rotation and rotate my gameobject in specific direction by Imput.compass.trueHeading
?
Answer by unity_ek98vnTRplGj8Q · Mar 18, 2020 at 02:52 PM
Smooth the rotation itself instead of the angles using Quaternion.Slerp()
Quaternion target = Quaternion.Euler(0, desiredAngle, 0);
transform.rotation = Quaternion.Slerp(transform.rotation, target, speed * Time.deltaTime);
If this doesn't give you the rotation you desire, you could also try Quaternion.RotateTowards(), which will give you a slightly different kind of smoothing
transform.rotation = Quaternion.RotateTowards(transform.rotation, target, speed * Time.deltaTime);
I tried to use Quaternion.Slerp()
. The problem is this method give me short-way rotation as a result. Okay, I will try Quaternion.RotateTowards()
method