Vertical ShootemUp Projectile retains X Position of the Player

Godot Version

4.7.2

Question

I’m trying to learn how to perform basic functions in the Godot 2D space, and I have followed a guide online on firing a projectile. The problem I have is that the projectile will move side to side with the player moving side to side, rather than just moving up the Y axis at the X position it spawned at.

I’ve tried a few guides online, such as putting owner. before add_child. I tried to see if there is a signal to export the function to main scene, or if I can just put the function in main to see if that would fix it, to no avail.

In short, I’m hoping to have the player’s bullet to move up the screen, and to start from when the “shoot” button has been pressed. I’ll fix other issues like fire rate later. Below is the player script and projectile script.

Thanks in advance.

#####Player Script######################################################

extends Area2D

@export var speed = 200
@onready var bullet = preload(“res://projectile.tscn”)
var screen_size

Called when the node enters the scene tree for the first time.

func _ready() → void:
screen_size = get_viewport_rect().size

Called every frame. ‘delta’ is the elapsed time since the previous frame.

func _process(delta: float):
var velocity = Vector2.ZERO
if Input.is_action_pressed(“move_left”):
velocity.x -= 1
if Input.is_action_pressed(“move_right”):
velocity.x += 1
if Input.is_action_pressed(“shoot”):
pass

if velocity.length() > 0:
	velocity = velocity.normalized() * speed
	$AnimatedSprite2D.play()
else:
	$AnimatedSprite2D.stop()

position += velocity * delta
position = position.clamp(Vector2.ZERO, screen_size)

func start(pos):
position = pos
show()
$CollisionShape2D.disabled = false

func _physics_process(delta: float) → void:
if Input.is_action_pressed(“shoot”):
var bullet_temp = bullet.instantiate()
add_child(bullet_temp)

#####Projectile Script##################################################

extends Area2D

var direction = Vector2.ZERO
var speed = 300

func _process(delta):
	position.y -= speed * delta

Don’t parent the bullet to the player. Instead, parent it to some node that’s above the parent. Alternatively you can parent it to player and set its top_level flag so it doesn’t inherit parent’s transforms.

Like normalized said, the bullet is a child of the player, so it inherits every move the player makes. You want it added to the level instead, then placed where the player is:

func _physics_process(delta: float) -> void:
	if Input.is_action_just_pressed("shoot"):
		var bullet_temp = bullet.instantiate()
		get_parent().add_child(bullet_temp)
		bullet_temp.global_position = global_position

get_parent() is whatever the player sits under (your main scene), so the bullet lives there and only its own script moves it. Set global_position after add_child so it starts wherever the player was when you fired

I also swapped is_action_pressed for is_action_just_pressed. With is_action_pressed you spawn a bullet every physics frame while the button is held, which is 60 a second. just_pressed gives you one per press until you add a fire rate timer

One more thing worth doing early: the bullets never get deleted, so they pile up forever off screen. Add a VisibleOnScreenNotifier2D to the projectile scene and connect its screen_exited signal to queue_free()