英文:
Godot 4.1 global_position is offset. Why?
问题
If i use the player position to position the laser I get the laser in the origin of the player like I want it to be. Unfortunately I need the players global_position for positioning the laser. But everytime I try this it gets offset to the right and down. Why does this happen.
如果我使用玩家的位置来定位激光,我可以将激光放在玩家的原点,这正是我想要的。不过,我需要使用玩家的全局位置来定位激光。但是每次我尝试这样做时,它会向右和向下偏移。为什么会这样呢?
I have setup a demo image of some code and the result. Both lasers should be at the same spot but the second which uses global_position is not at the origin of the player.
我设置了一些代码和结果的演示图像。两个激光应该在同一位置,但是使用全局位置的第二个激光不在玩家的原点。
I tried to find the root cause but I didn't get it.
我尝试找到根本原因,但没有找到。
Both lasers should overlap in my understanding.
在我的理解中,这两个激光应该重叠。
update:
Now I tried this: "laser2.global_position = $Player.global_position" but it also did not work. I get the same result as with "laser2.position = $Player.global_position".
更新:
现在我尝试了这个:“laser2.global_position = $Player.global_position”,但它也没有起作用。我得到的结果与“laser2.position = $Player.global_position”相同。
The reason why I use global_position is because I want to spawn the laser at the top of the gun with a Marker2D. I will get the position to spawn via Marker2D.global_position in the player node. Then I spawn the laser in the Level via the position = global_position of Marker2D.
我之所以使用global_position,是因为我想要在枪的顶部使用Marker2D生成激光。我将通过玩家节点中的Marker2D.global_position获取生成位置。然后,我通过Marker2D的position = global_position在Level中生成激光。
Like in this tutorial https://youtu.be/nAh_Kx5Zh5Q?t=11008
就像在这个教程中所示 https://youtu.be/nAh_Kx5Zh5Q?t=11008
英文:
If i use the player position to position the laser I get the laser in the origin of the player like I want it to be. Unfortunately I need the players global_position for positioning the laser. But everytime I try this it gets offset to the right and down. Why does this happen.
I have setup a demo image of some code and the result. Both lasers should be at the same spot but the second which uses global_position is not at the origin of the player.
I tried to find the root cause but I didn't get it.
Both lasers should overlap in my understanding.
update:
Now I tried this: "laser2.global_position = $Player.global_position" but it also did not work. I get the same result as with "laser2.position = $Player.global_position".
The reason why I use global_position is because I want to spawn the laser at the top of the gun with a Marker2D. I will get the position to spawn via Marker2D.global_position in the player node. Then I spawn the laser in the Level via the position = global_position of Marker2D.
Like in this tutorial https://youtu.be/nAh_Kx5Zh5Q?t=11008
答案1
得分: 0
不确定百分之百,但也许激光的起源是您在玩家周围绘制的碰撞形状的第一个点的位置?
英文:
not 100% certain but maybe the origin of the laser is the position of the first point of the collision shape you drew around the player?
答案2
得分: 0
以下是翻译好的部分:
在链接图像中的代码说:
laser.position = $Player.position
add_child(laser)
在这种情况下,该代码在$Player
的父级中运行(因为您可以使用此语法获取它)和laser
中(因为您将其添加为子级)。
因此,$Player
和laser
具有相同的本地坐标(它们都相对于其父级具有相同的坐标,因为它们具有相同的父级)。
因此,在这里使用position
是可以的。
链接图像中的代码还说:
laser2.position = $Player.global_position
在这里,您正在设置laser2
的位置相对于其父级,使其等于$Player
相对于世界原点的位置。这些不是相同的原点,我们不能保证这将起作用。
如果这不起作用,那是因为父级(附有Script
的Node
)不在世界原点(或者已应用某种缩放或其他变换)。
取而代之,使用全局坐标是安全的,就像这样:
laser2.global_position = $Player.global_position
除非您在添加新的Node
之后执行此操作:
add_child(laser2)
laser2.global_position = $Player.global_position
另一种选择是使用to_local
将global_position
转换为本地坐标:
laser2.position = to_local($Player.global_position)
add_child(child_sprite)
在您的情况下,您不需要这样做,因为如开始所解释的,本地坐标匹配(因此您可以直接设置position
)。但是,在与您当前情况不同的其他情况下,这可能会有用。
英文:
The code in the linked image says:
laser.position = $Player.position
add_child(laser)
In this case, the code is running in both the parent of $Player
(since you can get it with this syntax) and laser
(since you are adding it as a child).
So $Player
and laser
have the same local coordinates (they both have the same coordinates respect to their parent because they have the same parent).
Therefore using position
here is OK.
The code in the linked image also says:
laser2.position = $Player.global_position
Here you are setting the position of laser2
relative to its parent to be equal to the position of $Player
relative to the world origin. Those are not the same origin, and we cannot trust this will work.
If this does not work it is because the parent (the Node
that has the Script
attached) is not in the world origin (or has some scaling or other transformation applied).
Instead it is safe to work with global coordinates, like this:
laser2.global_position = $Player.global_position
Except you would have to do it after adding the new Node
:
add_child(laser2)
laser2.global_position = $Player.global_position
Another option is to convert the global_position
to local coordinates with to_local
:
laser2.position = to_local($Player.global_position)
add_child(child_sprite)
In your case, you do not need to do this, since as explained at the start the local coordinates match (so you can directly set position
). However, in other situations different from your current one, this might be useful.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论