Smooth Camera Script Help

Below is a local script that is in StarterGui. The script works but the problem is the camera gets glitchy when you look straight up or down. I have tried on my own and with assistance from ChatGPT to fix this but I’m not sure how.

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Camera = workspace.CurrentCamera
local rad = math.rad
local Rot = CFrame.new()
local function GetRollAngle()
	local Character = Player.Character
	if not Character then
		return
	end
	local Cf = Camera.CFrame
	return -Cf.RightVector:Dot(Character.Humanoid.MoveDirection)
end

RunService:BindToRenderStep("RotateCameraInDirectionPlayerIsGoing", Enum.RenderPriority.Camera.Value + 1, function()
	local Roll = GetRollAngle() * 3
	Rot = Rot:Lerp(CFrame.Angles(0, 0, rad(Roll)),0.075)
	Camera.CFrame *= Rot
end)

-- Next part

local uis = game:GetService'UserInputService'
local mouse = Player:GetMouse()
local mult = 180/math.pi
local clamp = math.clamp
local current = Vector2.zero
local targetX, targetY = 0, 0
local speed = 0.3 -- Set the speed of the animation
local sensitivity = 0.2 -- Set the sensitivity of the rotation

uis.MouseDeltaSensitivity = 0.01
Camera.CameraType = Enum.CameraType.Custom
RunService:BindToRenderStep("SmoothCam", Enum.RenderPriority.Camera.Value-1, function(dt)
	local delta = uis:GetMouseDelta()*sensitivity*100
	targetX += delta.X
	targetY = clamp(targetY+delta.Y,-90,90)
	current = current + (Vector2.new(targetX, targetY) - current) * math.min(dt*8, 15)
	Camera.CFrame = CFrame.fromOrientation(-current.Y/mult,-current.X/mult,0)
end)
1 Like

The targetY clamp values shouldn’t be anything over 80.

RunService:BindToRenderStep("SmoothCam", Enum.RenderPriority.Camera.Value-1, function(dt)
	local delta = uis:GetMouseDelta()*sensitivity*100
	targetX += delta.X
	targetY = clamp(targetY+delta.Y,-80, 80) -- these values should never be 90 or over
	current = current + (Vector2.new(targetX, targetY) - current) * math.min(dt*8, 15)
	Camera.CFrame = CFrame.fromOrientation(-current.Y/mult,-current.X/mult,0)
end)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.