Player moves when bullet is created

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

I have a player that shoots a bullet.

Player script:

func shoot_bullet():
     readyToShoot = false

     # Create bullet
     var bullet = Bullet.instance()

     # Set bullet position to player's position
     #bullet.get_node('BulletRB').position = Vector2(500,100) # works
     bullet.get_node('BulletRB').set_position(self.position) # Makes player move too


     # Add bullet to scene
     self.add_child(bullet)

Bullet script:

func _ready():

     # Set to process this node
     self.set_process(true)

     # Set bullet speed
     self.set_axis_velocity(Vector2(-600,0))

     # Diga. note that bullet has been shot
     print('Shot bullet')

For some reason the bullet, when I get the position of the player to put the bullet at the player’s position makes the player move too. If I set a Vector2 point myself as shown above (commented out) then everything works fine. So it appears it is something with my positioning code. Maybe I’m ‘linking’ the two variables to have the same position or something.

How can I fix this?

:bust_in_silhouette: Reply From: eons

With self.add(bullet) you are adding the bullet as a child of the player, that means it will be affected by the parent transform (the player).

Your bullet should be a child of something else, can be added to the player parent or create a special node in the scene to hold all the bullets (for organization).

Scene
|-Player
|-Bullet

Another way is to clear the parent transform by adding as a child of a Node (plain node), child of the player.

Scene
|-Player
    |-BulletContainer(Node)
        |-Bullet

I did as you said, created a Node in my scene just for bullets.

func shoot_bullet():
   readyToShoot = false

   # Create bullet
   var bullet = Bullet.instance()

   # Set bullet position to player's position
   #bullet.get_node('BulletRB').position = Vector2(500,100) # works
   bullet.get_node('BulletRB').set_position(self.position) # Makes player move too

   # Add bullet to scene
   get_node('/root/SceneRoot/Bullets').add_child(bullet)

My player is at ‘/root/SceneRoot/Player’. I am still having the same effect.

Also, just to note. On the main question, when I used ‘self.add_child()’ but put the bullet position to a Vector2, everything worked correctly, cept’ the bullets weren’t originating from the right spot, obviously.

tproper | 2018-05-09 18:07

I think I misunderstood the issue, I have thought your bullets were moved by the player, not the other way around.

Still, bullets should be added on another node.

If bullets move the player is because they collide each other (assuming these are bodies), one way is to properly set up the collision layers and masks so bullets and players do not hit each other (easily done in the inspector).
Another way is to add a collision exception with add_collision_exception_with to player or bullet.

eons | 2018-05-09 20:41