Hello, Im making a game and I am wondering if I can make it so when a zombie gets near and NPC the NPC will run away using pathfinding service back to their house like minecraft but I am having trouble programming that so if anyone could help that would be great
What you’d probably want to do is set up different waypoints and maybe add some sort of orb around the NPC/Zombie so that when one of them touches it they change their path so they head back towards a waypoint called house.
I’m not too experienced in using this but this is what I’d do with the knowledge I have.
- In Minecraft they run to their house once it’s night, so I suggest using that instead.
- Just have a detection system and change their path and find the closest door.
So what you can do is use RunService.HeartStepped()
and run the following once a zombie gets close/when it’s night
local ClosestDoor
for _,door in pairs(doors:GetChildren)
if not ClosestDoor or (NPC.HumanoidRootPart.Position - door.Position).Magnitude >
(NPC.HumanoidRootPart.Position - ClosestDoor.Position).Magnitude then
ClosestDoor = door
end
end
-- use pathfinding service to make the NPC run through the door
Note, the doors variable is the model which ever door is in.
Instead I suggest checking how big the Magnitude of the NPC and the Zombie is; if it’s higher than X then the villager runs away.
Tho he might even need to make a custom Pathfinder, as else the NPC might run towards the zombie.
Yes but I have traps in my maze and I want the NPC to avoid them as well
please read:
In this article they mark the path with white neon balls; I suggest checking for every waypoint if they come in contact with a trap, so if the path comes in contact with a trap you can connect the :Blocked()
event to the path object and keep repeating this process, until you find the best path.
I have read that but I want to do much more than avoid traps avoiding dynamic objects like players well. the traps were examples
It’s the same thing, the NPC just has to keep track of the path he made and if the path doesn’t work out, for example a player comes in contact with a WayPoint of the path/gets close to a path (use Magnitude), then you use the :Blocked()
event and connect it to the path object.
There’s a whole topic talking about this subject, just read the article.