Godot Version
4.4
Question
Hello everyone! I apologize for the confusing title but I am trying to make a gun rotate around my player, and set flip_v
to true when the rotation exceeds a certain point so it doesn’t look weird. My attempt is this: if rotation_degrees == 270 or rotation_degrees == 90: flip_v = !flip_v
Now that doesn’t work because rotation_degrees
isn’t the actual rotation of the gun. I tried printing it to test it out and got results like these:
-30.123048193019
210.144104003906
220.723495483398
232.964645385742
251.924835205078
280.244079589844
312.375823974609
350.624145507813
366.963470458984
Please tell me how to get the actual rotation degrees. Forgive me if my question is poorly structured, it’s my first time here and in Godot too.
What do you mean by “actual” rotation degrees?
You are comparing exact number values, rotation_degress
will so rarely exactly equal 90 or 270, you must change your comparison if you want it to ever be true.
If you want to rotate and flip the object maybe this code will do you better
flip_v = absf(rotation_degrees) > 90
I’m sorry for not being clear. I thought rotation degrees range from 0 - 360 so the results i was getting are unexpected.
The code does its job a single time then stops working. I guess because the degrees aren’t limited between 0 and 360 like I said.
Ah, yes the stored rotation value can go past those effective limits. If you mathematically impose a 180 degree with fmod
then that might clean up that sample code
flip_v = absf(fmod(rotation_degrees, 180)) > 90
This takes rotation, fmod
wraps the rotation around 180 so it can be -180 to 180. absf
makes all values positive so it’s 0 to 180.
3 Likes
This is a well known issue and it happens because rotation is done in radians and then a conversion made (at least that is how I remember it when I had this exact issue).
Switch to using radians or store your integer degrees in an array and iterate that.
Or gertkeno’s formula.
2 Likes
This works well. Before this I tried using % instead of fmod but it didn’t work so I just completely discarded that idea lol. It’s weird Godot doesn’t do this automatically. Thank you both! : D
I didn’t find myself understanding it a lot so I kinda did my own way:
var deg = fmod(rotation_degrees, 360)
if deg < 0: deg += 360
if deg > 90 and deg < 270: flip_v = true
else: flip_v = false
Yours is cleaner though.