smooth 2d rotation

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

Hi there!

In my game, I need to rotate the player by 90 degrees or which I have working but it just snaps to 90 degrees, which works but doesn’t look good, and I couldn’t find any videos or anything in a syntactic so I was wondering how I would do that.

If you can help big thanks ahead of time!

:bust_in_silhouette: Reply From: BardiaAB87

You could add a small number to your rotation_degrees.
For example if you want the player to rotate from 0 to 90 degrees
you can say:

func _physics_process(delta):
    if rotation_degrees < 90:
	    rotation_degrees += 0.5

Hi, thanks for the reply!
does this mean there’s only a “hacky” way of doing it, no method to do it in a nicer/efficient way of doing it? If there’s not that’s fine and thanks for the help!

Tentamens | 2022-08-04 18:27

You could make the smoothing (less “hacky”, I suppose) by defining a rotation speed angularVelocity, and then apply the speed with the delta (e.g., rotation_degress += delta * angularVelocity, so even if your game lags the rotation will appear smooth.

If your game requires the player’s sprite rotation to determine hitboxes (shooting) or what not, you could apply the solution proposed by BardlaAB87 to achieve visual smoothness, but keep track of a seperate variable (e.g. actualSpriteRotation), so mechanically the player would instantly snap 90 degrees (you would do actualSpriteRotation += 90), allowing for immediate accurate actions based on sprite rotation angle, but the sprite would be visually catching up by rotating the sprite towards the actualSpriteRotation angle

godot_dev_ | 2022-08-04 18:38