fguillen
fguillen

Reputation: 38832

How to position Node2D in screen coordinates?

I want to position my Node2D in Screen coordinates x=100, y=200 (for example), having into consideration that the Camera may have moved. And I am surprised I don't know how to do it :)

I found plenty of documentation on how to get the Node2D coordinates in Screen coordinates:

my_node_2d.get_global_transform_with_canvas()

But this doesn't exists:

my_node_2d.set_global_transform_with_canvas(Vector2)

I may be missing something very obvious because I don't find any documentation/tutorial/QA about this.

Update: I can not use CanvasLayer because my Node2D is the Player and it has to exist in the World. The Scroll is moving and the player has to move with it

Upvotes: 1

Views: 943

Answers (1)

fguillen
fguillen

Reputation: 38832

This is the solution I have found:

func set_on_screen_position(node: Node2D, screen_position:Vector2):
    var canvas_position = node.get_canvas_transform().origin
    node.global_position = screen_position - canvas_position

But I am not sure if it will mess up things with Canvas scale and such

Upvotes: 0

Related Questions