Pathfinding not detecting location at an elevation

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

Why won’t my humanoid get to its location part?

1 Like

This is because if the NPC is at a lower elevation and picks a location higher than him, his goal’s Y position is inside the block.

local goal = torso.Position + Vector3.new(randX,0,randZ)

You’ll need to come up with a way to make sure that you move his goal’s position higher until it no longer collides with a block.

That line is from another function that is not currently being used. The function that is pathfinding to the part is called move().

Here is the ComputeAsync of that pathfinding function:

path:ComputeAsync(torso.Position,workspace.endpart.Position)

Does endPart collide with the ground.

Yes, the endpart collides with the ground

Screen Shot 2021-06-19 at 4.31.07 PM

1 Like

If you run :MoveTo on the part it will raise it until it no longer collides. Also make sure to anchor it, it may be falling through the map.

1 Like

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.

Please tell me if this works.

1 Like

I did what you said, but unfortunately the NPC is still not jumping to the part.

path:ComputeAsync(torso.Position,workspace.Model.PrimaryPart.Position)

(sorry for responding late)

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.

Maybe try put a stair…maybe it works

Stairs work, but I was wondering if the NPC can get to the part just by jumping to it.

I think it depends on how much height the part…maybe try to lower it a bit and see what happen

I made the height of the part lower as you told me, but it still won’t work.

As you can see, the jump is possible, so I can’t see why the NPC can’t compute the pathfinding path.

This could just be a limitation of pathfinding though

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.

1 Like