How do i limit the player camera rotation

i wanna make something like the attach camera mode but you can rotate the camera a little bit right and left

2 Likes

The idea is to clamp the amount the camera can rotate so it cannot go beyond the rotation limit


-- Services
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

-- Player and Camera
local player = Players.LocalPlayer
local camera = workspace.CurrentCamera

-- Variables
local maxRotation = 45 -- Maximum to rotate cam 

  local rotationangle = 0

-- Input Handling
local function onInputChanged(input)
 
   if input.UserInputType == Enum.UserInputType.MouseMovement then
        local DX = input.Delta.X
        rotationAngle = math.clamp(rotationAngle + DX, -maxRotation, maxRotation)
        
        camera.CFrame *= CFrame.Angles(0, math.rad(rotationAngle), 0)
    end
end

UserInputService.InputChanged:Connect(onInputChanged)

2 Likes

I tested this in studio, but it more so just multiplied the horizontal sensitivity.

1 Like

Would defining the rot angle outside of the function work?

2 Likes

Yes, I’ll be uploading a tweaked version shortly.

1 Like

This is a quick and dirty script that allows you to go 15 degrees to the left or right.

-- Solution for: https://devforum.roblox.com/t/how-do-i-limit-the-player-camera-rotation/3143850
-- Based off of work by MysteryX2076

-- Services
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

-- Player and Camera
local player = Players.LocalPlayer
local camera = workspace.CurrentCamera

-- Variables

local initialAngle = CFrame.lookAlong(Vector3.new(-5,5,-5),Vector3.new(1,0,2))
local horiztonalRotation = 0
local maxRotation = 15

-- Input Handling
local function onInputChanged(input)
	if input.UserInputType == Enum.UserInputType.MouseMovement then
		horiztonalRotation = math.clamp(horiztonalRotation + input.Delta.X,-maxRotation,maxRotation)
	end
end

UserInputService.InputChanged:Connect(onInputChanged)

RunService.RenderStepped:Connect(function(dt)
	camera.CFrame = initialAngle * CFrame.Angles(0,math.rad(horiztonalRotation), 0)
end)
1 Like

can you make a small explination of what is the script doing
Sorry for being late i was outside traffic was wild i had to wait 2 hours just to get home

1 Like

Moving the mouse increments horizontalRotation. horizontalRotation is clamped to be between -15 to 15 degrees.

Every frame, the camera is set back to a certain CFrame, which in this example I’ve called initialAngle. It is then rotated by a new CFrame made with horizontalRotation.

Do take note that my script doesn’t work but creates a baseline for the limiting.
In the function, we’re basically checking if the movement we detected was mouse movement. Then we are getting the delta (change) in the mouse movement from before. When we add this delta to the rotationangle, we would get the angle we need to move the camera by, but since we want to limit it, we will clamp it (a process in which any number supplied cannot be less than the minimum supplied and greater than the maximum supplied.) Then we simply multiply it :slight_smile:

2 Likes