Pathfinding is not working as i want
Well i’m making a horror game and i want a monster that dodges obstacles and hunt the player, but when the monster is inside a room and hunting the player, if the player decides to hide and the waypoint is out of the room he begins go to the wall.
Video:
The Script:
local character = script.Parent
local humanoid = character.Humanoid
local PathfidingService = game:GetService("PathfindingService")
character.PrimaryPart:SetNetworkOwner(nil)
local path = PathfidingService:CreatePath(
{
["AgentHeight"] = 5;
["AgentRadius"] = 2;
["AgentCanJump"] = false;
Costs = {
Water = 100;
DangerZone = math.huge
}
}
)
local function getPath(destination)
local pathParams = {
["AgentHeight"] = 5,
["AgentRadius"] = 2,
["AgentCanJump"] = false;
Costs = {
Water = 100;
DangerZone = math.huge
}
}
local path = PathfidingService:CreatePath(pathParams)
path:ComputeAsync(character.HumanoidRootPart.Position, destination.Position)
return path
end
local RunService = game:GetService("RunService")
local function canSeeTarget(target)
local origin = character.HumanoidRootPart.Position
local direction = (target.HumanoidRootPart.Position - character.HumanoidRootPart.Position).Unit * 60
local ray = Ray.new(origin, direction)
local hit, pos = workspace:FindPartOnRay(ray, character)
if hit then
if hit:IsDescendantOf(target) then
return true
end
else
return false
end
end
local function findTarget()
local players = game.Players:GetPlayers()
local maxDistance = 40
local nearestTarget
for index, player in pairs(players) do
if player.Character then
local target = player.Character
local distance = (character.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance < maxDistance and canSeeTarget(target) then
nearestTarget = target
maxDistance = distance
end
if distance < 5 then
--local playerDeath = game.ReplicatedStorage:WaitForChild("DeathEvent")
--local player = game.Players:GetPlayerFromCharacter(target)
--playerDeath:FireClient(player)
end
end
end
return nearestTarget
end
local waypoints
local block
local nextway
local reachedway
local function followpath(destination)
local success, errormessage = pcall(function()
path:ComputeAsync(character.HumanoidRootPart.Position, destination)
end)
if success and path.Status == Enum.PathStatus.Success then
waypoints = path:GetWaypoints()
block = path.Blocked:Connect(function(blockedWaypointIndex)
if blockedWaypointIndex >= nextway then
block:Disconnect()
followpath(destination)
end
end)
if not reachedway then
reachedway = humanoid.MoveToFinished:Connect(function(reached)
if reached and nextway < #waypoints then
nextway += 1
humanoid:MoveTo(waypoints[nextway].Position)
else
reachedway:Disconnect()
block:Disconnect()
end
end)
end
nextway = 2
humanoid:MoveTo(waypoints[nextway].Position)
else
warn("Error", errormessage)
wait()
end
end
local function walkTo(destination)
local path = getPath(destination)
if path.Status == Enum.PathStatus.Success then
for index, waypoint in pairs(path:GetWaypoints()) do
local target = findTarget()
if target and target.Humanoid.Health > 0 then
humanoid.WalkSpeed = 10
followpath(target.HumanoidRootPart.Position)
break
elseif not target then
humanoid.WalkSpeed = 5
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
end
else
humanoid:MoveTo(destination.Position - (character.HumanoidRootPart.CFrame.LookVector * 10))
end
end
local function patrol()
local waypoints = workspace.waypoints:getChildren()
local randomNum = math.random(1, #waypoints)
walkTo(waypoints[randomNum])
end
while wait() do
patrol()
end
Sorry for my english because i dont know speak English very well.