|
|
|
 |
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:
- Get the player node.
- Get the hook node.
- Get the global position of the player node.
- Get the global position of the hook node.
- 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
.
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(hook.name)
.
Feel free to ask more questions and I will gladly help.
haydenv | 2022-05-01 08:28