Miniscule problems with viewmodel bobbling

I have a viewmodel! yay!

i want to add some sort of bobbling to it so it doesn’t feel stiff whenever you walk, but as we all know it requires some complex mathematics

soo i tried using the camera bobble effect roblox provides on the documentation
but uh, it didn’t work :pensive:

the BindToRenderStep is located at the bottom of this code block

local rService = game:GetService("RunService")
local rStorage = game:GetService("ReplicatedStorage")
local pService = game:GetService("PhysicsService")
local uInputService = game:GetService("UserInputService")
local tService = game:GetService("TweenService")

-- Tween parameters;
local tweenParameters = TweenInfo.new(
	0.3,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out
)

local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local camera = workspace.CurrentCamera
local mouse = plr:GetMouse()

local viewmodelsFolder = rStorage:WaitForChild("Viewmodels")

-- Path to modules;
local modulesFolder = rStorage:WaitForChild("Modules")
local weaponModules = modulesFolder:WaitForChild("WeaponModules")

-- Modules;
local shootingModule = require(weaponModules.ShootingModule_Client)
local weaponStatsModule = require(weaponModules.ClientWeaponStats)
local reloadingModule = require(weaponModules.ReloadingModule_Client)

-- Connections;
local lmbConnection = nil
local rConnection = nil
local shoveConnection = nil
local rmbConnection = nil

local function SetCollisionGroup(children, group)
	for i = 1, #children do
		local child = children[i]

		if child:IsA("BasePart") then
			child.CollisionGroup = group
		end

		SetCollisionGroup(child:GetChildren(), group)
	end
end

function OnChildAdded(weapon: Tool)
	if not weapon:IsA("Tool") then return end

	for _,part in weapon:GetDescendants() do
		if part:IsA("BasePart") then
			part.Transparency = 1
		end
	end

	local viewmodel = viewmodelsFolder:FindFirstChild("v" .. weapon.Name)

	if not viewmodel then warn("Viewmodel instance wasn't found!") return end

	local clonedViewmodel: Model = viewmodel:Clone()

	clonedViewmodel.Parent = workspace
	local viewmodelHumanoid = clonedViewmodel:FindFirstChild("Humanoid")

	SetCollisionGroup(clonedViewmodel:GetChildren(), "Viewmodel")

	rService:BindToRenderStep("WeldViewmodel", Enum.RenderPriority.Camera.Value, function()	
		local now = tick()		
		local velocity = humanoid.RootPart.Velocity
		
		local bobble_X = math.cos(now * 6) * 0.3
		local bobble_Y = math.abs(math.sin(now * 6)) * 0.2
		
		local bobble = Vector3.new(bobble_X, bobble_Y, 0) * math.min(1, velocity.Magnitude / humanoid.WalkSpeed)
		
		if viewmodelHumanoid.MoveDirection.Magnitude > 1 then
			clonedViewmodel:PivotTo(camera.CFrame * CFrame.new(bobble)) -- doesn't work :(
		else
			clonedViewmodel:PivotTo(camera.CFrame)
		end
	end)

ummm

this is still not fixed :grin:

Nah I would rework your bobble code here (also making it positive doesn’t let it go in 2 directions, due to how sine waves work)

rService:BindToRenderStep("WeldViewmodel", Enum.RenderPriority.Camera.Value, function()	
   if humanoid.MoveDirection == Vector3.zero then return end
   local cycles = 6
   local amplitude = 0.2
   local shake = amplitude * math.sin(cycles * tick()) --[[y = 0.2sin(6x)]]
   workspace.CurrentCamera.CFrame *= CFrame.Angles(math.rad(shake),math.rad(shake),0)
end)

nooo i don’t want to affect the player camera

i want to affect the viewmodel

Move direction is normally 0-1 value I believe so the check should be >0.9 to account for this.

Also could be the reason the if statement is not working.

nope

changing it to 0.1 or 0.9 doesn’t do anytihngh