Godot Version
v4.3.stable
The sitiation
I was making a space invaders game but I can’t make the player shoot laser
Player script
extends CharacterBody2D
const SPEED = 110
var laser = preload("res://Scenes/laser.tscn")
func shoot():
laser.instantiate()
func _physics_process(delta):
var direction = Input.get_vector("move_left", "move_right", "move_up", "move_down")
position += direction * SPEED * delta
if Input.is_action_pressed("Shot"):
shoot()
move_and_slide()
Laser script
extends Area2D
const SPEED = 130
func _physics_process(delta):
position.y += -SPEED * delta
I can’t figuer it out alone. If someone can healp I be thankful.
You still have to add the laser to the scene. Add this line of code after laser.instantiate()
:
add_child(laser)
1 Like
Erro
Invalid type in function ‘add_child’ in base ‘CharacterBody2D (space_ship.gd)’. The Object-derived class of argument 1 (PackedScene) is not a subclass of the expected argument class.
I added the line and got this erro
Replace laser.instantiate()
with var new_laser = laser.instantiate()
, then replace add_child(laser)
with add_child(new_laser)
.
1 Like
That fix the not spaning plobem thaks.
Now I have a new problem:
the lazer is afected by the player movement
I am going to try to fix that by my on and come back if I am cluless
system
Closed
February 26, 2025, 4:13pm
7
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.