0 votes

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: https://godotengine.org/qa/34102/how-to-make-ideal-circle-path2d-in-simple-way

Godot version 3.4.2
in Engine by (40 points)

1 Answer

+1 vote
Best answer

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

var center = get_viewport().get_rect().size * 0.5
by (2,159 points)
selected by

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

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.

That worked! Thank you.

Welcome! Good luck with your project!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.