Godot Version
v4.7
Question
Hi, I’m having trouble when it comes to removing projectile when switching scene. When I fire and quickly change scene, the projectile is still there.
In the bullet script
extends Area2D
# How fast and How far the bullet can go
var bullet_speed : int = 500
var travel_distance : float = 0.0
var max_range : int = 1500
var pierce : int
var damage : int
func _physics_process(delta: float) -> void:
var direction = Vector2.RIGHT.rotated(rotation)
position += direction * bullet_speed * delta
# Increases the further it goes until it hits max range
travel_distance += bullet_speed * delta
if travel_distance > max_range:
queue_free()
func _on_body_entered(body: Node2D) -> void:
if pierce <= 0:
queue_free()
pierce -= 1
if body.has_method("take_damage"):
body.take_damage(damage)
In the player script
# the range_scene_path is the bullet. “res://Scripts/bullet.gd”
# curret_weapon is the gun. For this example, it's the pistol: load(“res://GlobalAndResource/pistol.tres”)
func shoot() -> void:
var new_bullet = load(current_weapon.range_scene_path).instantiate()
var mouse_pos := (get_global_mouse_position() - global_position).angle()
new_bullet.damage = current_weapon.damage
new_bullet.global_position = global_position + Vector2(5, 0).rotated(mouse_pos)
new_bullet.global_rotation = mouse_pos
get_tree().root.add_child(new_bullet)
As for the going to a different scene.
func _on_map_pressed() -> void:
get_tree().change_scene_to_file("res://Scenes/wolrd_map.tscn")
The world map is the scene where the player goes to 90% of the time. There are other levels, and yes, the bullet does linger there as well if I’m quick enough.