I made this basic script for a Viewmodel, but how do I animate it?
Script:
-- Services
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
-- References
local camera = workspace.CurrentCamera
local viewmodelsFolder = game:GetService("ReplicatedStorage"):WaitForChild("Viewmodels")
-- Config
local ROTATION_SMOOTHNESS = 0.15
local MOUSE_ENABLED = false
-- Set Config
UserInputService.MouseIconEnabled = MOUSE_ENABLED
-- Lighting
local attachment = Instance.new("Attachment")
attachment.Parent = camera
attachment.Name = "LightAttachment"
local light = Instance.new("SpotLight")
light.Shadows = true
light.Angle = 100
light.Range = 25
light.Brightness = 2
light.Parent = attachment
light.Name = "Light"
-- Pistol
local pistol = viewmodelsFolder:WaitForChild("PistolVM"):Clone()
pistol.Parent = camera
local lastRotation = CFrame.new()
RunService.RenderStepped:Connect(function()
local camCFrame = camera.CFrame
local camPos = camCFrame.Position
local camRot = camCFrame - camPos
lastRotation = lastRotation:Lerp(camRot, ROTATION_SMOOTHNESS)
local finalCFrame = CFrame.new(camPos) * lastRotation
if pistol.PrimaryPart then
pistol:SetPrimaryPartCFrame(finalCFrame)
else
pistol:PivotTo(finalCFrame)
end
end)
Pistol Viewmodel:
Pist0l.rbxm (76.0 KB)