Raycasts rarely phasing through objects

I’ve noticed that ~1/50 raycasts just pretend that objects don’t exist.

I’ve tried shapecasts, they also suffer the problem.

No matter what I try, I cannot have raycasts consistently detect walls. I can reduce the amount of times they just pass through walls, but when I perform a large amount of raycasts, some of them ALWAYS miss. This is a huge problem, and I can’t find anybody else talking about it, or any potential solutions.

I used this code to verify the problem i’m having, it might be something i’m doing?

local part:BasePart = Instance.new("Part")
part.Position = Vector3.new(0,-100,0)
part.Anchored = true
part.CanQuery = false
part.CanTouch = false
part.EnableFluidForces = false
part.CanCollide = false
part.Color = Color3.fromRGB(255,0,0)


function CreateVisualRayPart()
	return part:Clone()
end



local function reflect(vector, normal)
	return -2 * vector:Dot(normal) * normal + vector;
end


function VisualiseRay(Start,Direction,Distance)


	local Params = RaycastParams.new()

	local Result = workspace:Raycast(Start,Start+(Direction*Distance),Params)

	local End = (Start + (Direction*Distance))

	local rayPart = CreateVisualRayPart()

	if Result then
		End = Result.Position or End
		rayPart.BrickColor = BrickColor.new("Lime green")

		rayPart.Size = Vector3.new(0.05, 0.05, (Start- End).Magnitude)
		rayPart.CFrame = CFrame.new((Start + End)/2,End)
		rayPart.Material = Enum.Material.Neon
		spawn(function()
			task.wait(3)
			rayPart:Destroy()
		end)
		rayPart.Parent = workspace
		return reflect(Direction,Result.Normal),End
	else 
		rayPart.Size = Vector3.new(0.05, 0.05, (Start- End).Magnitude)
		rayPart.CFrame = CFrame.new((Start + End)/2,End)
		rayPart.Material = Enum.Material.Neon
		game.Debris:AddItem(rayPart,5)
		rayPart.Parent = workspace
	end


	return Direction,End
end




while true do task.wait()
	local LastDirection = Vector3.zero
	local Direction,End = VisualiseRay(Vector3.new(10,12,10),Vector3.new(math.random(-100,100),math.random(-100,100),math.random(-100,100)).Unit,100)
	local Bounces = 30
	while Bounces > 0 do
		Bounces -= 1
		LastDirection = Direction
		Direction,End = VisualiseRay(End,Direction,100)
	end
end

Does anybody know why this is happening, or how I can fix it? Because it’s really troubling when i’m trying to use raycasts for things like projectile systems, sound systems, or anything that needs a large number of collision checks.

I mean are you having problems during the bouncing sequences or you run it 50 times and one of the times it just dosent work completely.

If its during the bouncing sequence it might be something to do with robloxs floating point numbers that can get goofed up and maybe potentially phase a through a wall. Also any number error like if the rays position gets read as 1.00001 instead of 1 that error is gonna get amplified every bounce.

also if you’re running this constantly its many many parts every second which can cause lag and maybe make raycasts screw up, you’re also running a ton of raycasts every frame and creating the params for it. These along with robloxs affinity for bad floating point numbers is likely the issue.

Theres probably ways for work arounds something like rounding off numbers at every bounce.

Honestly though what could you possibly need this for that needs this many bounces. Ive personally never had problems like raycasts going through walls when using raycasts normally.

You didn’t normalize the direction before * it by distance. You set direction as math.random(-100,100) then you do Direction*Distance so the length is longer then what it should be the fix is to add

	Direction = Direction.Unit
	local Result = workspace:Raycast(Start, Direction * Distance, Params)

normalize the direction vector then multiply by distance

None phase

Yes, I am.

Vector3.new(math.random(-100,100),math.random(-100,100),math.random(-100,100)).Unit

This is a normalized unit vector.

Any time i’m raycasting in large batches, I notice that roughly one in fifty raycasts ignores objects.


Notice how, in my completely enclosed space, rays are somehow outside the walls? That should by all means be physically impossible.

Are you running that on the client? I don’t know what else you might be doing different, that would change the outcome. Could you break down how you got there?

My bad I misread the other issue I forgot the point out was with this line here

local Result = workspace:Raycast(Start,Start+(Direction*Distance),Params)

typically when you raycast you pass Origin, Direction, as a vector so with direction and magnitude which you passed here when you did Direction * Distance. But the mistake here is that you add the Start position again. The origin of the vector is already defined but you add it again

So just remove the start

local Result = workspace:Raycast(Start,(Direction*Distance),Params)

thats the fix

Thanks! I completely missed that because I was testing from the 0,0,0 origin. I should pay more attention to my code.

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