Enemy seperation while pathfinding

Hello, I am having problems making pathfinding, where npcs keep distance from each other.

I was recommended this video, which gave me some kind of insight:

https://www.youtube.com/watch?v=mUDuuVa0A6U

As you can see here, if npcs follow the player they eventually get together and become a stack, and that’s not what I really want, because it looks stupid when there are like 50 npcs inside of 1 npc.

So this is how following the player works :

--we have a goal position which is where the npc is supposed to go
local goalPos = part.Position + (part.Position - HRP.Position).Unit * self.FollowDistance

--Trying to get "repulsion forces", honestly not even sure what I am doing

		local RepulsionVector = Vector3.new()
		for i, npc in pairs(NearbyNPC) do
			local npcHRP = npc:FindFirstChild("HumanoidRootPart")
			if npcHRP then
				local distance = (npcHRP.Position - HRP.Position).Magnitude
				if distance > 0 and distance <= 10 then
					RepulsionVector = RepulsionVector + (npcHRP.Position - HRP.Position).Unit * 1000 / distance
				end
			end
		end


--This is where the npc moves

local FirstWaypoint: PathWaypoint = nil
			for i, waypoint in ipairs(waypoints) do
				if i ~= 1 and ((waypoint.Position - HRP.Position) * Vector3.new(1, 0, 1)).Magnitude > self.FollowDistanceMargin then
					
				
				
					FirstWaypoint = waypoint
					break
					
				end
			end

			if FirstWaypoint then
				
			
				
				
				if FirstWaypoint.Action == Enum.PathWaypointAction.Walk then
					hum:MoveTo(FirstWaypoint.Position)
				end

How would I make the NPCs move to a position (in this case the player), while avoiding other npcs. I am not entirely sure what calculations should I do. Video was helpful, but I couldn’t read the code.

1 Like

A very simple system would be to make a part that is attached to the NPC and whenever that part is touched it moves in the opposite direction of which face was hit. Although, with simplicity comes a lot of problems. If you think about the system enough you will find them.

that’s not exactly the functionality I am looking for here. Besides using hit detection like that wouldn’t be wise.

Assuming just having the NPCs collide with each other is not an option, you could try to have the npc move away from colliding (use magnitude for this) NPCs when calculating the arguments for MoveTo().

I’m having the same problem, you can see my attempts at a boids algorithm in my post here:

Really want to find a fix to this, would make the enemies look so much better.

yeah I was not able to figure out a good calculation