For some reason, when I raycast something in a RunService loop I get these raycasts when I move
But when standing still it’s fine
I’m shooting the raycast from the yellow cube, which is welded on the server (shouldn’t be the problem). Anyone have idea why does this happen. It triggers me so much.
This looks like the RenderStepped just doesn’t run on the correct frames, it just seems desynchronized.
My code:
local function visualizeRay(origin, ray)
local distance = ray.Distance
local rayVisualizer = Instance.new("Part")
rayVisualizer.Anchored = true
rayVisualizer.CanCollide = false
rayVisualizer.CanQuery = false
rayVisualizer.CanTouch = false
rayVisualizer.Transparency = 1
rayVisualizer.CastShadow = false
rayVisualizer.Size = Vector3.new(0.1, 0.1, distance)
rayVisualizer.CFrame = CFrame.lookAt(origin, ray.Position) * CFrame.new(0, 0, -distance / 2)
local attachment0 = Instance.new("Attachment")
attachment0.Parent = rayVisualizer
attachment0.CFrame = CFrame.new(0, 0, rayVisualizer.Size.Z / 2)
local attachment1 = Instance.new("Attachment")
attachment1.Parent = rayVisualizer
attachment1.CFrame = CFrame.new(0, 0, -rayVisualizer.Size.Z / 2)
local beam = Instance.new("Beam")
beam.Attachment0 = attachment1
beam.Attachment1 = attachment0
beam.TextureSpeed = 0
beam.TextureMode = Enum.TextureMode.Stretch
beam.LightInfluence = 0
beam.Transparency = NumberSequence.new(0.35)
beam.Width0 = 0.155
beam.Width1 = 0.155
beam.FaceCamera = true
beam.Parent = rayVisualizer
rayVisualizer.Parent = rayPreviewFolder
end
local rayParams = RaycastParams.new()
rayParams.FilterDescendantsInstances = { character }
rayParams.FilterType = Enum.RaycastFilterType.Exclude
rayParams.IgnoreWater = true
local function raycast(origin, normal)
local ray = workspace:Raycast(origin, normal * 500, rayParams)
if ray then
visualizeRay(origin, ray)
local reflectedNormal = normal - (2 * normal:Dot(ray.Normal) * ray.Normal)
raycast(ray.Position, reflectedNormal)
end
end
local updateRayPreview = nil
tool.Equipped:Connect(function()
updateRayPreview = runService.RenderStepped:Connect(function()
for _, part in pairs(rayPreviewFolder:GetChildren()) do
part:Destroy()
end
--// rayDirection = rayDestination − rayOrigin
raycast(rayStart.CFrame.Position, (mouseHit.Position - rayStart.Position).Unit)
end)
end)
tool.Unequipped:Connect(function()
updateRayPreview:Disconnect()
end)