How can I have a sprite orbit the centre of the screen and keep pointing at the same point?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By iamrifki

So I have this sprite that orbits around the centre of the screen, however it doesn’t point to the centre like I want to. How can I do that?enter image description here

The code I got for this is from here:

:bust_in_silhouette: Reply From: DaddyMonster

Godot gives you the look_at method which is exactly what you need.

var center = get_viewport().get_rect().size * 0.5

Well, it made it look straightforward, but not to the centre of the screen.

Not Quite

Here’s the following code by the way (Ship’s code is commented completely out save for the extends)

extends Node2D

var speed = 2  # rotation speed (in radians)
var radius = 300  # desired orbit radius
var screen_size  # Size of the game window.
var center

func _ready():
	$Ship.position = Vector2(radius, 0) # desired orbit radius	
	screen_size = get_viewport_rect().size
	center = screen_size * 0.5

func _process(delta):
	rotation += speed * delta

iamrifki | 2022-01-16 11:42

Godot considers a node to be facing in the positive x axis in 2d. Looks like your model is pointing towards positive y. Just rotate the sprite 90 degrees (PI/2) to correct. You could do this in code but just turning it in the editor is probably the easiest.

DaddyMonster | 2022-01-16 12:02

That worked! Thank you.

iamrifki | 2022-01-16 12:11

Welcome! Good luck with your project!

DaddyMonster | 2022-01-16 12:25