0 votes

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?

in Engine by (38 points)

1 Answer

0 votes
by (4,010 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.