#Enemy stuck on each other as they pathfind their way to the player.

3 messages · Page 1 of 1 (latest)

worthy cosmos
#

I'm making a Vampire Survivors clone game, and one of the issues I'm encountering is that enemies are getting stuck on each other and piling up like this. Obviously I could remove the collision, but otherwise is there a way to make them pathfind more effectively towards the player?

Here's the relevant code. Also bonus points if you guys can help with performance optimization cuz it starts to lag around 400 enemies:

extends CharacterBody2D

@export var hp: = 10
@export var movement_speed: = 50.0
@export var knockback_recovery: = 3.0
@export var xp_drop_amount: float = 1.0

var knockback: = Vector2.ZERO

@onready var player: CharacterBody2D = get_tree().get_first_node_in_group("player")
@onready var xp_orb: = preload("res://Utility/xp_orb.tscn")
@onready var sprite: Sprite2D = $Sprite2D
@onready var sound_hit: AudioStreamPlayer2D = $SoundHit
@onready var collision: CollisionShape2D = $CollisionShape2D
@onready var animation: AnimationPlayer = $AnimationPlayer

signal remove_from_array(object)

func _physics_process(_delta: float) -> void:
    enemy_movement()

func enemy_movement():
    knockback = knockback.move_toward(Vector2.ZERO, knockback_recovery)
    var direction: = global_position.direction_to(player.global_position)
    velocity = direction * movement_speed + knockback
    move_and_slide()
    
    if direction.x > 0.1:
        sprite.flip_h = true
    elif direction.x < -0.1:
        sprite.flip_h = false

func die():
    emit_signal("remove_from_array", self)
    
    animation.play("die")

func spawn_xp_orb():
    var spawned_xp_orb = xp_orb.instantiate()
    spawned_xp_orb.experience = xp_drop_amount
    
    spawned_xp_orb.global_position = global_position
    get_tree().current_scene.call_deferred("add_child", spawned_xp_orb)

func _on_hurtbox_hurt(damage, angle, knockback_amount) -> void:
    hp -= damage
    knockback = angle * knockback_amount
    if hp <= 0:
        die()
    sound_hit.play()
worthy cosmos
#

?

tender needle