My pathfinding NPC is really allergic to jumping despite canjump being set to true

I’m trying to make a functioning npc that chases you whenever you’re in their “LoS” radius.

It was going quite flawlessly until I decided to check whether the fella can jump or not, turns out his legs are a bit broken.

He does walk towards you, but he just doesn’t have the ability to jump, for some reason.

This is what happens (compressed because went above file size!!)

I don’t see any issues with my code:

local LOSsize = Vector3.new(75,25,75)
local Dummy = script.Parent
local PF = game:GetService("PathfindingService")
local DummyHumanoid = Dummy.HumanoidRootPart
local RS = game:GetService("RunService")
local players = game:GetService("Players")


function createAreapreview()
	local part = Instance.new("Part")
	part.Name = "LoSChecker"
	part.Parent = Dummy.HumanoidRootPart
	part.CFrame = Dummy.HumanoidRootPart.CFrame
	part.Size = LOSsize
	part.Color = Color3.fromRGB(255, 0, 4)

	part.Anchored = true
	part.Transparency = 0.6
	part.CanCollide = false
end
createAreapreview()

local DMGsize = Vector3.new(10,7,10)

function createDamageArea() -- ignore this function, it's irrelevant to the issue
	local part = Instance.new("Part")
	part.Name = "DamageDealer"
	part.Parent = Dummy.HumanoidRootPart
	part.CFrame = Dummy.HumanoidRootPart.CFrame
	part.Size = DMGsize
	part.Color = Color3.fromRGB(230, 255, 120)
	
	part.Anchored = true
	part.Transparency = 0.6
	part.CanCollide = false
end
createDamageArea()

local dmgDealer = DummyHumanoid.DamageDealer -- ignore this also
local losChecker = DummyHumanoid.LoSChecker

local parameterTable = {
	AgentRadius = 3,
	AgentHeight = 5,
	AgentCanJump = true, -- set to true but the guy still doesn't jump
	AgentCanClimb = false,
	WaypointSpacing = 2
}

RS.Stepped:Connect(function() 
	---------------LINE OF SIGHT CHECKER----------------
	local LOS = workspace:GetPartBoundsInBox(losChecker.CFrame, losChecker.Size)
	losChecker.CFrame = DummyHumanoid.CFrame
	for _,v in LOS do
		if v.Parent:FindFirstChild("Humanoid") then
			local plr = players:GetPlayerFromCharacter(v.Parent)
			if plr then
				losChecker.Color = Color3.fromRGB(123, 255, 93)
				local path = PF:CreatePath(parameterTable)
				path:ComputeAsync(DummyHumanoid.Position, plr.Character.HumanoidRootPart.Position)


				if path.Status == Enum.PathStatus.Success then
					Dummy.Humanoid:MoveTo(plr.Character.HumanoidRootPart.Position)
				end
			else
				losChecker.Color =  Color3.fromRGB(255, 0, 4)
			end	
		end
	end
-- there's more code under here but it's connected to the damage function, no "ends" are missing
	```

The issue is this part:

All you’re doing with this is seeing if a path is possible, and if so, are making the NPC walk in a straight line to a player - regardless of whether or not there’s walls or obstacles.

What you’ll have to do is get and use the path’s waypoints with :GetWaypoints() and have the humanoid move to each waypoint instead of the player’s root part. Waypoints also let you know if the NPC should either walk or jump at that waypoint. You can code your NPC to jump at the jump waypoints to solve your issue.

How exactly do i know when the jump waypoint occurs and when to make the NPC jump?

Is it done via some if statement that compares the Y position of NPC and each waypoint? Or is it done differently

You would iterate through the waypoints in a key value pair:

if path.Status == Enum.PathStatus.Success then
	local waypoints = path:GetWaypoints()
	for _, waypoint in pairs(waypoints) do
		print(waypoint.Action)
	end
end

Where the print statement currently is, you can check the waypoint’s “Action” property.

if waypoint.Action == Enum.PathWaypointAction.Walk then
	humanoid:MoveTo(waypoint.Position)
else --presumably the waypoint action would be a "jump" one otherwise
	humanoid.Jump = true
	humanoid:MoveTo(waypoint.Position)
end

You’ll need some additional logic to wait until a waypoint is reached (or becomes blocked). I would recommend doing a distance check of about 4-5 studs from the NPC’s root part and the waypoint to determine if they “reached” it. You can also listen to some pathfinding events to determine this, but I’ve found those to be a bit flaky.

I see, i’ll play around with this, everything else works fine for now.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.