I am trying to create an enemy AI that can see the player with 360 degree vision.
Currently he only detects the player if he is in front of him.
I’m not too sure how raycasts work, so I’m not sure why this is happening. I thought orientation does not matter when raycasting, but I guess it does. I attached a video below showing what the AI does. Thanks for the help.
This loop constantly runs, controlling the AI:
local target, player = findTarget()
if target then
target = target + (ai.HumanoidRootPart.CFrame.LookVector*5)
local distance = (ai.HumanoidRootPart.Position - target)
local lookVector = ai.HumanoidRootPart.CFrame.LookVector
local dotProduct = distance.Unit:Dot(lookVector)
local raycastResult = workspace:Raycast(ai.HumanoidRootPart.Position, target - ai.HumanoidRootPart.Position,raycastParams);wait()
print(raycastResult)
if raycastResult then
local rayPart = Instance.new("Part", workspace)
rayPart.Anchored = true
rayPart.CanCollide = false
rayPart.Color = Color3.fromRGB(255,0,0)
rayPart.Name = "RayPart"
rayPart.Size = Vector3.new(1, 1, (ai.HumanoidRootPart.Position - raycastResult.Position).Magnitude)
rayPart.CFrame = CFrame.new((ai.HumanoidRootPart.Position + raycastResult.Position)/2, raycastResult.Position)
end
if raycastResult --[[ and dotProduct < maxVector]] then
print(raycastResult.Instance.Parent)
if raycastResult.Instance.Parent == player.Character then
ai.Humanoid:MoveTo(target)
task.wait()
elseif player.Character.Humanoid:LoadAnimation(player.Character.Crouching:WaitForChild("Crouching")).IsPlaying then
Path:Run(target)
end
else
task.wait()
end
else
task.wait()
end
ai test.wmv (1.2 MB)