The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

+2 votes

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
in Engine by (123 points)

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.

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.

1 Answer

+5 votes
Best answer

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).

by (123 points)
edited by
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.