Tween: Looping rotation (2D)

Godot Version

4.3 rc3

Question

How do i make a 2D object spin forever with a tween?

i thought this code would work:

func spin(rotation_length_s: float):
	var tween = create_tween().set_loops()
	tween.tween_property(sprite, "rotation", 360, rotation_length_s)

It does not. If i call spin(1) it spins really fast for 1 second then stops. If i call spin(3) it spins for 3 seconds then stops. It looks like the 4th argument to tween_property doesn’t control the time it takes to reach the final state, it controls how long the tweening loops. Which is not what the docs say.

i have a similar method for scale that works as expected so i’m really confused.

The tween hits it’s target rotation, (should be TAU for your example) then the next “loop” the sprite is already at the target rotation. Use .from to specify the starting rotation

func spin(rotation_length_s: float):
	var tween = create_tween().set_loops()
	tween.tween_property(sprite, "rotation", TAU, rotation_length_s).from(0)

so it was looping but to the same position it already achieved.

Am I right to assume you just want the tween to be repeating forever once it ends?

func spin(rotation_length_s: float):
	var tween = create_tween()
	tween.tween_property(sprite, "rotation", 360, rotation_length_s)
	tween.finished.connect(func(): spin(rotation_length_s))

Also if you want to use .set_loops() then you need to also set a value because by default it’s .set_loops(0) as per documentation.

1 Like

Someone is up late. It’s after 1am Texas time.

i spent an hour debugging and reading docs and Google posts. i was never going to guess TAU. Although the key to infinite loops was .from(0) which maybe i should have figured out. i was getting hung up on how the animation appeared to last as long as the duration no matter how small or large i set it.

But yeah, your code works. That is the solution.

1 Like

Calling this method without arguments will make the Tween run infinitely, until either it is killed with kill, the Tween’s bound node is freed, or all the animated objects have been freed (which makes further animation impossible).

set_loops(0) or set_loops() will make it run forever. set_loops(1) is the normal run-once behaviour, the default parameter assumes one will only call set_loops when one wants the tween to loop.

1 Like