# Raycast in a 360 degree angle

Hello developers I’ve only ever raycasted in straight lines before and was wondering how I would go about raycasting in a circle. Does anybody know a way to achieve this?

2 Likes

What I can think of is block casts, sphere casts (which would cause less lag than firing 360 rays),but maybe you can do a for i = 1, 360 loop, then maybe fire a raycast with a cframe.angles() using the “i” value

I kinda suck at explaining, so heres what I mean:

``````for i = 1, 360 do
local direction = lookvector * CFrame.Angles(0, (i / i), 0) -- divide i by i to make it always be "1"
end
``````

or, you could make a block cast that would hover over an area, however, it wouldnt be a circle

2 Likes

You can perform multiple raycasts at different angles around a central point.

example code:

``````local centerPoint = Vector3.new(0, 5, 0)
local radius = 10
local numerOfRays = 16

for i = 1, numerOfRays do
local angle = math.pi * 2 * (i / numerOfRays )
local direction = Vector3.new(math.cos(angle), 0, math.sin(angle))
local raycastResult = workspace:Raycast(centerPoint, direction * radius)

if raycastResult then
local hitPart = raycastResult.Instance
print("Raycast hit:", hitPart.Name)
end
end
``````
2 Likes

Even tho the solution was given, a Sphere cast would do the job.

3 Likes

I’ll try that I just finished the basics of what i am making

As the 1st guy said, it might create less lag since it’s made for that. Instead of creating a line it basically creates a Sphere. You can get more information of it here : Introducing Shapecasts

2 Likes

Yeah I like theses better thank you all for the help

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.