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)
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)
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:
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.