Hi. Here I am again, after many hours of stress of trying to make NPCs avoid each other. I made many solutions but they all didn’t work, which is why I am here.
How can I do this effectively so that they avoid each other most of the time? Please leave some ideas below because I am getting really stressed and desperate. Thanks for your help!
There are many ways to approach this, you could do it by name or add values inside to indicate their “team.” That is what I would do when programming NPC’s, I usually add some Value with the name of their “team” and use an if statement to check for that value. Here is an example:
NPC (Model) > Bot (BoolValue)
if not target.Parent:FindFirstChild("Bot") then -- target would just be the basepart of the character the NPC wants to chase after
-- code here
end
I already have my NPCs grouped together in a group, where they know all of the people in the group so they don’t really need a value to keep track if they are a bot. To me, it doesn’t matter if groups collide with groups. But, I like the idea that you brought up. I will raycast, then check if it is part of the group, then avoid it.
I’ve recently made something like this. All it does is raycast in the lookvector of the HumanoidRootPart.
As long as nothing is there I generally don’t do anything. But if there is something blocking then I do a generic for loop in increments of a specific angle. For instance
for i=-45, 45, 15 do
-- loop between -45 degrees and 45 degrees by 15 degree increments.
end
I then check to see if there’s a free angle. If so then I make them walk in that direction for x amount of seconds. Then they go back to walking forward.
It’s not perfect. But it does work. This is an earlier version. It’s definitely resource-hungry but as you can see it works kind of okay with a large number. In my experience, the delays for walking back in a straight line gets longer and longer. I’ve tried using parallel Lua but unfortunately, it caused crashes frequently.