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)
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.
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.
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.
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.
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.
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.
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.