0 votes

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?

in Engine by (59 points)

1 Answer

+1 vote

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.

by (10,750 points)

what do i assign the variable other_object to?

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

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 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 webmaster@godotengine.org with your username.

Categories