0

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.

enter image description here

enter image description here

enter image description here

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

04M04
  • 3
  • 3

2 Answers2

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?

:-)

mourad
  • 667
  • 4
  • 11
0

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.

Theraot
  • 31,890
  • 5
  • 57
  • 86
  • 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". – 04M04 Jul 13 '23 at 10:45