I made a pathfinding script for my NPC for it to go to a specific part. When the location part is on the same ground level as the NPC, it works. However, when I put the location part to a higher elevation, the pathfinding does not work.
I have included the jump action in my script so the NPC should be able to jump to get to its location. I have also tried to get to the part in-game, and the jump is possible. Here is my script:
local human = script.Parent:WaitForChild("Humanoid")
local torso = script.Parent:WaitForChild("LowerTorso")
local value = script.Parent.Value
local pathArgs = {
["AgentHeight"] = 5,
["AgentRadius"] = 2, -- how wide the character is
["AgentCanJump"] = true
}
function move()
local path = game:GetService("PathfindingService"):CreatePath(pathArgs)
path:ComputeAsync(torso.Position,workspace.endpart.Position)
local waypoints = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success then
for i, waypoint in pairs(waypoints)do
if waypoint.Action == Enum.PathWaypointAction.Jump then
human.Jump = true
end
human:MoveTo(waypoint.Position)
human.MoveToFinished:Wait(2)
end
else
print("Unsuccessful")
human.Health=0
end
end
function moveRandom()
local randX = math.random(-100,100)
local randZ = math.random(-100,100)
local goal = torso.Position + Vector3.new(randX,0,randZ)
local path = game:GetService("PathfindingService"):CreatePath(pathArgs)
path:ComputeAsync(torso.Position,goal)
local waypoints = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success then
for i, waypoint in pairs(waypoints)do
if waypoint.Action == Enum.PathWaypointAction.Jump then
human.Jump = true
end
human:MoveTo(waypoint.Position)
human.MoveToFinished:Wait(2)
end
else
print("Unsuccessful")
human.Health=0
end
end
if value.Value == true then
move()
else
while wait(1) do
moveRandom()
end
end
Also sorry didn’t mention this originally but if you use :MoveTo you’ll have to put the part in a model, set the part as the primary part and then call the function on the model.
I also found out that the pathfinding works when the part is put at a lower elevation compared to the position it was in before, so it’s still able to jump.
Here it is at the part’s current position:
Here it is at a lower elevation:
Note that both part positions are possible to get to, but the NPC only wants to complete the latter.
The highest an NPC pathfinding can jump is 7 studs.
If you want to look at the navigation mesh go to studio settings and you will be able to see where the NPC can jump up and down from
I also know the maximum distance lengthwise is limited too. When you open it look at the arrows then you will be able to know what an NPC can and can’t do.
The maximum distance for a pathfinding jump lengthwise is 6 studs.