Prevent a camera from looking up

I want to prevent the player from looking up, but it keeps glitching out because the mouse does not lock fast enough:

https://gyazo.com/c72c8547598573f1169c83735b0f2d9e

Code:

local function CameraAngle(CurrentCamera: Camera): boolean
	local LookVector = CurrentCamera.CFrame.LookVector.Unit
	local UpVector = Vector3.new(0, 1, 0)

	if LookVector:Dot(UpVector) <= 0 then 
		return true
	else
		return false
	end
end
local Angle = CameraAngle(self.CurrentCamera)
	
	if Angle then 
		UserInputService.MouseBehavior = Enum.MouseBehavior.Default
		
		self.CurrentCamera.CFrame = self.CameraPart.CFrame * CFrame.Angles(
			math.rad(((self.Mouse.Y - self.Mouse.ViewSizeY / 2) / self.Mouse.ViewSizeY) * -self.Tilt),
			math.rad(((self.Mouse.X - self.Mouse.ViewSizeX / 2) / self.Mouse.ViewSizeX) * -self.Tilt),
			0
		)
	else
		UserInputService.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
		self.CurrentCamera.CFrame = self.CameraPart.CFrame
	end

Is there a better way to rework this entire piece of code? Thanks.

I’m not super well versed in CFrame math and Angles, but you might be able to just clamp your calculations for the CFrame.Angles so the angle’s never exceed a specific number.

1 Like

I should have thought of this :laughing: !

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