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)
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.
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)
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.
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.
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)
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.
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.