How to animate Viewmodel

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)

2 Likes

Although you could use CFrame animations, I would recommend you animate it just like how you would animate a rig. You must ensure it has a primary part, an AnimationController, and is properly rigged with Motor6Ds. After that, simply use the animation window and animate it! If you’re using MoonAnimator, I’m not too sure if you need to rig it properly or maybe it auto-rigs (sorry, I don’t use MoonAnimator).

1 Like

sorry, i forgot animator’‘’‘’’

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.