Raycasts are all stacking at same position even with different orientation

So i made a script that creates 360 raycasts, with x rotation increasing with each one.

local params = RaycastParams.new()

for i=1,360 do
	params.FilterDescendantsInstances = workspace.Debris:GetChildren()
	
	local raycast = workspace:Raycast(workspace.RaycastOrigin.Position, Vector3.new(i,0,0), params)
	
	if raycast == nil then continue end
	
	local hitvis = Instance.new("Part")
	
	hitvis.Shape = Enum.PartType.Ball
	hitvis.Size = Vector3.new(1,1,1)
	hitvis.Position = raycast.Position
	hitvis.Anchored = true
	hitvis.Parent = workspace.Debris
end

For some reason, all the hit visualisers, the spheres, are stacking at the same place. Any help?

1 Like

EDIT: nvm ignore this…

The raycasts are probably hitting the hit visualizer parts.
Try disabling their raycast collision:

hitvis.CanCollide = false
hitvis.CanQuery = false

Raycasts cast from the first Vector3 to the second Vector3.
What your code is doing is simply making the end point move 1 stud on the X axis 360 times, but its staying in the same core point, and not rotating, just changing where the raycast is pointing to.

Raycasts DO NOT use rotation.

Here is a visual example of what your code is doing:


Red = raycast end points.
Yellow = Raycast origin.

As for why they’re stacking in the same point, it could be that you haven’t set the params.FilterType to Ignore.

The second argument to workspace:Raycast isn’t an orientation, its a direction vector. You aren’t going in a 360 circle by incrementing the X by 1, you’re just raycast farther and farther in the X direction by i. If you want it to raycast 360 you should do something like this:

local DIST = 10 -- How far you want each raycast to travel in studs

local params = RaycastParams.new()

for i=1,360 do
	local x = math.cos(math.rad(i))
	local z = math.sin(math.rad(i))
	local dirVec = Vector3.new(x,0,z) * DIST,

	params.FilterDescendantsInstances = workspace.Debris:GetChildren()
	
	local raycast = workspace:Raycast(workspace.RaycastOrigin.Position, dirVec, params)
	
	if raycast == nil then continue end
	
	local hitvis = Instance.new("Part")
	
	hitvis.Shape = Enum.PartType.Ball
	hitvis.Size = Vector3.new(1,1,1)
	hitvis.Position = raycast.Position
	hitvis.Anchored = true
	hitvis.Parent = workspace.Debris
end
1 Like

Vector3.new(i, 0, 0) is always pointing straight +X.
Only the length of the ray changes each loop, not its direction, so every ray hits the same spot and all balls stack there.

Make the direction a unit vector that actually spins around Y, then multiply by your max distance:

local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = {workspace.Debris}

local origin = workspace.RaycastOrigin.Position
local range  = 200 

for deg = 0, 359 do
	local rad = math.rad(deg)
	local dir = Vector3.new(math.cos(rad), 0, math.sin(rad)) * range

	local result = workspace:Raycast(origin, dir, params)
	if result then
		local hitvis = Instance.new("Part")
		hitvis.Shape   = Enum.PartType.Ball
		hitvis.Size    = Vector3.new(1,1,1)
		hitvis.CFrame  = CFrame.new(result.Position)
		hitvis.Anchored = true
		hitvis.Parent  = workspace.Debris
	end
end

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