i am looking for better or different ways to implement projectiles.

Godot Version

3.4

Question

i wrote simple code to shoot projectiles which preloads a ‘bullet’ scene then when input is pressed it ‘spawns’ an instance of that scene in that position the player is in . but shooting when an instance is already running rests said instance’s position to the player’s.
I am a beginner and understand the need to solve my problems especially simple ones myself but I’m stumped. pls help and thank you.
EDIT : I have not proovided enough of the code for it to be understandable and I apologise.

extends CharacterBody2D
var bullet_instance = preload("res://scenes/bullet.tscn")


@export var speed = 600
@export var accel = 4000
@export var decel = 2000
@export var my_posx = position.x
@export var my_posy = position.y
#func handle_input():
#	var direction = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
func shoot():
	bullet_instance.instantiate().set_position(Vector2(position.x, position.y))
	owner.add_child(bullet_instance)
	
func _physics_process(delta):
	if Input.is_action_just_pressed("shoot"):
		shoot()
	var direction = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
	if direction != Vector2.ZERO:
		var target_velocity = speed * direction
		velocity = velocity.move_toward(target_velocity, accel * delta)
	else :
		velocity = velocity.move_toward(Vector2.ZERO, decel * delta)
	move_and_slide()

Where in your code do you spawn the bullet_instance? Have you tried making a new .instantiate() each time the player shoots?

What @gertkeno said. Try this:

1 Like