+1 vote

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!

Godot version Godot v3.4.4
in Engine by (101 points)

3 Answers

0 votes

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

https://docs.godotengine.org/en/stable/classes/class_vector3.html?highlight=Vector3#class-vector3-method-distance-to

by (19,164 points)

(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.
Thanks

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.

+1 vote

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:

Spatial
Spatial/Ground
Spatial/Wall
Spatial/Wall/Hook
Spatial/Player

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.

by (180 points)
edited by

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.

https://imgur.com/a/buffRaI
The scene tree
also, I am working in 2d.
But other than that Thanks

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"?

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

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.

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

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(hook.name).

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

0 votes

From the Godot documentation:

Vector2
float distance_to ( Vector2 to )

Returns the distance between this vector and to.

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