Raycasting lagging behind?

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)

I am assuming its due to u casting the ray on the server, there is a delay between what you see and the server sees.

To work around this, cast the ray on both the server and the client, the ray on the client wont do anything but be for show, the server ray will then be sent via another remote event, but do not send it to the player who casted it, so for everyone else on the server it looks fine, including you!

The raycasts are done only on the client.

So there is no server component whatsoever?

The only server component is the IKControls that make the arms hold the gun and the welding (I tested making those on the client and same thing happens)

1 Like

Is there any reason you are using RenderStepped, specifically? And can you try using heartbeat just to see what will happen?

I’ll try running them in two different threads. Maybe that for loop inside the RenderStepped makes it yield, making the rays look Iike they are lagging behind.