While not a perfect example, here is the code that will rotate the cube by 90 degrees when the keyboard arrows are pressed:
extends Spatial
const GRID_SIZE = 1;
const MOVE_SPEED = 1;
var tween_node;
var tween_moving = false;
func _ready():
tween_node = get_node("Tween");
tween_node.connect("tween_completed", self, "_on_tween_completed");
tween_moving = false;
func _process(_delta):
if (tween_moving == false):
if (Input.is_action_pressed("ui_up")):
tween_node.interpolate_property(self, "translation",
translation, translation + (Vector3.FORWARD * GRID_SIZE),
MOVE_SPEED, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT);
tween_node.interpolate_property(self, "rotation",
rotation, rotation + Vector3(deg2rad(-90), 0, 0),
MOVE_SPEED, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT);
tween_node.start();
tween_moving = true;
elif (Input.is_action_pressed("ui_down")):
tween_node.interpolate_property(self, "translation",
translation, translation + (Vector3.BACK * GRID_SIZE),
MOVE_SPEED, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT);
tween_node.interpolate_property(self, "rotation",
rotation, rotation + Vector3(deg2rad(90), 0, 0),
MOVE_SPEED, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT);
tween_node.start();
tween_moving = true;
elif (Input.is_action_pressed("ui_right")):
tween_node.interpolate_property(self, "translation",
translation, translation + (Vector3.RIGHT * GRID_SIZE),
MOVE_SPEED, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT);
tween_node.interpolate_property(self, "rotation",
rotation, rotation + Vector3(0, 0, deg2rad(-90)),
MOVE_SPEED, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT);
elif (Input.is_action_pressed("ui_left")):
tween_node.interpolate_property(self, "translation",
translation, translation + (Vector3.LEFT * GRID_SIZE),
MOVE_SPEED, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT);
tween_node.interpolate_property(self, "rotation",
rotation, rotation + Vector3(0, 0, deg2rad(90)),
MOVE_SPEED, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT);
tween_node.start();
tween_moving = true;
func _on_tween_completed(object, property_nodepath):
if (property_nodepath == ":translation" and object == self):
tween_moving = false;
The code above doesn't work properly when the rotation is non uniform. When the rotation is uniform, it works as expected though.
Ideally you'd want to use interpolate_method
instead of interpolate_property
and interpolate the rotate_x
/rotate_y
/rotate_z
functions instead of changing the rotation
property. I wasn't able to get the interpolate_method
function working with any of the rotate functions though, but I didn't send too much time playing with it.
Here is a link to the Godot project on my Google Drive. Hopefully this give you an idea of how to do it. :smile:
(Side note: Welcome to the forums!)