Help with viewmodel sway

So I was making a framework and I ran into something.

I don’t know how to track a camera’s movement!

I am trying to make the viewmodel turn the opposite way the velocity is going

Edit*
I’m trying to make it move on the Z axis when it sways

User RunService to track every framerate, and then use the :SetPrimaryPartCFrame prorpety of your model, and put inside brackets your camera CFrame so it could be :

game:GetService('RunService').RenderStepped:Connect(function(dt)
Viewmodel:SetPrimaryPartCFrame(camera.CFrame)
end)

In parenthesis you can add offsets like shooting…

I’ll try this solution and see if it works.

For the sway effect it is a bit trickier since you will have to get Camera CFrame Orientation Components

This just sets the viewmodel’s CFrame to the camera.

Thats what you asked didn’t you ?

I want it to move on the Z axis. Read it.

Oops didn’t mean it that way sorry.

If you want to offset it on Z axis or other axis, just add this :

SetPrimarypartCFrame(camera.CFrame * CFrame.new(0, 0, 6)) -- will add 6 studs to Z axis
--You can also add Angles : 
SetPrimarypartCFrame(camera.CFrame * CFrame.Angles(math.rad(90), 0, 0) -- Will turn it 90 degrees on X axis

How do I do this with sway like detecting the x axis velocity?

Applying CFrame Sway to your model takes what i did eariler, although it requires a bit of maths.

So first of all, declare a variable called ‘LastCameraCFrame’ , ‘SwayCF’ and ‘Intensity’ that represents what they said

local LastCameraCFrame = CFrame.new()
local SwayCF = CFrame.new()
local Intensity = 4

Then to keep track of camera’s CFrame add this :

RunService.RenderStepped:Connect(function(deltaTime)
local RotationCFrame = workspace.CurrentCamera.CFrame:toObjectSpace(LastCameraCFrame) -- Get Rotation relative to workspace.CurrentCamera
local X, Y, Z = RotationCFrame:toOrientation() -- Get Each Axis from the CFrame components
SwayCF = SwayCF:Lerp(CFrame.Angles(math.sin(X), math.sin(Y), 0)) -- Apply our rotation to SwayCFrame
 workspace.CurrentCamera.CFrame=  workspace.CurrentCamera.CFrame -- Reset  workspace.CurrentCamera.CFrame
end)

And then add this to your model PrimaryPartCFrame !
SetPrimarypartCFrame(camera.CFrame * SwayCFrame)

Hope it helps

What should this be assigned to since I’m getting a squiggly line from it.

Sorry i just edited the post i misspelled this variable. Since I get a lot of messages on FPS Frameworks and their aspects, I will surely make a Video explaining all from A to Z

This gives me errors (Unable to cast double to CoordinateFrame)

Post updated. Just forgot declaring CFrame.Angles. If its working for you please put it at solved (And change the topic name)

And then I lerp it to the camera?

No just multiply the SwayCF like this :

Viewmodel:SetPrimarypartCFrame(camera.CFrame * SwayCF)

Oh I did it wrong. Sorry about that!

1 Like

Please change the Topic name to something like ‘Help with Model Sway’ It will be helpful for others who gets on this

https://gyazo.com/f6daf3fef7f7da91859a6a94737dbb50

Is it meant to just stay there?