Hello, I am working on a weapons system for a game and I want to create a sway movement for the viewmodel to make it look more natural and fluid, rather then just having it glued to the screen. This is what I have so far:
And this works, but theres a plethora of problems I need to solve, and its not efficient. If I show you the code behind it your probablty going to laugh at how primitive it is:
vmStep = runService.RenderStepped:Connect(function()
task.delay(.03, function()
viewModel:SetPrimaryPartCFrame(cam.CFrame * CFrame.new(0,0,0))
end)
end)
I have tried looking for solutions on ther dozens of other posts similiar to mine, and they usually entail having a seperate module script to call for not only the sway, but for different viewmodel motions as well. The end result normally doesnt look very nice, not to mention I dont understand what their 200+ line code is even doing.
Any help is greatly appreciated, thank you.
2 Likes
In your loop
viewModel:PivotTo(viewModel:GetPivot():Lerp(cam.CFrame, dt * 10))
1 Like
You could maybe try using :Lerp() just like @SubtotalAnt8185 showed. But to make it better you could probably tween it. I haven’t tested it but it would probably look smoother
Why is there a task.delay
?
You can quite of do the same thing but much more better with this code below:
vmStep = runService:BindToRenderStep("viewmodel", Enum.RenderPriority.Camera.Value - 1, function(dt)
local lastcf = viewModel:GetPivot()
local cf = cam.CFrame * CFrame.identity -- CFrame.identity is basically CFrame.new(0, 0, 0)
local target = lastcf:Lerp(cf, 0.3)-- the higher the number, the snappier it looks
-- the lower the smoother and slower
viewModel:PivotTo(target)
end)
Change the 0.3 to something that multiplies dt
because on lower framerates it will go slower.
You and SubtotalAnt’s code edits work, however theres still a few unresolved issues, and it still looks slightly choppy.
I only want the viewmodel to sway when you turn your camera, I dont want it to move when your walking, jumping, or doing other actions. It doesnt look nice, and is also dependent on movement factors such as how fast the character walks, how high it jumps, etc.
Pardon me but I forgot that you should snap the viewmodel to the Camera’s CFrame
Replace target
with cam.CFrame * lastcf:Lerp(cf, 0.3)
and remove the cam.CFrame
on the cf
variable
1 Like