How to create a viewmodel and animate them

I would like to know how you guys create your ViewModels and animate them. I am not advanced enough for this feature yet and would like to learn more. Thank you! :slight_smile:

Sending a scriptof the viewmodel would also be nice but not required for this post.

I would also like to know how you guys create this system when you are required to show the body of the player. Do you usually make the arm invisible when the viewmodel is activated?

2 Likes

First off, get two arms, long ass meshes, get whatever weapon you want, and make a part to indicate the camera, and rig the model, set the primary part of the model to the camera.

After that, make an idle animations thats repeating.

Then, every render stepped, put the primary part of that model to the camera.

local viewmodel = --PATH
viewmodel.Parent = workspace.Camera

local runservice = game:GetService("RunService")
local camera = workspace.CurrentCamera

runservice.RenderStepped:Connect(function()
   viewmodel.Camera.CFrame = camera.CFrame
end)

Boom, you got the basics done,

Now animations:

Let’s start with bobbing, we will be using sin and cos waves for those

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local hum = character:WaitForChild("Humanoid")

local viewmodel = --PATH
viewmodel.Parent = workspace.Camera

local runservice = game:GetService("RunService")

local camera = workspace.CurrentCamera

local bobCF = CFrame.new()

locla running = 0

runservice.RenderStepped:Connect(function()

   if hum.MoveDirection.Magnitude > 0 then running = 1 else running = 0 end --Checks if the player is walking or not.

   local bobX = math.cos(tick() * 5 * hum.WalkSpeed/16) * .2 --Numbers inside () make the speed, and the .2 is the intensity, for both the Y and X
   local bobY = math.abs(math.sin(tick() * 6 * hum.WalkSpeed/16) * .3)

   bobCF = bobCF:Lerp(CFrame.new(bobX * running,bobY * running,0), .4) --Makes it smooth

   viewmodel.Camera.CFrame = camera.CFrame * bobCF --Boom

end)

Now time to make a camera sway:

We need the mouse delta for this, let’s use UserInputService:


local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local hum = character:WaitForChild("Humanoid")

local viewmodel = --PATH
viewmodel.Parent = workspace.Camera

local uis = game:GetService("UserInputService")
local runservice = game:GetService("RunService")

local camera = workspace.CurrentCamera

local bobCF = CFrame.new()
local swayCF = CFrame.new()

locla running = 0

runservice.RenderStepped:Connect(function()
   local mouseDelta = uis:GetMouseDelta()/50 -- devide it by how much u want

   if hum.MoveDirection.Magnitude > 0 then running = 1 else running = 0 end --Checks if the player is walking or not.

   local swayX =  math.clamp(mouseDelta.X, -0.2,0.2) --We clamp so that we dont get janky results
   local swayY = math.clamp(mouseDelta.Y, -0.2,0.2)

   local bobX = math.cos(tick() * 5 * hum.WalkSpeed/16) * .2 --Numbers inside () make the speed, and the .2 is the intensity, for both the Y and X
   local bobY = math.abs(math.sin(tick() * 6 * hum.WalkSpeed/16) * .3)

   bobCF = bobCF:Lerp(CFrame.new(bobX * running,bobY * running,0) , .4) --Makes it smooth
   swayCF = swayCF:Lerp(CFrame.new(swayX, swayY, 0), .3)

   viewmodel.Camera.CFrame = camera.CFrame * bobCF * swayCF --Boom

end)

answering your questions, personally, i make the viewmodels, add the shirts, and color the arms to be the same as the character’s arm skin color, and make sure all the bodyparts are invisible in first person

13 Likes

Hey, Thanks for the tutorial! I will implement this ASAP once I am able to use my PC again Do you have any gifs/video to show how this will look like?

1 Like

I don’t really understand this part and the “set primary part of the model to the camera”. Could you please elaborate?

1 Like

Use joins to connect the parts of the viewmodel

1 Like

Thank you!!! It took me a while to get a grasp on this viewmodel concept but your explanation and scripts made it clear

2 Likes

you’re welcome man

3 Likes

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