I’m using this kind of code to keep the viewmodel gun fixed to the camera, but it’s acting quite weird. When I fire the gun, it uses some position calculations, and everything works fine when I use Heartbeat. However, when I use the RenderStepped event, the bullets spawn from completely the wrong position. What could be causing this, and how can I fix it?
local function RepositionGun()
viewmodel:PivotTo(camera.CFrame * CFrame.new(0, -0.6, -2))
end
This Code is small part of my FireGun Function
local Direction = (Mouse.Hit.p - viewmodel.m4a1.Muzzle.MuzzleAttachment.WorldCFrame.Position).Unit
local Origin = viewmodel.m4a1.Muzzle.Position
I’m assuming this is as small as smaller than a stud, maybe the bullets are spawning outside the muzzle, or the bullets are way off the avatar. Any screenshots or videos of that issue? And how are you spawning the bullets? Any extra code snippets?
My best guess is with task scheduling priorities, and that animations are applied only after callbacks from BindToRenderStep and connections from PreAnimation, meaning those functions before PreRender do not receive the updated positions. Whereas Heartbeat is after everything else, including physics, are calculated, and before the game enters the next frame. If your bullets rely on physics, then maybe events between PostSimulation to Heartbeat will receive the correct position to spawn the bullets, but not the others before it.
Oh, wait. I feel kind of dumb now. The issue disappeared after I anchored the HumanoidRootPart of the viewmodel. I guess I didn’t notice the falling movement because it wasn’t visible from the camera. Thank you.