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