NPC bugs when stepping on different terrain material

I have an npc that uses pathfinding, its path updates everytime it reaches a waypoint.
For some odd reason, pathfinding service doesn’t create a straight path and starts twisting and changing its path whenever my npc steps on a different terrain material.

I did not specify material costs so they should all be default 1. I did test it by changing costs before but this bug still persisted.
Any help is appreciated. (boosting bc i need help)

1 Like

So when you encounter issues with pathfinding, particularly when the NPC changes direction or twists due to terrain changes, the solution usually revolves around handling the costs and behavior of the pathfinding service more precisely. Here’s a refined approach that might resolve the issue:

  1. Material Costs Adjustment: Even though you mentioned setting the material costs to 1, it’s worth ensuring that each terrain type has a proper material cost assigned, as different terrain materials could have inherent default costs. Use the following to adjust the costs manually for different materials:

local pathfindingService = game:GetService(“PathfindingService”)
local agentParams = {
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = false,
AgentMaxSlope = 45
}
local path = pathfindingService:CreatePath({
AgentRadius = agentParams.AgentRadius,
AgentHeight = agentParams.AgentHeight,
AgentCanJump = agentParams.AgentCanJump,
AgentMaxSlope = agentParams.AgentMaxSlope,
Costs = {
Grass = 1,
Sand = 1,
Concrete = 1,
– Add more materials here with explicit costs
}
})

Ensure each terrain type has a consistent cost of 1 or adjust them accordingly if different terrains are supposed to affect pathfinding. This prevents terrain from influencing the path if it’s not intended to.

  1. Path Smoothing: Roblox pathfinding can sometimes create erratic paths if the terrain varies significantly in elevation or type. A solution to prevent sharp turns or path changes is to implement a smoothing algorithm after the path is computed:

local function smoothPath(waypoints)
local smoothedWaypoints = {}
local prevPoint = waypoints[1].Position

for i = 2, #waypoints do
    local currentPoint = waypoints[i].Position
    if (currentPoint - prevPoint).magnitude > 3 then -- Adjust the threshold as needed
        table.insert(smoothedWaypoints, waypoints[i])
        prevPoint = currentPoint
    end
end

return smoothedWaypoints

end

After computing the path, use this function to smooth out any sudden changes between waypoints.

  1. Re-evaluation Frequency: Pathfinding updates whenever the NPC reaches a waypoint, but constant recalculation can lead to erratic behavior if the terrain is complex. You might want to reduce the re-evaluation frequency by only recalculating the path when the NPC is sufficiently far from its next waypoint or if an obstacle actually blocks the path.

  2. Check for Custom Terrain Obstacles: If you have custom obstacles or specific regions that are not well-handled by the pathfinding service, ensure that you mark them as non-traversable or adjust their costs to make the NPC avoid those regions.
    combining these techniques, you should see an improvement in how the NPC navigates the terrain, with fewer unnecessary twists and changes in its path.

1 Like

I already set the material costs to 1 before and that issue still appeared. But I think the smoothing algorithm is a good idea, thank you.

1 Like

What type of games you are building