I’m currently trying to program a viewmodel with an equip animation. The viewmodel should be attached to the camera after the animation starts to prevent dumb arms. However, the viewmodel still gets updated prior to the equip animation playing, resulting in dumb arms for a frame. I have tried a different animation, and this problem still occurs. I have also tried adding a check for if the animation was played, but the problem still happens. I also tried printing the times the function was run, but I found that the animation was playing before the arms were updated.
Equip function:
-- I'm using a tutorial don't bully me
self.camera = workspace.CurrentCamera
self.character = game.Players.LocalPlayer.Character
-- Throw the viewmodel under the map. It will go back to the camera the next render frame once we get to moving it.
self.viewmodel.HumanoidRootPart.CFrame = CFrame.new(0,-100,0)
-- We're making the gun bound to the viewmodel's rootpart, and making the arms move along with the viewmodel using hierarchy.
self.viewmodel.HumanoidRootPart.Main.Part1 = self.viewmodel.Main
self.viewmodel.LeftArm.left.Part0 = self.viewmodel.Main
self.viewmodel.RightArm.right.Part0 = self.viewmodel.Main
-- I legit forgot to do this in the first code revision.
self.settings = require(self.viewmodel.settings)
self.viewmodel.Parent = workspace.Camera
self.loadedAnimations.equip = self.viewmodel.AnimationController.Animator:LoadAnimation(self.settings.animations.viewmodel.equip)
self.loadedAnimations.equip.Looped = false
self.loadedAnimations.idle = self.viewmodel.AnimationController.Animator:LoadAnimation(self.settings.animations.viewmodel.idle)
self.loadedAnimations.firing = self.viewmodel.AnimationController.Animator:LoadAnimation(self.settings.animations.viewmodel.firing)
self.loadedAnimations.firing.Looped = false
self.loadedAnimations.equip:Play(0)
reallyEquipped = true
connection = self.loadedAnimations.equip.Stopped:Connect(function()
self.loadedAnimations.idle:Play(0)
self.equipped = true
connection:Disconnect()
end)
end
-- runs every frame
function module:update(deltaTime)
-- IF we have a gun right now. We're checking the viewmodel instead for "reasons".
if self.viewmodel then
if reallyEquipped ~= true then return end
-- get velocity for walkCycle
local velocity = self.character.HumanoidRootPart.Velocity
-- it'll be final for a reason. You'll see!
local idleOffset = self.viewmodel.offsets.idle.Value
local aimOffset = idleOffset:lerp(self.viewmodel.offsets.aim.Value, self.lerpValues.aim.Value)
local finalOffset = aimOffset
-- Let's get some mouse movement!
local mouseDelta = game:GetService("UserInputService"):GetMouseDelta()
self.springs.sway:shove(Vector3.new(mouseDelta.x / 200,mouseDelta.y / 200)) --not sure if this needs deltaTime filtering
-- speed can be dependent on a value changed when you're running, or standing still, or aiming, etc.
-- this makes the bobble faster.
local speed = 1
-- modifier can be dependent on a value changed when you're aiming, or standing still, etc.
-- this makes the bobble do more. or something.
local modifier = 0.1
if self.aiming == true then
modifier = 0.01
end
-- See? Bobbing! contruct a vector3 with getBobbing.
local movementSway = Vector3.new(getBobbing(10,speed,modifier),getBobbing(5,speed,modifier),getBobbing(5,speed,modifier))
-- if velocity is 0, then so will the walk cycle
self.springs.walkCycle:shove((movementSway / 25) * deltaTime * 60 * velocity.Magnitude)
-- Sway! Yay!
local sway = self.springs.sway:update(deltaTime)
local walkCycle = self.springs.walkCycle:update(deltaTime)
self.viewmodel.HumanoidRootPart.CFrame = self.camera.CFrame:ToWorldSpace(finalOffset)
--ToWorldSpace basically means rootpart.CFrame = camera CFrame but offset by xxx while taking rotation into account. I don't know. You'll see how it works soon enough.
self.viewmodel.HumanoidRootPart.CFrame = self.viewmodel.HumanoidRootPart.CFrame:ToWorldSpace(CFrame.new(walkCycle.x / 2,walkCycle.y / 2,0))
-- Rotate our rootpart based on sway
self.viewmodel.HumanoidRootPart.CFrame = self.viewmodel.HumanoidRootPart.CFrame * CFrame.Angles(0,-sway.x,sway.y)
self.viewmodel.HumanoidRootPart.CFrame = self.viewmodel.HumanoidRootPart.CFrame * CFrame.Angles(0,walkCycle.y,walkCycle.x)
end
end