So, we’re developing a horror game where an AI will be chasing you around a relatively large map. I’d rely mostly on PathfindingService for it to navigate properly. However, there was one kink I couldn’t quite figure out.
The map has doors that are collidable, and I wondered if there’s any way for me to make it so the AI can travel through those doors (or open them) but for that to work PathfindingService would need to be able to ignore those models.
Any way as to how to go about this? Thanks!
5 Likes
Maybe set the doors to CanCollide = false on the server (but true on the clients) and then do the pathfinding?
Edit:
The door object has a tag of “Door”.
LocalScript:
local DOOR_TAG = "Door"
local CollectionService = game:GetService("CollectionService")
local conns = {}
local function KeepSolid(part)
part.CanCollide = true
conns[part] = part:GetPropertyChangedSignal("CanCollide"):Connect(function()
part.CanCollide = true
end)
end
for _, door in pairs(game:GetService("CollectionService"):GetTagged(DOOR_TAG)) do
KeepSolid(door)
end
CollectionService:GetInstanceAddedSignal(DOOR_TAG):Connect(KeepSolid)
CollectionService:GetInstanceRemovedSignal(DOOR_TAG):Connect(function(door)
local conn = conns[door]
if conn then
conn:Disconnect()
conns[door] = nil
end
end)
server Script (mostly just copied from Character Pathfinding):
local DOOR_TAG = "Door"
local PathfindingService = game:GetService("PathfindingService")
local CollectionService = game:GetService("CollectionService")
local function SetDoorsCanCollide(canCollide)
for _, door in pairs(game:GetService("CollectionService"):GetTagged(DOOR_TAG)) do
door.CanCollide = canCollide
end
end
--------- start wiki code
-- Variables for the zombie, its humanoid, and destination
local zombie = game.Workspace.Dummy
local humanoid = zombie.Humanoid
local destination = game.Workspace.Goal
-- Create the path object
local path = PathfindingService:CreatePath()
-- Compute the path
SetDoorsCanCollide(false) ------------- added to wiki code
path:ComputeAsync(zombie.HumanoidRootPart.Position, destination.Position)
SetDoorsCanCollide(true) ------------- added to wiki code
-- Get the path waypoints
local waypoints = path:GetWaypoints()
-- Loop through waypoints
for _, waypoint in pairs(waypoints) do
local part = Instance.new("Part")
part.Shape = "Ball"
part.Material = "Neon"
part.Size = Vector3.new(0.6, 0.6, 0.6)
part.Position = waypoint.Position
part.Anchored = true
part.CanCollide = false
part.Parent = game.Workspace
-- Move zombie to the next waypoint
humanoid:MoveTo(waypoint.Position)
-- Wait until zombie has reached the waypoint before continuing
humanoid.MoveToFinished:Wait()
end
3 Likes
That would work I think, but it pains me that there is no sort of filter that you can give to the API (like you can with RaycastParams for example).
1 Like
I’ve been working on an alternative to Roblox’s PathfinderService, a navigation mesh pathfinder which supports editing the mesh (whitelists, blacklists) and custom links (opening doors, taking an elevator, entering a vehicle). You can follow the progress on discord here: Polaris-Nav.
In the mean time, you can put the doors in a model with a humanoid. This will cause Roblox’s pathfinder to ignore it, but you will need to hide the name and health, and rename it so most weapons don’t confuse it with a normal humanoid.
4 Likes
There is no whitelist / ignore list because the underlying structure is different… Navigation meshes are more precise and meant to be traversed quickly so a large area can be covered. A new mesh has to be made for every desired combination of whitelist / blacklisted parts.
Raycasting on the other hand is much simpler, using spatial search data structures which try to limit the number of intersections found, instead of expanding out and searching many at great speed.
2 Likes
That is correct, but the PathfindingService offers nearly zero customizability which irritates me. You cannot even use a Roblox API to make a new navmesh and use that one instead. @nicemike40’s workaround works fine, but the game engine should offer a more elegant solution in my opinion.
3 Likes
When I interned at Roblox, I pitched working on the pathfinder for them. I didn’t, and it doesn’t seem to be a priority for them. They use the same libraries other engines use, but have much less support in comparison.
I hope that my pathfinder can replace the Roblox one, and one day even replace pathfinders in other game engines.
2 Likes