Trying to attach the viewmodel to the player’s camera, and most gun kits and games have it just perfectly on the camera?
im wondering how they do this as im just setting the pivot of the viewmodel to the currentcam’s cframe which isnt working too well
Code that i forgot to add
-- Wait()
repeat task.wait() until game:GetService("Players").LocalPlayer.Character
-- Variables
local Player = game:GetService("Players")["LocalPlayer"]
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local HoldAnim = Instance.new("Animation"); HoldAnim.AnimationId = "rbxassetid://10469949936"
local ShootAnim = nil
local ViewModel = game.ReplicatedStorage.AkViewmodel:Clone(); ViewModel.Parent = game.Workspace.Camera
local Animator = ViewModel:WaitForChild('Humanoid').Animator
local Locked = false
local LockedConnection = nil
-- Functions
local function LockToCamera()
if Locked then return end
Locked = true
coroutine.resume(coroutine.create(function()
local S, E = pcall(function()
ViewModel:WaitForChild("CameraBone")
LockedConnection = RunService.Heartbeat:Connect(function()
ViewModel:PivotTo(workspace.CurrentCamera)
end)
end)
end))
end
local function Unlock()
if Locked then
Locked = false
LockedConnection:Disconnect()
end
end
-- Main
ViewModel.CameraBone.Transparency = 1
Animator:LoadAnimation(HoldAnim):Play()
LockToCamera()
Most if not all fps games set their viewmodels cframe to the camera’s cframe every frame, so try setting your’s in a RenderStepped event or use BindToRenderStep
are you doing it on clientside? it shouldnt lag behind
you could handle it on a local script for the localplayer, then fire a remote event with the cframe to tell the server to tell the other clients where that player is facing to update it on their clientsided
Is this code you posted the only code you have? because I noticed that it will error
Try these following changes
local function LockToCamera()
if Locked then return end
Locked = true
coroutine.resume(coroutine.create(function()
- local S, E = pcall(function()
ViewModel:WaitForChild("CameraBone")
- LockedConnection = RunService.Heartbeat:Connect(function()
+ LockedConnection = RunService.RenderStepped:Connect(function()
- ViewModel:PivotTo(workspace.CurrentCamera)
+ ViewModel:PivotTo(workspace.CurrentCamera.CFrame)
- end)
end)
end))
end
This is exactly why you shouldn’t use pcall in this manner.
The following is for the thread’s poster.
local CameraBone = ViewModel:WaitForChild("CameraBone") --This should be moved outside of the function (it only needs to be waited for once).
local function LockToCamera()
if Locked then return end
Locked = true
--The coroutine is completely unnecessary, 'Heartbeat' will not yield the current thread's execution.
LockedConnection = RunService.Heartbeat:Connect(function()
ViewModel:PivotTo(workspace.CurrentCamera.CFrame)
end)
end
--Unlock function here.
-- Main
CameraBone.Transparency = 1 --Earlier 'WaitForChild' simplifies this reference.
Animator:LoadAnimation(HoldAnim):Play()
LockToCamera()