• 2D
  • Rotation of objects.

Hello friends! :) How do you rotate an object so that when it reaches a certain degree of rotation, it rotates inversely? For example, I make a sprite rotate from 60 to 0 degrees, I want it to go back to 60 when it reaches 0 degrees.

Use an AnimationPlayer node and simply set the keys to 60, 0, 60.

Here's a possible solution using a Tween node:

extends Node2D

export (float) var start_rotation = 0;
export (float) var end_rotation = 0;
export (float) var rotation_time = 2.0;
var at_end_rotation = false;
var tween : Tween = null;

func _ready():
	# Making a tween node
	tween = Tween.new();
	add_child(tween);
	
	# connect the tween finished signal
	tween.connect("tween_all_completed", self, "on_tween_all_completed")

	# setting the rotation to the start rotation
	global_rotation = start_rotation;
	at_end_rotation = false;
	# starting the tween
	tween.interpolate_property(self, "global_rotation", start_rotation, end_rotation, rotation_time);
	tween.start();

func on_tween_all_completed():
	# based on the value of at_end_rotation, rotate either to the end or to the start
	if (at_end_rotation == false):
		at_end_rotation = true;
		tween.interpolate_property(self, "global_rotation", end_rotation, start_rotation, rotation_time);
		tween.start();
	else:
		at_end_rotation = false;
		tween.interpolate_property(self, "global_rotation", start_rotation, end_rotation, rotation_time);
		tween.start();