How to get the distance between a Kinematic and a Rigid body in 3D

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

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.

It’s the same distance_to

Magso | 2020-09-25 23:42

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?

Lcs-lts | 2020-09-26 01:27

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

RedBlueCarrots | 2020-09-26 01:46

:bust_in_silhouette: Reply From: jgodfrey

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