I am trying to script a simple FPS viewmodel system for the first time and for some reason my camera just seems to be gradually falling to the ground over time
RunService.RenderStepped:Connect(function()
GunModel.Parent = cam
GunModel:SetPrimaryPartCFrame(cam.CFrame)
if aiming == true then
cam.CFrame = GunModel.AimPart.CFrame![Untitled|video]
else
cam.CFrame = GunModel.Main.CFrame
end
end)
the video shows the camera gradually getting lower and lower over time
Anyway, it probably happens because you’re setting the CFrame of the camera to match the AimPart, which isn’t the best idea. At least I wouldn’t do it like that, because I’d prefer to have more direct control over the camera and make the gun follow the camera. I’m guessing that small errors are accumulating over time causing the slowly moving camera. Might be floating-point error or something like that.
To fix it, I’d just change the PrimaryPart of the GunModel before SetPrimaryPartCFrame, like this:
RunService.RenderStepped:Connect(function()
if aiming == true then
GunModel.PrimaryPart = GunModel.AimPart
else
GunModel.PrimaryPart = GunModel.Main
end
GunModel:SetPrimaryPartCFrame(cam.CFrame)
GunModel.PrimaryPart = GunModel.Main --Reset the PrimaryPart so that anything that needs it to be Main doens't break.
end)
SetPrimaryPart CFrame has some strange issues, when it’s called millions of times your model will start to offset. Just set the CFrame of the PrimaryPart in the other way.