+3 votes

I'm making a node point towards the mouse angle, and I'm using lerp to ease the rotation of the node. But when the mouse angle changes from a value above 179, the node rotates all the way around instead of just adding + 1 on the node's angle. Is there a built in function to ease rotations without this happening?

in Engine by (35 points)
reshown by

2 Answers

0 votes

I had the exact same issue, and could only solve it with some if statements testing for crossing -PI and +PI (using radians)
I basically ask: if the change in angle is more the 180 (PI) then go the other way around.

Two things I've noticed:
if you use radians, the engine doesn't seem to mind if values go far above or below PI, it still rotates correctly. In which case you might start by setting all the rotations simply far above PI so they always stay positive. Then you can add or subtract the change relative to your mouse.

Good luck

by (68 points)

Still struggling with this:

if the change in angle is more the 180 (PI) then go the other way
around.

How exactly are you telling it to go other way around?

+1 vote

Old question, but I'll leave it here for anyone dealing with this problem.

I don't know how to explain it in a formal way, with trigonometry and all that, but you can take a look here if want to understand it: https://tauday.com/tau-manifesto

When you are in the inspector, you can see that any rotation "resets" when crossing the 0/180 degrees threshold. When you are rotating something by code and want to avoid weird behaviours due to this angles, you can use the lerp_angle function:

https://docs.godotengine.org/en/stable/classes/class_@gdscript.html#class-gdscript-method-lerp-angle

by (120 points)

Hi there, I am still dealing with this problem lol....

I have a boss I want to aim at the player... follows him around etc... I have the same rotation reset where in a point it goes all the way the other way instead of +1 to the side...

here my code: the 1.57 is a correction coz my player sprite is rotated

func _aim_at_player():
    if stage.has_node("Player"):
        var player = stage.get_node("Player")
        var aim_dir = (player.position - self.position).angle() - 1.57
        var current = self.rotation
        $Tween.interpolate_property(self,"rotation",current,aim_dir,1,Tween.TRANS_LINEAR,Tween.EASE_OUT)
        $Tween.start()
    else: pass

Actually I just fixed it by removing the tween and replacing with:

self.rotation = lerp_angle(current,aim_dir,0.02)

I'll just leave this here for future reference I guess ;)

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.