0 votes

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 physicsprocess(delta: float) -> void:
if rotation_degrees == 360:
rotate(2 * delta)

Thank you so much for your help

Godot version v3.4.4
in Engine by (33 points)

1 Answer

0 votes

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 (rotationdegrees >= 360.0):
degrees -= 360.0

Or alternately:

rotationdegrees = fmod (rotationdegrees + 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.

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

by (141 points)
edited by
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 Frequently asked questions and 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 [email protected] with your username.