Rotating an object to a certain angle

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Turnovus

I’m trying to write a script that rotates an object towards a given angle over time.

For the sake of example, let’s say I’m trying to get angle A to match angle B.
My code looks something like this:

var RAMP_TURN  = 0.75

func _process(delta):
    #this will prevent angle A from wrapping around multiple times:
    if b > 180:  b -= 360
    if b < -180: b += 360

    var turn = min( RAMP_TURN, abs( A - B ) ) #limits rotation speed

    if a < b: a += turn
    if a > b: a -= turn

This works pretty well most of the time. However, whenever angle B goes above 180 or below -180 and wraps around, angle A has a tendency to take the long way around.

How can I get angle A to slowly approach angle B, while consistently taking the shortest way possible?

:bust_in_silhouette: Reply From: klaas

Hi,
Have you considered using this?