Raycast breaks through walls

Hello everyone.
I came across such a strange glitch.
I am trying to create a realistic laser that does not pass through obstacles and highlights them.
I use Raycast and Beam for this.
The End Attachment Beam is calculated by the result returned by Raycast.
Here is a simple code:

local RunService = game:GetService("RunService")
local laserGunPart = script.Parent

local visualBegin = laserGunPart.StartAttachment
local visualEnd = laserGunPart.EndtAttachment
local rcParams = RaycastParams.new()
rcParams.FilterDescendantsInstances = {laserGunPart}
rcParams.FilterType = Enum.RaycastFilterType.Exclude
rcParams.IgnoreWater = true


local function onHeartbeat()

	local rayOrigin = laserGunPart.Position
	local rayDirection = (laserGunPart.CFrame.LookVector * -150)

	local raycastResult = workspace:Raycast(rayOrigin, rayDirection, rcParams)

	if raycastResult then
		visualEnd.Position = Vector3.new(0, 0, raycastResult.Distance)
		--visualEnd.WorldPosition = raycastResult.Position
		else print("!")
	end
end


RunService.Heartbeat:Connect(onHeartbeat)

However, it is periodically seen how the laser passes through the walls.
Moreover, this is clearly visible only in normal mode, when a player is present at the level.
I am attaching an example of a test level.
PlaceLaser.rbxl (81.8 KB)
PlaceLaser.rbxl (81.2 KB)

3 Likes

I don’t understand your issue completely but if you’re making some sort of gun fire the raycast from the humanoidrootpart to the mouse. This would stop it from going through walls. Also ensure that the obstacles have canquery on.

1 Like

To understand my problem, run the last test level in the attachment in the studio. The laser rotates randomly in the room and periodically you can see how it passes through the walls.

1 Like

Yeah I re-read your post and now I understand. But even the attachment rotating shouldn’t mean that it passes through parts. Maybe you could try setting the raycast on the attachment’s worldposition instead? Or ensure that canquery is off. Also there is a good reason why you’re raycasting on a heartbeat event? That can be very heavy on resources.

1 Like

try workspace:FindPartOnRayWithWhitelist or workspace:FindPartOnRayWithBlacklist

I used heartbeat because I thought the Raycast couldn’t keep up with the rotation of the laser, everything was fine with conquery, I also tried to calculate the endpoint in a different way
visualEnd.WorldPosition = raycastResult.Position
but nothing changes.

Aren’t rays deprecated though? WorldRoot | Documentation - Roblox Creator Hub

30char

they are the same as workspace:Ray, they’re just deprecated for no reason

1 Like

Yes but it’s still deprecated, you can just use raycastparams and some if statements if you want to find out which part it is.

Posted a video of the problem if you don’t want to download the test level.

I think I know the fix:

if raycastResult then
		visualEnd.Position = Vector3.new(0, 0, raycastResult.Distance)
		-- Change this line
		else print("!")
end

Change this line to this:

visualEnd.Position = raycast.Position
		-- New line
		else print("!")

You do not take into account that the endpoint is calculated for the Attachment, the attachment coordinates have only the distance from the beginning.
If you apply your code, the beam will show you who knows where.

well maybe u could try using raycast.Position somewhere I guess.

I have already told you that I tried to use the following code

visualEnd.WorldPosition = raycastResult.Position

but it doesn’t change anything at all.

I’ve downloaded your test place and made the script place a marker at every position of your end attachment. After letting the script run for a bit not a single marker was placed outside the box you built. I’m fairly certain the issue lies with the beam or the attachment and not the raycast itself. Why exactly the beam travels outside the box is something I haven’t figured out yet unfortunately.

I noticed that if you run the level on F8, then the problem is not visible. The glitch manifests itself when the player is on a level. it’s very strange.

It definitely is, I see the issue happening myself but the .WorldPosition of the attachment always stays inside the square you’ve built and aligns properly with the result of the raycast. Honestly your best bet might be to mess around with the beam to see if that’s where the problem lies. Other than that you could try moving the laser around via the same script and see if this is some odd edge case with Roblox physics. If the latter turns out to be the case you can create a bug report in the Developer Forums bug report category.

You just walked through that building, do you have respect can collide enabled on the raycast parameters, if so they WILL go through.

This isn’t true. When RespectCanCollide is set to true the CanCollide property is used over the default CanQuery one. All his parts have the .CanQuery property set to true so this should not be a problem.

No, RespectCanCollide detects CollisionsEnabled, not CanQuery, that just removes the part from being touched at all even if it’s on or off in the parameters.