Hello I need help on adding camera shake from the animation in my view model.
In the view model there is a Head and that has a animation which controls the camera shake and i need the current camera to follow the heads rotational offset inside the view model.
video with camera shake in moon animator2
video without camera shake in game
Local Script Inside a tool:
local tool = script.Parent
local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local mouse = player:GetMouse()
local buttondown = false
local viewmodel = nil
local replicatedstorage = game:GetService("ReplicatedStorage")
local runservice = game:GetService("RunService")
tool.Equipped:Connect(function()
viewmodel = replicatedstorage.ViewModels:WaitForChild("Pistol"):Clone()
viewmodel.Parent = camera
end)
player.Character:FindFirstChildOfClass("Humanoid").Died:Connect(function()
if viewmodel then
viewmodel:Destroy()
end
viewmodel = nil
end)
local swayAmount = .6
local swayCF = CFrame.new()
local lastCameraCF = CFrame.new()
runservice.RenderStepped:Connect(function()
if viewmodel then
viewmodel.PrimaryPart = viewmodel:FindFirstChild("Head")
local rot = camera.CFrame:ToObjectSpace(lastCameraCF)
local X,Y,Z = rot:ToOrientation()
swayCF = swayCF:Lerp(CFrame.Angles(math.sin(X) * swayAmount, math.sin(Y) * swayAmount, 0), .1)
lastCameraCF = camera.CFrame
viewmodel:PivotTo(camera.CFrame * swayCF)
end
end)
tool.Unequipped:Connect(function()
if viewmodel then
viewmodel:Destroy()
end
viewmodel = nil
end)
local cooldown = 0.5
local deb = false
mouse.Button1Down:Connect(function()
buttondown = true
end)
mouse.Button1Up:Connect(function()
buttondown = false
end)
local cooldown = 0.5
local deb = false
while task.wait() do
if viewmodel then
if deb == false and buttondown == true then
deb = true
local fire = viewmodel:FindFirstChildOfClass("Humanoid").Animator:LoadAnimation(script.Fire)
fire:Play(0)
task.wait(cooldown)
deb = false
end
end
end
Thank You For Reading