Hello devs, so after a long time I decided to use pathfinding for my entity in my game.
local PathfindingService = game:GetService('PathfindingService')
local Variables = {
AgentParams = {
AgentCanClimb = true,
AgentRadius = 3,
AgentCanJump = true,
AgentHeight = 4,
WaypointSpacing = 5,
Costs = {
}
},
}
--find target just returns closest HumanoidRootPart
while task.wait() do
local Target = FindTarget()
if Target then
local path = PathfindingService:CreatePath(Variables.AgentParams)
path:ComputeAsync(HumanoidRootPart.Position,Target.Position)
for i,v in pairs(path:GetWaypoints()) do
Humanoid:MoveTo(v.Position)
end
end
end
Everything works except for pathfinding part.
Entity chases player properly but thing is he doesnt avoid barriers (just walking straightly to player), same as using Humanoid:MoveTo(Target.Position)
Its been like 3-4 hours since im trying to fix and my brain is melting. So I came here to ask for help more experienced devs than me.
The issue might not have to do with your script, I don’t see anything inherently wrong with it.
How thick are the barriers? if they are too thin the navmesh might miss the walls and pretend they don’t exist, causing the AI to generate a path straight to the player.
Also try using ComputeSmoothPathAsync() instead. It’s deprecated but sometimes it solves the problem.
I see the issue now, in the for i, v in pairs loop you don’t wait for the humanoid to have moved, so it’ll cycle through all the waypoints instantly, then it’ll stop at the last waypoint which is the player position, essentially acting as a :MoveTo function.
0.3 seconds isn’t much of a delay, it is pretty average for roblox pathfinding. I would suggest adding a function that makes the AI immediately move towards the target location if it has direct line of sight, and let pathfinding do it’s thing if that isn’t the case.