Ray Cast paired to mouse drifts

I am working to create a script that will fire and visualize a ray for a player based on where they are pointing their mouse.

The issue I’m facing, and struggling to resolve is that the ray slowly drifts from where the mouse is pointing at an object, to the actual location of the mouse of screen (see video),

I have attempted to fix this by simply detecting if the mouse is stationary and not recalculating the ray, but through print statements the code seems to think the mouse IS moving even when its not which is probably why the ray is moving

This takes place in a Local script, any help would be HUGELY appreciated

RunService.Heartbeat:Connect(function(deltaTime)
	if not weaponEquipped or not rayPart then return end

	local mousePos = mouse.Hit.p

	print("Mouse Position:", mousePos)

	if lastMousePos and (mousePos - lastMousePos).magnitude < 0.01 then
		return
	end

	lastMousePos = mousePos

	local handlePos = Handle.Position

	local direction = originalDirection or (mousePos - handlePos).unit
	originalDirection = nil 

	local distance = (mousePos - handlePos).magnitude

	rayPart.CFrame = CFrame.new(handlePos:Lerp(mousePos, 0.5), mousePos)
	rayPart.Size = Vector3.new(0.1, 0.1, distance)
end)

Try setting the raypart can collide to false and canquery to false. The mouse is probably hitting the part.

1 Like

I didn’t think it would be that easy, tysm

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