Third person gun camera is very jumpy and glitchy

I edited this post from how to make a third person camera to the current title. I found out how to achieve what I wanted, but now the only problem is is that the camera is very glitchy and jump as shown in this video:


It may be a bit hard to tell due to the low frame rate of the video, but the camera is very glitchy. It isn’t smooth but very jumpy. I’m not very good at camera manipulation so I’m not sure how to fix this. If someone could help me that would be great!

Code:

local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")


local localPlayer = game:GetService("Players").LocalPlayer
local HRP = localPlayer.Character.HumanoidRootPart
local Mouse = localPlayer:GetMouse()


local Camera = game:GetService("Workspace").CurrentCamera

local xAngle = 0
local yAngle = 0
local cameraPos = Vector3.new(3.5,0,7.5)

wait(1)
Camera.CameraType = Enum.CameraType.Scriptable
userInputService.MouseBehavior = Enum.MouseBehavior.LockCenter

userInputService.InputChanged:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseMovement then
		xAngle = xAngle-input.Delta.x*0.4
		--Clamp the vertical axis so it doesn't go upside down or glitch.
		yAngle = math.clamp(yAngle-input.Delta.y*0.4,-80,80)
		
		game:GetService("RunService").RenderStepped:Connect(function()
    		localPlayer.Character:SetPrimaryPartCFrame(CFrame.new(HRP.CFrame.Position, Vector3.new(Mouse.Hit.Position.X, HRP.CFrame.Position.Y, Mouse.Hit.Position.Z)))
		end)
		
	end
end)

runService.RenderStepped:Connect(function()
	local Character = localPlayer.Character
	local rootPart = Character:FindFirstChild("HumanoidRootPart")
	if Character and rootPart then
		--Set rotated start cframe inside head
		local startCFrame = CFrame.new((rootPart.CFrame.p + Vector3.new(0,2,0)))*CFrame.Angles(0, math.rad(xAngle), 0)*CFrame.Angles(math.rad(yAngle), 0, 0)
		
		--Set camera focus and cframe
		local cameraCFrame = startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,cameraPos.Z))
		local cameraFocus = startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,-50000))
		
		Camera.CFrame = CFrame.new(cameraCFrame.p,cameraFocus.p)
	end
end)
1 Like

I’ve already tried this script and it doesn’t work.

I don’t get what you’re trying to do. Maybe any examples of what you’re trying to achieve?

Sorry I should’ve been more descriptive, here’s a video of what I want. I took this from a free model made by Roblox. I want the camera over the player’s shoulder. (I deleted the old post because my friend’s voices were in the background) :sweat_smile:

Interesting. I know I’m replying a bit late and you may have a solution, but I’ll try and help anyway:

I’m thinking that you can weld a part to the humanoid’s head. You can then set the camera’s subject to that part. You may have to use run service if it’s glitchy.

Side note: I don’t have access to studio at the moment, so you can play around with some camera styles to make sure that the camera is the way you want.

I’ll try this out. Thanks for the reply :slight_smile:

i think you are setting the rootpart’s cframe in a loop that runs too slowly, like what @dodle120 said, you should use RunService.Heartbeat

The camera works fine now though when the player is in the camera for about 1 minute it gets progressively more laggy and fps drops to about 5.

thats because you never Disconnect() your previous RenderStepped functions and they all pile up on eachother,
you have to do something like:

RStepConn = RS.RenderStepped:Connect(function()

end)
-- do this before you start a new one
if RStepConn then
     RStepConn:Disconnect()
end

I’ve tried disconnecting both the Heartbeat function and RenderStepped function in the script and both cause issues. If I were to disconnect the Heartbeat function, the camera stays in scriptable mode and doesn’t seem to move with the character and if I disconnect the RenderStepped function the camera doesn’t move with the character when I move my mouse, though when I move around with keys it follows the character.