So I'm making a dungeon game, and I want there to be guards that you have to kill, i would just retexture the default roblox player tracking mob, but it pathfinds through walls, so it's not very good for what I'm wanting to do.
Basically I want it to pathfind like a Minecraft zombie (doesn't pathfind through walls), but I'm unsure of how to do so.
If anyone could maybe share how I would do that, I would greatly appreciate it.
The following code is little sniplets from my npc George, hes quite advanced. You can find him in person here:
https://www.roblox.com/library/331847610/Its-George
He can also (or could) drive vehicles and did i mention he can also get travel sick! :P
lol anyway the basics of his path finding is below. Your more than welcome to download him and disect his brain (he's kinda used to me doing it! lol)
Code:
local Self = script.Parent -- Base local ME = Self.Humanoid -- My Brain! :D local Path = {} -- a Table that holds the points of our path local CurrentPoint = 1 -- Current point on our path to our destination local function computePathtoV3(Target) local PFS = game:GetService("PathfindingService") local path = PFS:FindPathAsync(Self.HumanoidRootPart.CFrame.p,Target,1024) return path:GetWaypoints() end -- wrap this in your target finder function/bit! --[[ kinda like local Target = workspace.TGazza local TargetPos = Target.HumanoidRootPart.Position ]] myPos = TargetPos Path = computePathtoV3(myPos) -- Main loop for traveling along the found path... local atEnd = false while true do if(#Path > 0 and AtEnd == false) then local Dist = (Self.HumanoidRootPart.Position - Path[CurrentPoint].Position).magnitude Self.Humanoid:MoveTo(Path[CurrentPoint].Position) if(Dist < 3) then CurrentPoint = CurrentPoint +1 end end if(CurrentPoint >= #Path) then AtEnd = true --// do stuff! end wait() end
Each time your npc gets to the end of a path you would clear the Path table and resetting the path index (CurrentPoint ) to 1
aka:
Path = {} CutrrentPoint = 1
Hope this helps! :)