find distance between two points

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

I am trying to add a feature to my game where if your player is within x distance from the “hook” you can grapple to it. but how do I find the distance from the player to the “hook”
Thank you!

:bust_in_silhouette: Reply From: Necco

From the Godot documentation:

float distance_to ( Vector2 to )

Returns the distance between this vector and to.

:bust_in_silhouette: Reply From: jgodfrey

You don’t say whether you’re working in 2D or 3D, but the Vector2 and Vector3 objects have a distance_to method, which is what you want.

Here’s the 3D docs…

(Apologies for the dumber question) but how would I get the position of an object from a different scene tree like the hook for instance. and use that position to compare it against the player using the distance_to method?
note this is in 2d apologies for not clarifying that in the previous question.

Tentamens | 2022-04-30 04:25

Not really sure what you mean by “object from a different scene tree” but the node’s global_position might be what you’re looking for.

SweetPie | 2022-04-30 05:31

:bust_in_silhouette: Reply From: haydenv

Exactly how to do this will depend on which script you’re calculating the distance in, and also what your scene tree is like. But the plan is:

  1. Get the player node.
  2. Get the hook node.
  3. Get the global position of the player node.
  4. Get the global position of the hook node.
  5. The positions are either Vector2’s or Vector3’s. Use the distance_to function to calculate the distance.

For example, suppose your scene tree is as follows:


And suppose that you want your distance calculation to be in the script attached to the player node. The code snippet below carries out steps 1-5. Note that since the code is in the script on the player node, then we can skip step 1.

var hook = get_node("../Wall/Hook")
var player_position = global_transform.origin
var hook_position = hook.global_transform.origin
var d = player_position.distance_to(hook_position)

A word of caution:

If you re-organize the scene tree, then you might need to change the path you’re passing to get_node.

How might I get a node from another scene?
using the get_node function but I can’t seem to get the path to said node.

Imgur: The magic of the Internet
The scene tree
also, I am working in 2d.
But other than that Thanks

Tentamens | 2022-04-30 15:12

Ah, yes. Which of those nodes has the script with the distance checking code? Also, are you wanting to find the distance from the node named “player” to the node named “Grapple hook”? Or are you wanting to find the distance from the node named “player” to a child of the node named “Grapple hook”?

haydenv | 2022-04-30 16:30

The distance checking code will be in the “Player” node, and yes I am trying to get the distance between the “Player” and the “Grapple hook”.
Thanks again

Tentamens | 2022-04-30 17:08

You’re welcome.

Try get_node(“…/Grapple hook”).

Notice the …/ that’s used in the get_node function. Using “…” means “get the parent”, so in this case “…” will be “Main”. Then since grapple hook is a child of Main the “/Grapple hook” means “get the child of Main named Grapple hook”.

I hope this helps you get access to the grapple hook node from your player script.

haydenv | 2022-04-30 17:25

forgive me for all the questions.
In your first reply, you use things like global_transform along with .origin
Godot doesn’t want me to use global_transform and if I manually type it out and run the code it passes me an error Invalid get index 'get_transform' (on base: 'Nil') along with if I try and use .origin it instantly gives me an error saying that it is not declared In Vector2. I have tried using get_origin. But I still run into issues with the global_transform
Thanks again this should be the last question

Tentamens | 2022-04-30 21:28

No problem, glad to help.

Firstly, I just fixed a mistake in my answer where I wrote global_transform.origin() when it was supposed to be global_transform.origin.

There is a more direct way of doing that in 2D, which is to use the global_position property.

So you could try running this code in your player script.

var hook = get_node("../Grapple_hook")
var player_position = global_position
var hook_position = hook.global_position
var d = player_position.distance_to(hook_position)

I think the errors you are getting are because there was a mistake when using get_node, since it says on base: 'Nil'. You can test whether you got the node by running print(

Feel free to ask more questions and I will gladly help.

haydenv | 2022-05-01 08:28