How to make the camera have limits?

right now i have a camera script that replaces the players, but it doesn’t have any limits
and I want it to have a SMOOTH limit on how far it can go like this:


I have no idea how to do this for I am not good with cframes and the internet is not helping
any help would be well appreciated
my code:

local runservice = game:GetService("RunService")
local userinputservice = game:GetService("UserInputService")
local camera =  game.Workspace.CurrentCamera
local camerapart = game.Workspace:WaitForChild("Personal human").Head
local cameraRotation = Vector2.new(0,math.rad(0))

runservice.RenderStepped:Connect(function()
	local camPosition = camerapart.Position
	local mouseDelta = userinputservice:GetMouseDelta()
	cameraRotation = cameraRotation - mouseDelta/200
	local cameraRotationCFrame = CFrame.Angles(0, cameraRotation.X, 0)*CFrame.Angles(cameraRotation.Y, 0, 0)
	camera.CFrame = cameraRotationCFrame+camPosition
end)
1 Like

Here’s one way to make a smooth camera limit:
Check when the camera’s rotation vector is over the limit and then interpolate it back to the limit which will make it smooth

I added the smooth camera limit to your code

-- The settings here

local minX = math.rad(-60) -- Minimum camera X angle
local maxX = math.rad(60) -- Maximum camera X angle

local minY = math.rad(-60) -- Minimum camera Y angle
local maxY = math.rad(60) -- Maximum camera Y angle

local stiffness = 16 -- a smaller number will make the limit more smooth while a bigger number will make the limit less smooth

-- Code

local runservice = game:GetService("RunService")
local userinputservice = game:GetService("UserInputService")
local camera =  game.Workspace.CurrentCamera
local camerapart = game.Workspace:WaitForChild("Personal human").Head
local cameraRotation = Vector2.new(0,math.rad(0))

runservice.RenderStepped:Connect(function(dt)
	local camPosition = camerapart.Position
	local mouseDelta = userinputservice:GetMouseDelta()
	cameraRotation = cameraRotation - mouseDelta/200


	local t = 1 - math.exp(-dt * stiffness)

	if cameraRotation.X < minX then
		cameraRotation += Vector2.new((minX - cameraRotation.X) * t, 0)
	elseif cameraRotation.X > maxX then
		cameraRotation += Vector2.new((maxX - cameraRotation.X) * t, 0)
	end

	if cameraRotation.Y < minY then
		cameraRotation += Vector2.new(0, (minY - cameraRotation.Y) * t)
	elseif cameraRotation.Y > maxY then
		cameraRotation += Vector2.new(0, (maxY - cameraRotation.Y) * t)
	end


	local cameraRotationCFrame = CFrame.Angles(0, cameraRotation.X, 0)*CFrame.Angles(cameraRotation.Y, 0, 0)
	camera.CFrame = cameraRotationCFrame+camPosition
end)

[quote=“denkodin, post:2, topic:2821959, username:denkodin”]

local minX = math.rad(-60) -- Minimum camera X angle
local maxX = math.rad(60) -- Maximum camera X angle

local minY = math.rad(-60) -- Minimum camera Y angle
local maxY = math.rad(60) -- Maximum camera Y angle

local stiffness = 16 -- a smaller number will make the limit more smooth while a bigger number will make the limit less smooth

-- Code

local runservice = game:GetService("RunService")
local userinputservice = game:GetService("UserInputService")
local camera =  game.Workspace.CurrentCamera
local camerapart = game.Workspace:WaitForChild("Personal human").Head
local cameraRotation = Vector2.new(0,math.rad(0))

runservice.RenderStepped:Connect(function(dt)
	local camPosition = camerapart.Position
	local mouseDelta = userinputservice:GetMouseDelta()
	cameraRotation = cameraRotation - mouseDelta/200


	local t = 1 - math.exp(-dt * stiffness)

	if cameraRotation.X < minX then
		cameraRotation += Vector2.new((minX - cameraRotation.X) * t, 0)
	elseif cameraRotation.X > maxX then
		cameraRotation += Vector2.new((maxX - cameraRotation.X) * t, 0)
	end

	if cameraRotation.Y < minY then
		cameraRotation += Vector2.new(0, (minY - cameraRotation.Y) * t)
	elseif cameraRotation.Y > maxY then
		cameraRotation += Vector2.new(0, (maxY - cameraRotation.Y) * t)
	end


	local cameraRotationCFrame = CFrame.Angles(0, cameraRotation.X, 0)*CFrame.Angles(cameraRotation.Y, 0, 0)
	camera.CFrame = cameraRotation

Thank you sm :sob:

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