As the title implies I need help understanding ViewModel bobbing here are some of the questions I have
How do you do it?
How would I implement it into my code?
Whats the best way to do it (not animation)?
Here is the the basic code I made it only clones and sets the CFrame of the ViewModel nothing else.
local runService = game:GetService("RunService")
local uis = game.UserInputService
local plr = game.Players.LocalPlayer
local character = plr.Character or plr.CharacterAdded:Wait()
local camera = workspace.Camera
local viewmodels = game.ReplicatedStorage:WaitForChild("ViewModels")
local gunmodel = viewmodels.Gun
local viewmodel = nil
runService.RenderStepped:Connect(function(dt)
if character:FindFirstChild("UsingGun") then
if camera:FindFirstChild("Gun") then
if character.Humanoid.Health == 0 then
if camera:FindFirstChild("Gun") then
camera:FindFirstChild("Gun"):Destroy()
viewmodel = nil
end
end
if viewmodel == nil then return end
viewmodel:SetPrimaryPartCFrame(camera.CFrame * CFrame.new(0, -1.5, -2) * CFrame.Angles(0.5,0,0))
else
local viewModel = gunmodel:Clone()
viewModel.Parent = camera
viewModel:SetPrimaryPartCFrame(camera.CFrame * CFrame.new(0, -1.5, -2) * CFrame.Angles(0.5,0,0))viewmodel = viewModel
viewmodel = viewModel
end
else
if camera:FindFirstChild("Gun") then
camera:FindFirstChild("Gun"):Destroy()
viewmodel = nil
end
end
end)
How would I make the bobbing and how would I implement it into my code?
Thanks in advance for the help!
For view bobbing, animation would really be your best bet. For sway however, I use Lerping for my viewmodels. This is a tool based viewmodel.
local equipped = false
local ViewModel
local camera = workspace.Camera
tool.Equipped:Connect(function()
equipped = true
ViewModel = game:GetService("ReplicatedStorage").Viewmodels.MarketGardener:Clone()
ViewModel.Parent = camera
end)
tool.Unequipped:Connect(function()
equipped = false
ViewModel = camera.MarketGardener
ViewModel:Destroy()
end)
local swayCF = CFrame.new()
RS.RenderStepped:Connect(function()
if player.Character:WaitForChild("Humanoid").Health <= 0 then --Making sure not to display VM when player is dead
if camera:FindFirstChild("MarketGardener") ~= nil then
workspace.Camera.MarketGardener:Destroy()
end
end
if equipped == true then
if camera:FindFirstChild("MarketGardener") ~= nil then
camera.MarketGardener:SetPrimaryPartCFrame(camera.CFrame)
for i, v in pairs(camera.MarketGardener:GetChildren()) do
if v:IsA("BasePart") then
v.CanCollide = false
v.CanQuery = false
v.CanTouch = false
end
end
local mouseDelta = UIS:GetMouseDelta()/50
local swayX = math.clamp(mouseDelta.X, -0.05,0.05)
local swayY = math.clamp(mouseDelta.Y, -0.05,0.05)
swayCF = swayCF:Lerp(CFrame.new(swayX, swayY, 0), .3) -- Adjust alpha based on preference
camera.MarketGardener:SetPrimaryPartCFrame(camera.CFrame * swayCF)
end
end
end)