How do i get the location of a different object

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

How do i get the location of a different object. For example, when i press a key i want an object to move to the location of another object. Can anyone help me out?

:bust_in_silhouette: Reply From: jgodfrey

You need a reference to the other object. Assuming you have that stored in a variable named other_object, you can get its location via other_object.position.

what do i assign the variable other_object to?

James122333 | 2020-11-26 17:07

If the object you want was created in the GUI you can set other_object using it’s node path, for example

other_object = $a_child_object
other_object = $"../a_peer_object"
other_object = $/root/World/somewhere_else/in_the_node_tree/object

etc

Or, if other_object was created in a script you would create it and store a reference to the new object, something like this

other_object = BadGuy.instance()

Or you can find a node using methods such as get_child or Groups etc

This is one of the most important (and potentially confusing) things you need to understand to get started with Godot - you will use this ALL the time when writing in Godot. I recommend you take a bit of time to watch some of the tutorials about this, for example this one is nice

AndyCampbell | 2020-11-28 01:36