I wrote a simple viewmodel script, but something about it tells me there’s room for improvement. Maybe because the last time I wrote one, it was kinda primitive and felt snappy. Either way, any tips or improvements are greatly appreciated.
--[[Variables]]--
--Services--
local Players = game:GetService("Players");
local ReplicatedStorage = game:GetService("ReplicatedStorage");
local RunService = game:GetService("RunService");
--Modules--
local modules = ReplicatedStorage:WaitForChild("Modules");
--User--
local plr = Players.LocalPlayer;
local char = plr.Character or plr.CharacterAdded:Wait();
local hum = char:WaitForChild("Humanoid");
local cam = workspace.CurrentCamera;
--Viewmodel--
local viewmodel = ReplicatedStorage:WaitForChild("Viewmodel"):Clone();
local root = viewmodel.PrimaryPart;
viewmodel.Parent = cam;
local VIEWMODEL_UPDATE_ALPHA = 0.1;
--[[Functions]]--
local lookAt = CFrame.lookAt;
function updateViewmodel()
local camCF = cam.CFrame;
local newPos = camCF.Position;
local currentPos = root.Position;
local deltaPos = (newPos - currentPos);
local posCF = root.CFrame + deltaPos; --> This is just the camera's CFrame without the rotation.
local goalCF = lookAt(posCF.Position, camCF.Position + camCF.LookVector); --> A CFrame where the viewmodel is looking in the same direction as the camera.
viewmodel:SetPrimaryPartCFrame(posCF:Lerp(goalCF, VIEWMODEL_UPDATE_ALPHA)); --Lerp the rotation part, but update position instantly.
end
RunService:BindToRenderStep("updateViewmodel", 2000, updateViewmodel);
function cleanUp()
RunService:UnbindFromRenderStep("updateViewmodel");
viewmodel:Destroy();
end
hum.Died:Connect(cleanUp);