Help with making a sway viewmodel

I’m trying to make a sway viewmodel like the deadline but i dont know how i would do it, i already tried with math.clamp but it dont look works properly

Example:

my current code:

-- Sway_Multiplifiers
local multX = 1
local multY = 0.2
local multZ = 2

-- Sway_Function
function SwayUpdate()
	local rotation = Camera.CFrame:toObjectSpace(lastCameraCF) 
	local x,y,z = rotation:ToOrientation()
	swayOffset = swayOffset:Lerp(CFrame.Angles(math.sin(x)*multX,math.sin(y)*multY,math.sin(z)*multZ), 0.1)
	
	Camera:FindFirstChild(Weapon_Name).CameraBone.CFrame = Camera:FindFirstChild(Weapon_Name).CameraBone.CFrame * swayOffset
end

without clamp result

with clamp result:

code with clamp:

-- Sway_Multiplifiers
local multX = 1
local multY = 0.2
local multZ = 2

-- Sway_Function
function SwayUpdate()
	local rotation = Camera.CFrame:toObjectSpace(lastCameraCF) 
	local x,y,z = rotation:ToOrientation()
	local maxtilt = game:GetService("ReplicatedStorage").NUMERO.Value
	local ValueX = math.clamp(x, -maxtilt,maxtilt)
	local ValueY = math.clamp(y, -maxtilt,maxtilt)
	local ValueZ = math.clamp(z, -maxtilt,maxtilt)

	swayOffset = swayOffset:Lerp(CFrame.Angles(math.sin(ValueX)*multX,math.sin(ValueY)*multY,math.sin(ValueZ)*multZ), 0.1)
	
	Camera:FindFirstChild(Weapon_Name).CameraBone.CFrame = Camera:FindFirstChild(Weapon_Name).CameraBone.CFrame * swayOffset
end

whats your current issue? is it at least functioning or is it just not working at all?

1 Like

I added some videos hope that explain

1 Like

im trying to visualize what part you’re using as the pivot point for this. i would recommend only doing 2 axis for side to side and up and down, not on the pitch.

also im assuming you’re calling the SwayUpdate function on renderstep, i’d have it slowly at all times reset the tilt values to 0 so that when you’re cam is stationary it resets to normal

2 Likes

im assuming the issue is the Z Rotation