How to make many enemies don't stack together in Godot4?, please help

I’m trying to make 2 RayCasts2D to check, but my coding skills are kinda bad so it lags alot and also doesn’t work well

In this picture, there’re a few Slimes stacked on top of each other, i just need to move circle around them and they will be stacked like that
image

I want to make it like this, so no matter where i go, it’ll not be stacked on top of each other

image

func Move(delta):
	if(Global.world.localPlayer != null):
		if(currentHealth > 0):
			if rayCasts.get_child(0).is_colliding() == false:
				direction = Global.world.localPlayer.global_position - global_position
			else:
				if rayCasts.get_child(1).is_colliding() == false:
					direction = rayCasts.get_child(1).target_position
				else:
					rayCasts.get_child(1).rotation_degrees = rayCasts.get_child(1).rotation_degrees + directionIncreaseRayCast
					if rayCasts.get_child(1).rotation_degrees >= maxDirectionIncreaseRayCast:
						direction = Global.world.localPlayer.global_position - global_position

			rayCasts.get_child(0).rotation = direction.angle()	

			if (direction.length() > attackRange):
				global_position += direction.normalized() * delta * speed

image

Hey there,
i dont work much with Raycasts, but as is looks, your enemy has a lot Raycasts. But why dont you just place a cone oder a circle collider in front of the eyes. (I develop mobile games, so i stay away from Raycaysts). Inside the viewsiegt area you can place another area2d for the attack range check. I never had problems with this kind of collision. Even on low end devices.

Maybe its a try worth. :slight_smile:

If this is no option for you, try to reduce the number of Raycasts.

Are there more than one enemy on the map? (Maybe out of sight)

1 Like

I fixed it, actually kinda easy, if 2 enemies collide, I just make them go the opposite way of each other and remove all the ray casts
image

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.