Follow rotation on Y axis ONLY clockwise/counterclockwise.

The code I’m using now follows target position and lerps to target rotation. But when it can’t catch up, it changes direction since it becomes shorter way to the target. How can I follow rotation on Y axis ONLY clockwise/counterclockwise? I’ve been trying to find an answer for a long time. Please help.

    void FixedUpdate()
    {
        transform.position = new Vector3(follow.position.x, follow.position.y, follow.position.z);

        Quaternion followRotation = Quaternion.Euler(new Vector3(followRot.eulerAngles.x, followRot.eulerAngles.y, followRot.eulerAngles.z));

        transform.rotation = Quaternion.Slerp(transform.rotation, followRotation, followRotationSpeed * Time.fixedDeltaTime);
    }

1 Answer

1

Mathematical lerp functions are not best way to lerp angles. Consider using lerpangle for each individual rotation (x, y and z)

LerpAngle still always goes the shortest way

Oh sorry that I misunderstood your question. You need to use transform.rotate with constant or variable speed (it needs to always be positive or negative for the same direction). You can check the difference between euler angles with vector3.distance or you can just check the difference between the desired axis. If it is more than a delta value you can just keep rotating. Hope this helps.