Telling a control node the position of a "world" node.

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

Working on 2d game.

I have a UI with a targeting reticle (a TextureRect node) on it’s own canvas layer. I want the reticle to move to the player avatar if the reticle is not moved for a while, so I need the reticle to know the global_position of the player avatar. I do not understand how to translate the player position to coordinates the TextureRect can understand.

I already read the documentation for Canvas Layer, Canvas Item, and Viewports but I can’t seem to make sense of it. I need someone to talk to me like I’m an idiot.

#This doesn't work at all but you can see what I'm trying.
var reticle
var playerCanvasPosition
onready var player = get_node("../../..")
func _process(delta):
	playerCanvasPosition = player.get_global_transform().affine_inverse().xform(player.global_position)
	reticle.rect_position = playerCanvasPosition

Well, I tried this and it still doesn’t line up…

var reticle
onready var player = get_node("../../../")

func _ready():
	reticle = get_node("Reticle")

func _process(delta):
	reticle.set_global_position(player.global_position, false) 

I tried with both true and false in the setter method.

TheIronGod | 2020-09-06 07:07

I thought I had it!

My second chunk of code kinda worked after I enabled Follow Viewport in the editor. The reticle went where it should. But everything else on my UI stopped moving with camera. I don’t understand WHY, though (and would really appreciate an explanation).

I don’t want to add a whole other layer just for the reticle, that seems woefully inefficient.

TheIronGod | 2020-09-06 07:56

:bust_in_silhouette: Reply From: TheIronGod

I found the answer after 2 days of googling.

I needed to put player.get_global_transform_with_canvas().origin in a variable then use reticle.set_global_position(variable).

1 Like