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)