How to lock camera Y axis

Error:

code piece: camera.CFrame.Rotation.Y = math.clamp(tonumber(camera.CFrame.Rotation.Y),1,1.3)

i assigned camera to the currentcamera.
this is in a localscript

Basically, I am having trouble with locking my camera in a certain Y axis so the player cant see their body weirdly when sliding. I want the character to face forward in the same angle the whole duraction of the slide. How do I keep the Y between a certain range from changing due to player moving the camera?

The function:

RunService.RenderStepped:Connect(function(step)
	-- anti camera clipping
	local myRay = Ray.new(char.Head.Position, ((char.Head.CFrame + char.Head.CFrame.LookVector * 2) - char.Head.Position).Position.Unit)
	local IL = char:GetChildren()
	local hit, position = workspace:FindPartOnRayWithIgnoreList(myRay,IL)
	if hit then
		char.Humanoid.CameraOffset = Vector3.new(0,-1,-(char.Head.Position - position).magnitude)
	elseif slideactive.Value == true then
		char.Humanoid.CameraOffset = Vector3.new(0,-2,0)
		camera.CFrame.Rotation.Y = math.clamp(tonumber(camera.CFrame.Rotation.Y),1,1.3)
	
	else
		char.Humanoid.CameraOffset = Vector3.new(0,-1,-1)
	end
end)

im new to camera manipulation, i didnt really understand much of the dev wiki.

CFrames and Vector3’s cannot have their individual components modified.
They need to either be modified mathematically with other vectors / cframes or you need to create entirely new ones.
If I want to add 3 Y to a vector, you wouldn’t do:

part.Position.Y += 3

you’d instead do either

part.Position += Vector3.new(0, 3, 0)

or

part.Position = Vector3.new(part.Position.X, part.Position.Y + 3, part.Position.Z)

Same with CFrames, except they have to be multiplicated together for additions (you can make cframes negative by doing :Inverse().)

So, what you should be doing in this case is something like:

local xAngle, yAngle, zAngle = camera.CFrame:ToEulerAnglesXYZ()-- XYZ angles for CFrames
local newCameraCFrame = CFrame.new(camera.CFrame.Position)-- This creates a CFrame without any rotation
newCameraCFrame *= CFrame.Angles(xAngle, math.clamp(yAngle, 1, 1.3), zAngle)-- CFrame.Angles creates a cframe without a position that has rotational data. We're adding it to the positional cframe. Reminder that "*" with cframes behaves like "+"
camera.CFrame = newCameraCFrame
1 Like