How to make a sprite stop rotating after rotating 360 degrees using the rotate() function

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

I am trying to do a 360-degree turn with my character. I use the rotate function, but it does not seem to stop rotating when it hits 360 degrees

This is my code:
extends Sprite

func _physics_process(delta: float) → void:
if rotation_degrees == 360:
rotate(2 * delta)

Thank you so much for your help

:bust_in_silhouette: Reply From: zenbobilly

It’s best not to compare against an absolute value with floating point in this sort of scenario because your increment may have gone beyond it. Use something like:

if (rotation_degrees >= 360.0):
rotation_degrees -= 360.0

Or alternately:

rotation_degrees = fmod (rotation_degrees + whatever value, 360)

The code you are using is never going to stop rotation because the rotate () function uses the current rotation, therefore, the code that says rotate (0) is doing nothing. If you want absolute rotation, then it is better to use rotation_degrees (or rotation) rather than the rotate () function, eg.

rotation_degrees = fmod (rotation_degrees + 2.0 * delta, 360)