Hello,
So I’m trying to make a game that has an AI like Piggy or Kitty. These AIs are very smart and have no problems climbing stairs, etc. But I’ve noticed that this is very hard to accomplish. I’m using the PathfindingService but I have had no luck. I set “AgentCanJump” off because I do not want my NPC to be able to jump, just like Piggy and Kitty. But if there is a small wall or table ( something the AI would normally jump over ) It just gets stuck. Even setting agentCanJump off.
Script:
local playersService = game:GetService('Players')
local pathService = game:GetService('PathfindingService')
local npc = script.Parent
local head = npc.Head
local hrp = npc.HumanoidRootPart
local humanoid = npc.Humanoid
npc.PrimaryPart:SetNetworkOwner(nil)
local npcPath = pathService:CreatePath({
['AgentRadius'] = 5.7,
['AgentHeight'] = 9.3,
['AgentCanJump'] = false
})
local function requestTarget()
if #playersService:GetPlayers() == 1 then
return game.Players:GetPlayers()[1].Character
elseif #playersService:GetPlayers() < 1 then
playersService.PlayerAdded:Wait()
end
local chosenPlayer = playersService:GetPlayers()[math.random(1,#playersService:GetPlayers())]
for _, player in pairs(playersService:GetPlayers()) do
if player.Character and player.Character.Humanoid and player.Character.Humanoid.Health > 0 then
local playerHead = player.Character.Head
if (head.Position - playerHead.Position).Magnitude < (head.Position - chosenPlayer.Character.Head.Position).Magnitude then
chosenPlayer = player
end
end
end
return chosenPlayer.Character
end
local function track(target)
if not target or not target.Humanoid or target.Humanoid.Health == 0 then
warn('Error, No target or no character, or target is dead')
return
end
npcPath:ComputeAsync(hrp.Position,target.HumanoidRootPart.Position)
if npcPath.Status == Enum.PathStatus.Success then
for _, waypoint in pairs(npcPath:GetWaypoints()) do
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
else
humanoid:MoveTo(hrp.Position - Vector3.new(1,0,0))
end
end
local function main()
local target = requestTarget()
if target and target.Humanoid then
if target.Humanoid.Health > 0 then
track(target)
end
end
end
while wait() do
main()
end
I would set jump to true because, in real life, things like this would be able to jump. AI that can jump over things IS smarter because the objective is to catch people and smart AI knows the fastest way to do that is jumping
just if you find any solution for this problem to reply me because is very annoying my customer jump in a structure i place… or if the customer is on floor 1 he go down by jumping from the floor and not use the stairs
Currently on mobile so apologies for the short reply.
Your solution lies with using Raycast in conjunction with PathfindingService. When an NPC gets stuck with pathfinding service, it fires a signal. When that signal is fired, you should see if your NPC is being blocked by something it can jump over (Ex, Raycast at Head and at HumanoidRoot.). Then go from there.
You have two options, regenerate the path until it generates one without a jump required, or use raycasting to detect a jump-able object and make your NPC jump
The piggy ai follows the players movements, if you move left it will move left at that same point. It only uses path finding if the player went through an obstacle they cant get past.
If you play piggy enough you will actually realise this. I think it saves the last few seconds of movement to a table then follows that and after its moved to that point it removes that position and adds a new position (the current position of that player its following) however if it isnt targeting anyone i think it does just use the path finding AI