Odd positioning

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By rainbowjelly

Hi, new user starting to get to grips with Godot, having a positioning issue with a child object, I have the following objects setup in the scene like so:
Kinematic2D(Player) > Position2D(PivotPointForGun) > Position2D(Muzzle).

So I instantiate a bullet instance when trigger is fired and it’s position is set with these lines:

func shoot():
var b = Bullet.instance()
add_child(b)
b.transform = $PivotPointForGun/Muzzle.global_transform

The PivotPointForGun is just an object setup as a pivot point for the gun sprite to pivot around when rotating - the Muzzle object rotates in correct position and the script works as expected while the Player object is static, but as soon as Player’s position moves the position of Muzzle doesn’t stay in position relative to it’s parent and the bullet instance becomes misaligned with the graphic.

Anyone come across this before, I tried adding a sprite child in the same child/parent relationship for a visual feedback but this rotates and follows its parent as expected so I would expect the bullet to instantiate at the same position.

Thanks.

your usingb.transform = $PivotPointForGun/Muzzle.global_transform but that its local going to global to do this
b.global_transform.origin = $PivotPointForGun/Muzzle.global_transform.origin

origin is just its x,y,z without anything else

lewis glasgow | 2021-08-12 09:25

Thanks lewis glasgow, that worked a treat.

Just for completeness and anyone having same issue I also then set the rotation in a similar way:
b.global_rotation = $PivotPointForGun/Muzzle.global_rotation

rainbowjelly | 2021-08-12 20:21