Part that follows the mouse jitters when moving the camera

Hello, I am working in an object placement system and I have been having some issues regarding the preview of where the object should be placed.

I use the Spring Module from Nevermore Engine to make the preview part move more smoothly, however when the camera moves, the preview part starts jittering, you can see an example in the video below.

This is how it should always move:

This is what happens when the camera moves:

This is the code I am using, located under Starter Player Scripts.

local Placer = {}

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")

local Spring = require(ReplicatedStorage.ModuleLoader.Utils.Spring)
local Find = require(ReplicatedStorage.ModuleLoader.Find) --Used to get instances safely
local Trove = require(ReplicatedStorage.ModuleLoader.Utils.Trove) --Used for cleanup

local stopPreview

local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera

local previewPart: Model = Find(script, "Preview")
local previewPartOrientation = previewPart.PrimaryPart.Orientation

local cfAngles = CFrame.Angles(math.rad(previewPartOrientation.X), math.rad(previewPartOrientation.Y), math.rad(previewPartOrientation.Z))

local function mouseRaycast(distance: number, blacklist: {})
	local mousePosition = UserInputService:GetMouseLocation()

	local mouseRay = camera:ViewportPointToRay(mousePosition.X, mousePosition.Y)

	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	raycastParams.FilterDescendantsInstances = blacklist

	local raycastResult = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * distance, raycastParams)

	return raycastResult
end

local function calculatePosition(blacklist: {})	
	local result = mouseRaycast(500, blacklist)
	
	if result and result.Instance then		
		return result.Position	
	else
		return Vector3.zero
	end
end

local function startPreview(character: Model)
	local previewTrove = Trove.new()
	
	local previewClone = previewTrove:Clone(previewPart)
	previewClone.Parent = workspace
	
	local previewSpring = Spring.new(calculatePosition({previewClone, character}))
	previewSpring.Speed = 25
	previewSpring.Damping = 0.8
	
	previewTrove:BindToRenderStep("positionPreview", Enum.RenderPriority.Last.Value , function(deltatime)
		
		local currentPosition = calculatePosition({previewClone, character})
		previewSpring.Target = currentPosition

		local cframe = CFrame.new(previewSpring.Position) * cfAngles
		previewClone:PivotTo(cframe)
		
	end)

	return previewTrove:WrapClean()
end

--Just for testing purposes
player.CharacterAdded:Connect(function(character)
	startPreview(character)
end)

return Placer

I have tried using :Lerp instead to fix the issue but the result was the same. I have also tried other render priorities and other events such as Heartbeat and Prerender but it always gave the same result.

If anyone knows what may be causing the problem, any help will be appreciated.

1 Like

Use :BindToRenderStep() and try either Character priority or Camera Priority

I have tried that, however the issue still persists.