Godot Version
4.5.1
Question
I am trying to make a rotating star for an animation, but when I run this code, it does not work.
extends Sprite2D
func _ready():
var tween = create_tween()
tween.tween_property($Twinkle, “rotation_degrees”, 360, 1).set_delay(0.1)
What could be causing this code to do nothing? There are no errors in the console so I am not sure.
360 and zero are the same so it’s likely not moving. Try 180 degrees and see what happens. Then go from there.
2 Likes
What’s the initial rotation? Print it out before starting the tween.
I tried 90 degrees and it still did not work
0 and 360 are not the same. Tween doesn’t know or care that it looks the same to us. It operates only on numbers and 0 and 360 are not the same number. As I said, print the initial value and print the actual value every frame in _process()
An obvious reason for this code to do nothing would be that it doesn’t run at all.
In any case, print is your best friend here.
After a little bit I found why it wasn’t working. I put the script from a node into a sprite2D and forgot that the sprite didn’t have the “twinkle” thing in it. I just moved it to a node and made the original sprite the twinkle and it worked
That would have displayed some errors, wouldn’t it?
I would have thought so but the debugger was empty. I’m pretty sure the code just didn’t run
Very unlikely but if it really happened that way it should be reported as an engine bug.
What should I call the bug? Like the title of the issue
“Engine not reporting tween error when it should”
Ok, maybe it works now, but that was an issue I encountered in the past with tweening a spinning throwing dagger. I ended up using an AnimationPlayer to solve the problem.
If animation player can do it, tween should be able to do it as well. Afaik rotation properties are not automatically clamped to 2pi range. Don’t remember how Godot 3 behaved in this respect.
If you have code that spins something 360 degrees over and over again I’d love to see it. This is an ongoing problem for me in a game I am working on. Here’s my code that didn’t work:
## Spin dagger
func spin(spin_direction: Vector2, full_rotation_speed: float) -> void:
spin_direction = spin_direction.normalized()
var tween: Tween = create_tween()
tween.tween_property(self, "rotation_degrees", 360 * spin_direction.x, full_rotation_speed)
timer.start(full_rotation_speed)
await timer.timeout
rotation = 0.0
spin(spin_direction, full_rotation_speed)
This is what I’m currently doing because it works:
func throw_animation(spin_direction: Vector2) -> void:
spin_direction = spin_direction.normalized()
if spin_direction.x == 1.0:
animation_player.play("throw_right")
else:
offset *= -1
animation_player.play("throw_left")
This code is from a Godot 4.4.1 project.
func spin_endless() -> void:
var t: Tween = create_tween()
t.tween_property(self, "rotation", TAU, 1.0).as_relative()
t.set_loops()
Note that angle doesn’t have to be 2pi here. It can actually be anything.
1 Like
Thank you. I’ll give it a shot.
1 Like
If we want to get really nitpicky, the above example is not endless. It will eventually go out of whack if we leave it run for months. Rotation will become a very large number and floating point precision will start causing issues. Nevertheless, it’s good enough for most use cases.
Here’s an alternative that’s truly endless and could go on until the collapse of the universe 
func spin_endless_for_realz_now() -> void:
while true:
rotation = wrapf(rotation, 0.0, TAU)
var t: Tween = create_tween()
t.tween_property(self, "rotation", rotation + TAU, 1.0)
await t.finished
1 Like