How can I make a swaying viewmodel?

Hi devforum,

I’ve been trying to create realistic swaying on my viewmodel for the past few days, but I can’t figure out how.
At the moment I’m using this code:

workspace.CurrentCamera:GetPropertyChangedSignal("CFrame"):Connect(function()
	game.TweenService:Create(viewModel.CFrameValue, tweenInfo, {Value = workspace.CurrentCamera.CFrame}):Play()
	viewModel:SetPrimaryPartCFrame(viewModel.CFrameValue.Value)
end)

But for whatever reason it goes under the ground whenever I’m not moving.
I really want to make some realistic swaying so my game can look a bit more realistic.

Thanks to anyone who helps.

2 Likes
local swayEffect=.7 -- the more the slower
local camera=workspace.CurrentCamera
local swayCFrame=CFrame.new()
local lastCamCFrame=CFrame.new()
local offset=CFrame.new(0,-.5,0) -- not necessary

workspace.CurrentCamera:GetPropertyChangedSignal("CFrame"):Connect(function()


local rotation=camera.CFrame:ToObjectSpace(lastCamCFrame)
local X,Y,Z=rotation:ToOrientation()

swayCFrame=swayCFrame:Lerp(CFrame.Angles(math.sin(X)*swayEffect,math.sin(Y)*swayEffect,0),.1) --

lastCamCFrame=camera.CFrame

 viewModel:SetPrimaryPartCFrame(camera.CFrame *swayCFrame*offset)

end)

very clever idea with

workspace.CurrentCamera:GetPropertyChangedSignal("CFrame"):Connect(function()
4 Likes

Thanks so much!
Is there any chance that you know how to make it not do this with unanchored objects though?

1 Like

This script should not affect it in any way, but why aren’t they anchored? Do you want to make weapon animations through scripts adjusting positions? But it’s easier through animations.
You can only make anchored ‘HumanoidRootPart’ and the rest just rig right. And make sure you turn off ‘CanCollide’ property absolutely in all parts of the model

2 Likes