Making bots for my game right now. I’m trying to give them a sort of vision based entirely on raycasting, and trying to offset the lookvector so they have a large field of view. I am failing miserably.
Yes, it works but only on specific orientations, such as 0,0,0
and 0,180,0
. Other angles make the entire thing shoot rays at random places or a fixed position, seen below:
I’ve tried adjusting the ray distance, the orientations and all. No effect. I’ve tried looking around the forum for something similar to my problem, couldn’t find anything.
The code responsible for raycasting:
-- Visualizer and raycaster
function visualray(origin,result)
--ray visualizer by @incapaz
local point = Instance.new("Part",workspace)
point.Size = Vector3.new(.5,.5,.5)
point.Material = Enum.Material.Neon
point.Transparency = .5
point.Position = result
point.Shape = Enum.PartType.Ball
point.Anchored = true
point.CanCollide = false
point.CanTouch = false
point.Color = Color3.new(0,1,0)
local distance = (origin - result).Magnitude
local ray = Instance.new("Part",workspace)
ray.Material = Enum.Material.Neon
ray.Color = Color3.new(1,0,0)
ray.Transparency = .5
ray.Anchored = true
ray.CanCollide = false
ray.CanTouch = false
ray.Size = Vector3.new(0.1, 0.1, distance)
ray.CFrame = CFrame.lookAt(origin, result)*CFrame.new(0, 0, -distance/2)
game.Debris:AddItem(ray,task.wait())
game.Debris:AddItem(point,.5)
end
function raycast(direction)
local ray = workspace:Raycast(hum.RootPart.Position,direction,Rayparams)
if ray then
visualray(hum.RootPart.Position,ray.Position)
if ray.Instance then
if ray.Instance.Name == "BOT_BACKOFF" then
return "laser",(ray.Position - char.Head.Position).Magnitude
else
local possiblechar = ray.Instance:FindFirstAncestorOfClass("Model")
if possiblechar then
if possiblechar ~= workspace then
if possiblechar:FindFirstChild("Humanoid") then
local possibleplr = game.Players:GetPlayerFromCharacter(possiblechar)
if possibleplr then
if possibleplr.Team == game.Teams.Humans then
return possiblechar, (hum.RootPart.Position - possiblechar.HumanoidRootPart.Position).Magnitude
end
end
end
end
end
end
end
end
end
--Sight checker, responsible for the rays seen in the video
function sight()
for i = -90,90,5 do
local pos = (char.Torso.CFrame.LookVector * 50) + Vector3.new(i,0,0)
local result,dist = raycast(pos)
if result then
if result == "laser" and dist <= 10 then
return "laser"
else
if dist <= 15 then
return result, true
else
return result, false
end
end
end
end
end
--char is the npc, hum is it's humanoid. bot_backoff is a trigger used for preventing bots from killing themselves.
If there’s an alternative, or better way to give the bots a vision, I’d love to hear it. Thanks in advance.