In this script, I have an NPC who shoots out raycasts from his head in a radius to see if any players are in that radius. However, in texting the player is only detected if really close. What am I doing wrong?
local fov = 20
local dist = 100
local monster = script.Parent.Head
function MakeRay(startPos, endPos, show)
local ray = Ray.new(startPos, endPos)
local obj, endPoint = workspace:FindPartOnRay(ray)
if show then
local rayPart = Instance.new("Part", workspace)
rayPart.Name = "RayPart"
rayPart.BrickColor = BrickColor.new("Bright red")
rayPart.Transparency = 0.5
rayPart.Anchored = true
rayPart.CanCollide = false
rayPart.TopSurface = Enum.SurfaceType.Smooth
rayPart.BottomSurface = Enum.SurfaceType.Smooth
rayPart.formFactor = Enum.FormFactor.Custom
rayPart.Size = Vector3.new(0.2, 0.2, dist)
rayPart.CFrame = CFrame.new(startPos, Vector3.new(endPos.x,1.5,endPos.z)) * CFrame.new(0, 0, -dist/2)
game.Debris:AddItem(rayPart, 0.1)
end
return obj
end
while true do
local start = -180-(fov/2)
for t = start, start+fov, 5 do
local target = monster.CFrame
* CFrame.new(dist*math.sin(math.rad(t)), monster.Position.Y, dist*math.cos(math.rad(t)))
* monster.CFrame.lookVector
local hit = MakeRay(monster.CFrame.p, target, true)
if hit then
if hit.Parent:FindFirstChild("Humanoid") then
print("Humanoid in sight")
end
end
end
game:GetService("RunService").RenderStepped:wait()
end
Thanks!

1 comment
P.S. The while wait(5) loop MIGHT take some CPU usage so, if you want, use other method. Hope I helped.