Does .distance_to work on 3D nodes?

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

Every time I try to make this variable:

var distance2opponent = self.get_global_transform().distance_to(get_node("/workspace/Player").get_global_transform().get_translation())

I get the error: Method ‘distance_to’ is not declared on base ‘Transform’.

As if it doesn’t like 3D or something. Is there a work around for this?

:bust_in_silhouette: Reply From: Magso

I don’t fully understand transform but get_global_transform() is getting the matrix for the node’s position and rotation. The error says “not declared on base ‘Transform’” because distance_to() needs to come after a Vector3 which would be Transform.origin.

I feel like this should be working, but I’m clearly not understanding something about distance_to.

Here’s what I put in for the variable after your explanation:

var distance2opponent = self.transform.origin.distance_to(get_node("/workspace/Player").transform.origin)

And although there is no errors right away, when I run the script it gives me an error saying: “Invalid get index 'transform' (on base: 'null instance')

The debugger says it now can’t find my Player node.
This code is being put on a MeshInstance called “AI”.
My “Player” is also a MeshInstance. Both of them are children under the Spatial root “workspace”.

I don’t know what I’d doing wrong.

Dumuz | 2019-11-26 19:01

I also missed this, but get_node("/workspace... doesn’t need the slash. It should be get_node("workspace...

Magso | 2019-11-26 20:47

Thank you for your help!

Dumuz | 2019-11-28 18:20