how would i make it where once you turn the corner from the ai he is still gonna be chasing you until you lost him for 5 seconds, the ai i have rn right once u turn a corner/it looses sight of you it will not go after you im trying to make it more realistic where he still chases you for 5 seconds after turning a corner
local humanoid = teddy.Humanoid
teddy.PrimaryPart:SetNetworkOwner(nil)
local function canSeeTarget(target)
local origin = teddy.HumanoidRootPart.Position
local direction = (target.HumanoidRootPart.Position - teddy.HumanoidRootPart.Position).unit * 600
local ray = Ray.new(origin, direction)
local hit, pos = workspace:FindPartOnRay(ray, teddy)
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 = 600
local nearestTarget
for index, player in pairs(players) do
if player.Character then
local target = player.Character
local distance = (teddy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance < maxDistance and canSeeTarget(target) then
nearestTarget = target
maxDistance = distance
end
end
end
return nearestTarget
end
local function getPath(destination)
local PathfindingService = game:GetService("PathfindingService")
local pathParams = {
["AgentHeight"] = 15,
["AgentRadius"] = 4,
["AgentCanJump"] = false
}
local path = PathfindingService:CreatePath(pathParams)
path:ComputeAsync(teddy.HumanoidRootPart.Position, destination.Position)
return path
end
local function attack(target)
local distance = (teddy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance > 4.6 then
humanoid:MoveTo(target.HumanoidRootPart.Position)
else
local attackAnim = humanoid:LoadAnimation(script.Attack)
attackAnim:Play()
target.Humanoid.Health = 0
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
print("TARGET FOUND", target.Name)
attack(target)
break
else
print("Moving to ", waypoint.Position)
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
end
else
humanoid:MoveTo(destination.Position - (teddy.HumanoidRootPart.CFrame.LookVector * 10))
end
end
function patrol()
local waypoints = workspace.waypoints:GetChildren()
local randomNum = math.random(1, #waypoints)
walkTo(waypoints[randomNum])
end
while wait(0.1) do
patrol()
end