Avoid npcs getting in a stack

How would I avoid npc getting together like this? I just use basic roblox pathfinding. (loop to compute async to target, and move the character) .

What I mean is the npcs should avoid using too similar of a path / avoid walking too close to each other.

I would appriciate any help (I’ve tried using a loop to check if another npc is close and ignore that waypoint, but that didn’t really work and can be problematic),

there are pathfinding modifiers aswell, but those didn’t work. (also wouldn’t the npc avoid itself)

I don’t know how to help you, except maybe make it so the npcs can’t climb or jump?

The npcs seen don’t do those things. :slight_smile:

Yet they still happen to somehow get on top of each other?

They don’t? Did you watch the video?

OHH, when I first clicked that link, there was an error. Now I understand.
HOW ARE THEY WALKING THROUGH EACH OTHER?!?!?

Their baseparts have been assingned a collision group, and that collision group has been set to not collide with itself.

Them going through each other is not neccessarily the problem, but they should avoid it.

If not going through each other is, then what?

from what i read, i think you’re trying to prevent the npcs from standing next to each other?

if so you can check the magnitude between the 2 npcs and if they’re close to each other say 5 studs or something, you can code one of the npcs to walk to the minimum distance possible.

for instance,

local min_distance = 5
local magnitude = (npc_2.Position - npc_1.Position).Magnitude
local direction = (npc_2.Position - npc_1.Position).Unit
local rs = game:GetService("RunService")
local within_distance = false

rs.Stepped:Connect(function()
	if magnitude <= min_distance then
		within_distance = true
		local distToMoveTo = magnitude - min_distance
		local target_pos = npc1.Position + direction * distToMoveTo
		else within_distance = false
	end
end)

if within_distance == true then
	npc2.Humanoid:MoveTo(target_pos)
end

(idk if this would work btw but thats what i would do. not really a great solver but just a suggestion)

Yeah I tried magnitude checks like this, but it didn’t really help at all. It could also be that I used them in the wrong way.

Pathfinding modifiers have an ability to avoid objects, so similar functionality could be needed.

This starts looking really bad when you have many npcs and they all just end up stacking together, behaving almost like they follow the same path.

Well they’re NPCs you’re in a line, what else do they do?

This may be helpful for you

1 Like

Thanks for the reply. What I got from the video is that he’s using an algorythm for his pathfinding, but then also gives “a quick solution”.

That being moving the npc near the player using a random position. I would like to know how to exactly do that correctly.

local char: Model = self.Character
		local hum: Humanoid = char:FindFirstChild("Humanoid") if not hum then return end
		local HRP: BasePart = char:FindFirstChild("HumanoidRootPart") if not HRP then return end
		
		local distanceToTarget = (HRP.Position - part.Position).Magnitude
	
		local SpreadFactor = 1 + (distanceToTarget/ 300)
		
		local randomOffset = Vector3.new(math.random(1,50),0,math.random(1,50))

		
		local goalPos = part.Position + randomOffset + (part.Position - HRP.Position).Unit * self.FollowDistance

Just giving an extra random vector3 didn’t unsurprisingly produce better results: :sweat_smile:

Check the video from around 2:38 to 6:30. He talks about the repulsion system he uses, the pathfinding solution he uses, and how he combines that pathfinding with the repulsion to implement the separation effect. I’d say just copy more-or-less what he did

Repulsion
For each NPC, he queries neighboring NPCs within a radius and calculates a repulsion force that should act on that NPC (check 3:42). Perhaps, you could use GetPartBoundsInRadius to check for neighboring NPCs and calculate repulsion forces based on distances to each neighboring NPC.

Pathfinding
I assume for this, you’re using Roblox’s API for pathfinding. I don’t have a lot of experience with it but I imagine you’d be able to get a movement vector based on the next waypoint or player directly.

Combining the two
So now you have a repulsion vector and a movement vector. It’s up to you to decide how you want to combine the two. In the video (around 6:00 in the video), he mentions that if the NPC is far from the goal, he’ll weigh the repulsion vector more and if the NPC is near the goal, he’ll way the movement vector more. Ultimately it’s up to you to fine-tune it how you’d like though.

He also goes over another method using potential fields but I haven’t really watched that part.