I made an enemy with a pathfinding AI. However, if I put a copy of the enemy on the same scene, the project will crash
How can I make it so 2 or more copies of the pathfiniding enemy will work on the same scene?
My code
extends CharacterBody2D
const speed = 25
@export var player: Node2D
@onready var nav_agent := $NavigationAgent2D as NavigationAgent2D
func _physics_process(_delta: float) → void:
var dir = to_local(nav_agent.get_next_path_position()).normalized()
velocity = dir * speed
move_and_slide()
func makepath() → void:
nav_agent.target_position player.global_position
func _on_timer_timeout():
makepath()
func enemy():
pass
func _on_collision_body_entered(body):
if body.get_name() == “bolinha”:
queue_free()
func _on_detection_area_body_entered(body):
if body.get_name() == “player”:
get_tree().change_scene_to_file(“res://Scene/world.tscn”)