0 votes

so I'm learning Godot, and now I'm trying to get the distance (in pixels, meters, whatever) between the player and a rigidbody. Just like in this question, but in 3D.

I'm trying to apply_impulse to this rigidbody, but the impulse should be divided by the distance between them.

in Engine by (16 points)

It's the same distance_to

i'm trying this:

var distance = get_node(".").distance_to(body)

*"body" is because I'm comparing to the body that entered in the area**

but it throws a invalid call, NonExistent Function in base KinematicBody.

any idea?

the distance_to function only works on Vector3s, so if you try:

var distance = get_node(".").translation.distance_to(body.translation)

that should hopefully work

1 Answer

0 votes

With a sample scene tree defined like this:

   +- KinematicBody
   +- RigidBody

Something like this (connected to the Spatial node) should work:

func _ready():
    var dist = $KinematicBody.transform.origin.distance_to($RigidBody.transform.origin)

With the KinematicBody at 0,0,0 and the RigidBody at (10,10,0), that results in a distance of 14.142

by (16,712 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.