Bullets move with player

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

I have an issue where when I move my player character, any bullets that it fires also move with it.

I have a separate scene which is my main scene, and have imported my main charecter as an instance into that scene

Code below:
extends KinematicBody2D

export (int) var speed = 200
var velocity = Vector2()
var main = get_tree().get_root()

export(PackedScene) var Bullet = preload(“res://Bulletthin.tscn”)

func shoot():
var b = Bullet.instance()
b.transform = $Muzzle.transform

func get_input():
velocity = Vector2()
if Input.is_action_pressed(‘right’):
velocity.x += 1
rotation_degrees = 90
if Input.is_action_pressed(‘left’):
velocity.x -= 1
rotation_degrees = -90
if Input.is_action_pressed(‘down’):
velocity.y += 1
rotation_degrees = 180
if Input.is_action_pressed(‘up’):
velocity.y -= 1
rotation_degrees = 360
velocity = velocity.normalized() * speed

func _physics_process(delta):
velocity = move_and_slide(velocity)
if Input.is_action_pressed(“fire”):

:bust_in_silhouette: Reply From: jgodfrey

Assuming the above script is attached to the player, your problem is here:


That’ll add the bullet as a child of the player, so the bullets will naturally move with the player.

To fix it, you need to add the bullet as a child of something outside of your player node’s hierarchy.

That could be as simple as this:


That’ll add the bullet as a child of the parent of the player (so, a sibling of the player). According to your above-described scene layout, it’ll be added as a child of the main scene. That should fix the problem.

Well, I did that, and all I needed to go was put
b.transform = $Muzzle.golbal_transform
instead of
b.transform = $Muzzle.transform

and it worked! Thank you!

DoGot | 2020-12-16 18:04

1 Like