RayCasting in a circle

This is sort of hard to explain, so let me try my best.

What I’m attempting to achieve is a system that raycasts a circle with a vector as the starting position, here is a picture of what I want in an over head view

I was attempting to use a for loop,

for i = 1, 360, 10 do

end

but I had no real luck with that, if anyone could help it’d be greatly appreciated!

4 Likes

Hi there, I think you should take a look at this: Circle and ellipse drawing algorithm - Math Open Reference

I took a look, I came up with this…

local Part = game:GetService("Workspace"):WaitForChild("CenterPart")

local h, k = Part.Position.X, Part.Position.Z

for i = 1, 360, 10 do
	x = h + 10 * math.cos(i)
	z = k + 10 * math.cos(i)
	
	local part = Instance.new("Part")
	part.Anchored = true
	part.Size = Vector3.new(1, 1, 1)
	part.Position = Vector3.new(x, Part.Position.Y, z)
	part.Parent = workspace
	part.BrickColor = BrickColor.new("Really red")
end

It resulted in this weird line.

  • You have to use radians when using math.cos or math.sin, so just put i in math.rad()

  • You have to use math.sin for x and math.cos for z, or vice versa, doesn’t really matter. You used the same function for both x and z, so you essentially made x and z equal.

Not much else needs to be done, I would think.

1 Like

It still has strange clumps though, is this due to the loop I’m using?

1 Like

As I said before, you have to use math.rad to convert i to radians:

    x = h + 10 * math.cos(math.rad(i))
    z = k + 10 * math.sin(math.rad(i))
5 Likes

Quick note about your loop: it will go 1, 11, 21, …, 351. I imagine you probably didn’t intend it to work like that (although it will do the job just fine).

You can fix it like this:

for i = 10, 360, 10 do
  -- loop body
end
1 Like

This does beg the question… why are you doing this? Raycasting in the circle is generally a bad idea because there are better ways to do it. Is this for an explosion?

probably related to this thread, from the same OP.

I’ve made a place for you to check out, how I’d do it. It’s quite satisfying too watch.
Example: https://gyazo.com/04eb91cf47db63d225130d7c9ef7c1e8 Place: Circular Raycasting - Roblox

1 Like

I fixed some issues, here’s a new preview: https://gyazo.com/33317650d6f3de96b99d1a3f1f3d0c3e