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
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)