我能获取Godot 4.0中Node3D的全局位置吗?

huangapple go评论60阅读模式
英文:

How Can I Get The Global Position Of A Node3D In Godot 4.0?

问题

Node3D.global_position() 和 Node3D.global_translate() 是两个可以用来设置节点在全局坐标系下位置的方法。你可以尝试使用它们来实现你的目标。

英文:

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.

答案1

得分: 0

In Godot 3 对于 Spatial,您会使用 node.global_transform.origin,在 Godot 4 中对于 Node3D 仍然适用。

然而,在 Godot 4 中,您现在可以使用 node.global_position 来操作 Node3D,这使其与 Node2D 保持一致。

关于 global_position 的官方文档表示:

该节点的全局位置。这等同于 global_transform.origin

global_transform.originglobal_position 都是属性,您可以获取:

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

或者设置:

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

显然,您尝试将其用作方法 Node3D.global_position()(因为您添加了括号,它会尝试调用它,但实际上它不是可调用的,因为它是 Vector3 而不是 Callable)。

还有 Node3D.global_translate,显然您尝试将其用作属性...它实际上是一个方法,您可以使用它来移动 Node3D

关于 global_translate 的文档表示:

使用 Vector3 偏移移动全局(世界)变换。偏移量是在全局坐标系中的。

您可以像这样使用它:

node.global_translate(some_offset)

这相当于:

node.global_position += some_offset

这相当于:

node.global_transform.origin += some_offset

这相当于:

node.global_transform.origin = node.global_transform.origin + some_offset
英文:

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

huangapple
  • 本文由 发表于 2023年5月21日 06:08:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297531.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定