I am trying to make an fps game, but any parts following the camera twitch alot. robloxapp-20230303-2043442.wmv (2.8 MB)
How could i fix this?
The method im using is binding a function every render to set the CFrame of the gun primarypart like this:
Gun.PrimaryPart.CFrame = (workspace.CurrentCamera.CFrame * GunData["CameraOffset"]) * GunData["CameraRotation"] --The camera offset and rotation are exactly what you think.
I have tried using tweenservice, but it returns similar results.
You could attempt using lerping the pos. of the gun’s PrimaryPart towards the target pos.
Example
-- Set the target position of the gun's PrimaryPart
local targetPos = (workspace.CurrentCamera.CFrame * GunData["CameraOffset"]) * GunData["CameraRotation"]
-- Lerp the position of the gun's PrimaryPart towards the target position
local lerpAlpha = 0.2 -- Adjust this value to control the speed of the lerp
Gun.PrimaryPart.CFrame = Gun.PrimaryPart.CFrame:Lerp(targetPos, lerpAlpha)
Here, we set the CFrame directly using the Lerp() method to interpolate the position between the currentPos and the target pos. The lerpAlpha value controls how fast the position will interpolate - a lower value will result in a slower, smoother motion.
In case you still see jittering, you can knock it down a bit more. You can add smoothing by keeping track of the previous few target positions and averaging them.
Example 2
-- Set the target position of the gun's PrimaryPart
local targetPos = (workspace.CurrentCamera.CFrame * GunData["CameraOffset"]) * GunData["CameraRotation"]
-- Add the new target position to a buffer and remove the oldest one
table.insert(targetPositions, targetPos)
if #targetPositions > smoothingSize then
table.remove(targetPositions, 1)
end
-- Average the target positions to smooth out jittering
local smoothedPos = Vector3.new()
for _, pos in ipairs(targetPositions) do
smoothedPos = smoothedPos + pos
end
smoothedPos = smoothedPos / #targetPositions
-- Lerp the position of the gun's PrimaryPart towards the smoothed position
local lerpAlpha = 0.2 -- Adjust this value to control the speed of the lerp
Gun.PrimaryPart.CFrame = Gun.PrimaryPart.CFrame:Lerp(smoothedPos, lerpAlpha)
What this does is calculate the target pos of the gun’s PrimaryPart, then add a new target pos to a buffer and remove the oldest one, keeping on the most recent smoothingSize positions. We average the positions to create a smoothed position, and using the Lerp() method to interpolate the position between the current position and smoothed position.
Additionally, you can experiment with different values of lerpAlpha and smoothingSize to find the values that work best for you.
So I have tested out solution 1, and for me alpha 0.4 seems to be the best for me, I am only seeing slight twitching with rotation, but its not that bad.
I know I said that it works but uh, it didn’t really
Instead I scripted my own Camera and movement system, which is quite easy and useful in my opinion. https://gyazo.com/036242260320281ad4955b863a876b4e
(I also added camera bobbing and sprinting into the mix)
this post isnt too old to call you a necroposter, instead im going to do that myself now XD
I found a wayy better way now, instead of me using RenderStepped from RunService, I would consider using PreRender instead, because it runs before a frame is rendered, so you wont see any inbetween frames (or so, i think)
By doing that, you wont need to interpolate it to get rid of the glitchiness and it takes way less code aswell.
Do note this isnt useful for making smooth moving parts, since it doesnt interpolate this way