Godot Version
4.3 stable
Question
Hello! I am making my first game to a gamejam, and I am having problems with fps drops when around 7 enemies try to walk.
I believe the problem is with my enemy movement code because when the enemies are stopped, the game runs fine
Here is enemy.gd code:
extends CharacterBody2D
var player_inside_vision: bool = false
var player: CharacterBody2D
var can_shoot: bool = true
const SPEED: float = 50.0
const ENEMY_BULLET = preload("res://scenes/enemy_bullet/enemy_bullet.tscn")
@onready var shoot_delay: Timer = $ShootDelay
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
if player_inside_vision == true and can_shoot == true:
velocity = Vector2(0, 0)
shoot()
func _physics_process(delta: float) -> void:
if player:
if player_inside_vision == false:
var direction = (player.global_position - global_position).normalized()
velocity = direction * SPEED
move_and_slide()
else:
velocity = Vector2(0, 0)
func _on_shooting_distance_body_entered(body: Node2D) -> void:
if body.is_in_group("Player"):
player_inside_vision = true
func _on_shooting_distance_body_exited(body: Node2D) -> void:
if body.is_in_group("Player"):
player_inside_vision = false
func _on_player_reference_body_entered(body: Node2D) -> void:
if body.is_in_group("Player"):
player = body
func shoot():
var bullet = ENEMY_BULLET.instantiate()
bullet.position = global_position
var direction = (player.global_position - global_position).normalized()
bullet.direction = direction
get_parent().call_deferred("add_child", bullet)
can_shoot = false
shoot_delay.start()
func _on_shoot_delay_timeout() -> void:
can_shoot = true
The game is a top down shooter.
Any ideas on how I can fix this?



