360 Degrees Raycasting Detection

I was wonder how do you make a ray-casting object detect all angles of it like a sensor. It keeps going in one direction forward and I want it to be 360 like around all of the part.

while true do
	wait()
	local Raycast = Ray.new(script.Parent.CFrame.p, script.Parent.CFrame.LookVector * 500, script.Parent.CFrame.UpVector * 500, script.Parent.CFrame.RightVector * 500)
	local Part, Position = game.Workspace:FindPartOnRayWithIgnoreList(Raycast, {script.Parent}, false, true)

	print(Part)
end

Yeah well, I don’t know what your point is for a 360 degrees raycasting detection, but it won’t still work
you will need thousands of raycasts depending on the distance.

So 360 isn’t enough.
example of what would happen:

red = raycasts

all of the spots you will leave without detection depending on the distance won’t really work.
(not to mention the tons of raycasts you’ll need.)

So if you want to do a sensor I would use distance or lookvectors to determine if something is in sight of the part.

1 Like

I would recommend a large part around the object and then just detect when objects touch the part using .Touched

But if you really wanna go for it.
I mean… sure?

local part = workspace.Sensor
local angle = 0;
local params = RaycastParams.new()
params.FilterDescendantsInstances = {part}
for i = 1,360,1 do
	angle = i;
	local startCFrame = part.CFrame * CFrame.Angles(0,math.rad(angle),0)
	local Raycast = workspace:Raycast(part.Position,startCFrame.LookVector.Unit * 200,params)
	if (Raycast) then
		-- stuff here
		print("detected",Raycast.Instance)
	end
end

don’t forget:

params.FilterType = Enum.RaycastFilterType.BlackList --or Whitelist depending on what you want ignored or detected

it’s not needed, blacklist is already pre-built selection for RaycastParams.new()

If you want 360-like spherical 360, then a raycast would not be accurate or effective, try:

local sensor = workspace:WaitForChild("Sensor")
local sensed_parts = {}

for _, v in pairs(workspace:GetChildren()) do
  if v:IsA("BasePart") then
    if (v.Position - sensor.Position).Magnitude <= 200 --[[ this is the radius of studs the sensor will detect in, make this a constant variable --]] then
      table.insert(sensed_parts, v)
    end
  elseif v:IsA("Model") then
    if v.PrimaryPart then
      if (v.Position - sensor.Position).Magnitude <= 200 --[[ this is the radius of studs the sensor will detect in, make this a constant variable --]] then
        table.insert(sensed_parts, v)
      end
    end
  end
end

print(sensed_parts)

or:

if (v.Position - sensor.Position).Magnitude <= 200 then
  sensed_parts[v] = true
else
  sensed_parts[v] = false
end
2 Likes

thanks, I learnt something new :smiley: !