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.