freddy mooney
freddy mooney

Reputation: 17

How can I get the global position of a Node3D in Godot 4?

I want to position one node to another, but they are under different parent nodes. How can I set one of the node's position to the other's, without interfering with the parent nodes' positions? Btw, the node3d with the position i'm trying to find has a random position

i've tried Node3D.global_position() and Node3D.global_translate (AI told me to try it). I know how to set global position, just not find it.

Upvotes: 1

Views: 14195

Answers (1)

Theraot
Theraot

Reputation: 40285

In Godot 3 for Spatial you would do node.global_transform.origin, and this continues to work in Godot 4 for Node3D.

However, in Godot 4 you can now use node.global_position for Node3D which brings it in line with Node2D.

The official documentation for global_position says

Global position of this node. This is equivalent to global_transform.origin

Both global_transform.origin and global_position are properties, that you can get:

prints(node.global_transform.origin, node.global_position)

Or set:

node.global_transform.origin = Vector3.ZERO
node.global_position = Vector3.ZERO

Apparently you were trying to use it as a method Node3D.global_position() (since you added parenthesis, it would be trying to call it, which it can't since it is a Vector3 not a Callable).

And also Node3D.global_translate which apparently you tried to use as a property... Well, it is a method which you can use to move the Node3D.

The documentation on global_translate says:

Moves the global (world) transformation by Vector3 offset. The offset is in global coordinate system.

You would use it like this:

node.global_translate(some_offset)

Which is equivalent to:

node.global_position += some_offset

Which is equivalent to:

node.global_transform.origin += some_offset

Which is equivalent to:

node.global_transform.origin = node.global_transform.origin + some_offset

Upvotes: 3

Related Questions