Rotation of Sprite whilst moving

Godot Version 4.2.1

Question

Hi

Im new to godot and I’m struggling to rotate my sprite whilst moving it. I want the sprite to simply rotate on its own centre as it moves to its position.

The sprite starts at x550 y710

This is my code:

extends Node2D

var speed = 9

Called when the node enters the scene tree for the first time.

func _ready():
pass # Replace with function body.

Called every frame. ‘delta’ is the elapsed time since the previous frame.

func _process(delta):
$Sprite2D.position = $Sprite2D.position.move_toward(Vector2(550,350), speed)
if position.y != 350:
speed -= 0.1
rotation_degrees +=1

Currently the rotation seems to be centred on x0 y0.

Any help gratefully received.

I’m not very familiar with GDScript but as far as I can tell, you don’t have a proper reference to the Sprite2D node here. What you can do is under the “extends Node2D”, you can write the following code:

@onready var sprite : Sprite2D = get_node("Sprite2D")
This will get a reference to the Sprite node in your code, and you can use it to modify it’s properties.

Then, in your code, you need to reference sprite like so:

sprite.position = sprite.position.move_toward(Vector2(550,350), speed)
	if (sprite.position.y != 350):
		sprite.speed -= 0.1
		sprite.rotation_degrees +=1