0 votes

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?

in Engine by (349 points)

1 Answer

+1 vote
Best answer

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.

by (3,219 points)
selected by

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.

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

Thank you for your help!

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 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 webmaster@godotengine.org with your username.