Raycast in all angles/directions? HELP


As shown in this clip, the raycast only works for the left and front direction, how would I make it work for all directions?

Script :

local Part = script.Parent
local HitBox = Instance.new("Part")
local RP = RaycastParams.new()
local FilterDescendantsInstances = { HitBox }
RP.FilterType = Enum.RaycastFilterType.Exclude
RP.FilterDescendantsInstances = FilterDescendantsInstances

while true do
	local RayDirection = {
		Part.CFrame.LookVector,
		-Part.CFrame.LookVector,
		Part.CFrame.RightVector,
		-Part.CFrame.RightVector
	}
	HitBox.Anchored = true
	for i, v in pairs(RayDirection) do
		local Raycast = workspace:Raycast(Part.Position, v * 20, RP)
		if Raycast then
			local Distance = (Part.Position - Raycast.Position).Magnitude
			
			HitBox.CanCollide = false
			HitBox.Position = Part.Position
			HitBox.Size = Vector3.new(1, 1, Distance)
			HitBox.CFrame = CFrame.new(Part.Position+(RayDirection[i] * (Distance/2)), Raycast.Instance.Position)
			HitBox.Parent = workspace
			print(Raycast.Instance)
		end
	end
	wait(0.250)
end

3 Likes

Hi there. Are you trying to get the part to cast a ray not confined to specific directions? You want a million rays cast in all possible directions?


The blue line is basically the direction I want (Red one is what’s currently available)

1 Like

I see. Sorry, I don’t know if that’s possible or reasonable. Are you using the ray cast to lock on to a player? Otherwise you might be better off just using (Part.Position - playerHumanoidRootPart.Position).Magnitude

I think to do it I need to make it cast a random ray in a random direction a lot of times, but I’m not sure how to do so

1 Like

I’m making it lock on to random stuff

For this purpose you can make a hitbox around a part and use workspace:GetPartsInPart(). You can learn more about it in the docs.

ooo! I really like that idea. And @1ocaluser could use a sphere shape to kind of imitate the behavior of raycasts.

what about workspace:GetPartBoundsInRadius, would that work?

Yes indeed. For raycasts though, I would do is this:

for i = 1,360 do
	local x,z = math.sin(math.rad(i)),math.cos(math.rad(i)) -- I made vectors from angles using sin and cosine.
	local ray = workspace:RayCast(Position,Vector3.new(x,0,z)*8,RP) -- Then I apply.
end
1 Like

Ah yes that would also work. I typed that message on mobile so I wasn’t able to say much as I suck with mobiles.

I’ve tried this :

local RayDirection ={}
	HitBox.Anchored = true
	for i = 1, 24 do
		local RNG = Vector3.new(math.random(0, 360), 4, math.random(0, 360))
		table.insert(RayDirection, RNG)
	end

Though it doesn’t work very well

It won’t as it’s quite an impracitcal method you’re doing. Try mine.

raycasting can be expensive, doing that is bad practice, better methood is using bounding box, then if it will detect any object shoot one ray to it to check if there’s any obstacle, you can use magnitude too