Pathinding taking longer paths than necessary

(Yellow nodes are jumps, green nodes are walks)


My AI is trying to take an extra long path to reach my character. It’s trying to jump onto the bench in front of it, then back down and then towards me, when it could just go straight to me. Why is this what path it would take?

BTW the squares represent the agent radius

local function followPlayer(target)
	local playerRoot = target:FindFirstChild("HumanoidRootPart")
	local playerHum = target:FindFirstChild("Humanoid")
	if playerRoot ~= nil and playerHum.Health > 0 then
		path = getPath(playerRoot)

		if path.Status == Enum.PathStatus.Success then
			local waypoints = path:GetWaypoints()
			local waypoint, index
			local jumpWaypoint, power

			for i, point in pairs(waypoints) do
				if i > 1 then
					if (waypoints[1].Position - point.Position).Magnitude > 2 then
						if point.Action == Enum.PathWaypointAction.Walk and waypoint == nil then
							waypoint = point
							index = i
						else
							break
						end
					end
				end
			end

			if index then
				local possibleJump = waypoints[index + 1]
				if possibleJump and possibleJump.Action == Enum.PathWaypointAction.Jump and waypoint ~= nil then
					jumpWaypoint = possibleJump
				end
			end
			if waypoint then
				if jumpWaypoint and jumpWaypoint.Action == Enum.PathWaypointAction.Jump then
					humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
					canJump = false
					local con

					con = humanoid.StateChanged:Connect(function(old, new)
						if new == Enum.HumanoidStateType.Landed then
							con = nil
							canJump = true
						end
					end)

					task.delay(3, function()
						if con ~= nil then
							con:Disconnect()
							canJump = true
							print(canJump)
						end
					end)
					local nodeReached = false
					humanoid:MoveTo(jumpWaypoint.Position)
					humanoid.MoveToFinished:Connect(function()
						nodeReached = true
					end)

					local currentNode = workspace.Assets.Paths:FindFirstChild(bot.Name .. "-".. index)

					while task.wait() do
						if nodeReached then
							if currentNode then
								currentNode:Destroy()
							end
							break
						end
					end
				end
				humanoid:MoveTo(waypoint.Position)
			end
		end
	end
end
1 Like