i’m working on an fps project where players control their firearms through their viewmodel on the client side,
back then the model keeps snapping to different positions as you move around and that appeared to be lame so i wanted to keep it moving, transitioning smoothly between different stances
i tried using lerp and it worked great (with slight stuttering) and it also gave me the swaying motion which i also wanted to implement but hadn’t known how, however setting its cframe to lerp gives this really weird glitch that happens for the first few to 10+ seconds and then works as intended afterwards
looking for solutions was difficult as i didnt know why it was this way so i came to the devforum after asking manywhere
tldr: title
this is a snippet of the script which handles the viewmodel:
local repStorage = game:GetService('ReplicatedStorage')
local runService = game:GetService('RunService')
local gunAnims = repStorage.GunAnims
local modules = repStorage.Modules
local firearmsFolder = game.ReplicatedStorage.Guns
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local vm = char:WaitForChild('Viewmodel')
local hum:Humanoid = char.Humanoid
local fakeHum:Humanoid = vm.Humanoid
local Viewmodel = {
Player = plr,
Model = vm,
Sprinting = false,
BobData = {
BobCounter = 0,
BobAmount = Vector3.new(0.15, 0.075, 0),
BobSpeed = 6
}
}
Viewmodel['Viewmodel Camera Handler'] = runService:BindToRenderStep('ViewmodelCam', Enum.RenderPriority.Camera.Value + 1, function(dT)
local fakeCam:BasePart = vm.FakeCamera
local cam = workspace.CurrentCamera
local Sprinting = Viewmodel.Sprinting
local BobData = Viewmodel.BobData
local Amount = BobData.BobAmount
local Speed = BobData.BobSpeed
for _, part in vm:GetDescendants() do
if part:IsA("BasePart") then
part.LocalTransparencyModifier = 0
end
end
vm["Left Arm"].CanCollide = false
vm["Right Arm"].CanCollide = false
if hum.MoveDirection.Magnitude > 0 then
local baseCFrame = CFrame.new(0, 0, 0)
local moveMagnitude = hum.MoveDirection.Magnitude
BobData.BobCounter = BobData.BobCounter + moveMagnitude * dT * Speed
local bobX = math.sin(BobData.BobCounter) * Amount.X
local bobY = math.sin(BobData.BobCounter * 2) * Amount.Y
local bobOffset = fakeCam.CFrame:VectorToWorldSpace(Vector3.new(bobX, bobY, 0))
local sprintOffset = Vector3.new(0, -0.4, 0)
local bobCFrame = cam.CFrame + bobOffset
local bobSprintCFrame = cam.CFrame + bobOffset + cam.CFrame:VectorToWorldSpace(sprintOffset)
if hum.WalkSpeed <= 18 then
fakeCam.CFrame = (fakeCam.CFrame:Lerp(bobCFrame, 0.4))
else
fakeCam.CFrame = (fakeCam.CFrame:Lerp(bobSprintCFrame, 0.4))
end
else
fakeCam.CFrame = (fakeCam.CFrame:Lerp(cam.CFrame, 0.4))
end
end)
the youtube video below shows the glitch…