My AI uses raycasting to see if a player is within it’s sight. However, it’s being finnicky and for some reason the RaycastResult will return nil despite visualizing it and clearly seeing that the Raycast is intersecting an object.
I should clarify: the raycast does find the target and return the result, other times it does not.
function checkSight(target)
local params = RaycastParams.new()
params.FilterDescendantsInstances = {model}
params.FilterType = Enum.RaycastFilterType.Exclude
local direction = (HumanoidRootPart.Position - target.Position).Unit * 60
local function showCast()
local part = Instance.new("Part")
part.Parent = workspace
part.CanCollide = false
part.Anchored = true
part.Size = Vector3.new(.1, .1, 60)
local dir = direction
part.CFrame = CFrame.new((HumanoidRootPart.Position + target.Position)/2, HumanoidRootPart.Position)
game:GetService("Debris"):AddItem(part, 10)
end
local raycastResult = workspace:Raycast(HumanoidRootPart.Position, direction, params)
if raycastResult then
if raycastResult.Instance then
if raycastResult.Instance:IsDescendantOf(target.Parent:FindFirstAncestorWhichIsA("Model")) or raycastResult.Instance == target then
return true
end
end
end
return false
end