Mouse part tween jitter

Hello, im making a mouse like part that is tweened and stays infront of the players view.

It acts as a mouse cause theres a billboard gui inside and it tweens all good but, i want to know if theres a fix so the part doesnt jitter or move to the movement of the players position when moving.

Thank you!

local script:

wait(1)

game:GetService("UserInputService").MouseIconEnabled = false

local ts = game:GetService("TweenService")
local c_cursorBlock = game:GetService("ReplicatedStorage"):WaitForChild("_aimBlock"):Clone()
c_cursorBlock.Parent = workspace
c_cursorBlock.Anchored = true

local inf = TweenInfo.new(
	0.1,
	Enum.EasingStyle.Cubic,
	Enum.EasingDirection.Out
)

game:GetService("RunService").RenderStepped:Connect(function()
	local camera = workspace.CurrentCamera
	local pos = camera.CFrame + camera.CFrame.LookVector * 3
	
	
	local tween = ts:Create(
		c_cursorBlock,
		inf,
		{ CFrame = pos }
	)
	tween:Play()
end)

video:

1 Like

You have the mouse part being tweened which takes 0.1 seconds, so the mouse part needs to finish its tween and then starts a new one right after. So, you should figure out a way to keep things smooth but take away the wait. I believe that’s the issue but I’m not sure.

2 Likes

The root of the problem of why it’s jittering is because of the Tween; Since you’re setting a duration of 0.1 seconds for each Tween, and you’re calling that Tween to be re-run every frame, it jitters around because it doesn’t fit to the timeframe you’re setting them in, so they overlap each other, causing the jittering.
This may be work-around by using a ‘dynamic’ version of a Tween, which determines the very next position smoothly and removing the time element, so you can freely manipulate it: That’s called a lerp.
Here’s how I would apply a lerp in your case:

game:GetService("UserInputService").MouseIconEnabled = false

local c_cursorBlock = game:GetService("ReplicatedStorage"):WaitForChild("_aimBlock"):Clone()
c_cursorBlock.Parent = workspace
c_cursorBlock.Anchored = true

-- Experiment with lerpAlpha until desired results.
local lerpAlpha = 0.2 -- It should be within 0 and 1 for it to work properly.

game:GetService("RunService").RenderStepped:Connect(function()
	local camera = workspace.CurrentCamera
	local pos = camera.CFrame + camera.CFrame.LookVector * 3

	c_cursorBlock.CFrame = c_cursorBlock.CFrame:Lerp(pos, lerpAlpha)
end)

Hope that helps.

1 Like

Hello and thank you for responding, i meant it so when the player moves the mouse part it moves to the direction of the movement, i want to remove that. The tweening isnt a problem and the .1 wait is intentional its just that i want the part to basically only tween the rotation and not the movement.

2 Likes

Also im no expert on lerping so it definitely made it smoother thanks : )

3 Likes

So, when you move your character you want the mouse to stay completely still on your screen?

I’m unsure how to actually do this, you could ig detect when the player is moving and apply a offset of some sort, or disable the actual movement of the mouse.

2 Likes

I meant this:

1 Like

Is it possible to remove that “movement placement” ?

1 Like

Its a part with a billboard gui attached to it.

1 Like

Then I would honestly do @safeLast120’s suggestion + adjust the position equation a bit

local lerpAlpha = 0.2 

RunService.RenderStepped:Connect(function()
    local camera = workspace.CurrentCamera
    local pos = camera.CFrame.Position + camera.CFrame.LookVector * 3

    c_cursorBlock.CFrame = c_cursorBlock.Position:Lerp(pos, lerpAlpha)
end)

Does this work? Just curious?
(Yes Vector3’s have a lerp method)

1 Like

The outcome is the same as @safeLast120 provided.

ps: your code has alil bit of errors :upside_down_face:

1 Like

Oh shoot yes, I was trying to assign a Vector3 to a CFrame, that’s on me :sweat_smile:

c_cursorBlock.Position = c_cursorBlock.Position:Lerp(pos, lerpAlpha)

Is this the only code that edits the billboard UI? and honestly, it might be better to use 2D UI in this case.

1 Like

Ohh, yeah, that can be done as well, although it would be much easier to do with pure UI, as WizulousThe2nd stated.
Here’s my best attempt at it; It’s not perfect, it can be refined, but it’s closer to what you want (check the comments in the code to understand what I changed):

wait(1)

game:GetService("UserInputService").MouseIconEnabled = false

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local HRP = character:WaitForChild("HumanoidRootPart")
local head = character:WaitForChild("Head") -- or the camera subject part.

local camera = workspace.CurrentCamera

local c_cursorBlock = game:GetService("ReplicatedStorage"):WaitForChild("_aimBlock"):Clone()
c_cursorBlock.Parent = workspace
c_cursorBlock.Anchored = true

-- Experiment with lerpAlpha until desired results.
local lerpAlpha = 0.2 -- It should be within 0 and 1 for it to work properly.

local pastHRPPos = HRP.Position
local pastCursorPos = c_cursorBlock.Position
game:GetService("RunService").RenderStepped:Connect(function()
	local pos = camera.CFrame + camera.CFrame.LookVector * 3

	local newHRPPos = HRP.Position
	local magHRP = (newHRPPos - pastHRPPos).Magnitude
	if magHRP > 0.1 then -- movement is not only rotational...
		-- ... so compensate the non-rotational factor with the HRP difference in movement by extrapolating its LookVector.
		pos += HRP.CFrame.LookVector
	end

	c_cursorBlock.CFrame = c_cursorBlock.CFrame:Lerp(pos, lerpAlpha)
	
	pastHRPPos = newHRPPos
	pastCursorPos = c_cursorBlock.Position
end)

Hope this helps.

1 Like

I mean it still does the “movement placement” but the part with the ui was an idea for when the mouse hovers over a part with a certain name it goes to it and retreats after the mouse stops hovering over like a interact thingy.

I will link the test file if any of you want to help me on this, it will be really appreciated cause im stuck on this for 2 days and im kind of hitting a roadblock here.

mousePart_test.rbxl (63.1 KB)

If anyone finds a solution please reply to the post, ill always be available : )

Thank you!

2 Likes

I dont really touch ui stuff, cause of my lack of knowledge and cause its really buggy sometimes.
I would use it but since like 2023 when i first tried it, it never worked in my favor.