Attention | Topic was automatically imported from the old Question2Answer platform. | |
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?