Trouble clamping camera

Hello there. I made my own camera script for a game I am making.

Long story short, I don’t know how to clamp the rotation axis so that it face upside down.

Here is a clip of how it currently looks like.

Let me load another one…

I have tried one method. Storing the mouse delta in a variable then simply stopping adding movement if that variable gets over 89 and -89. However that doesn’t work when the orientation of the character is different.

In other words I need to clamp the cameras rotation relative to the players body.

One other thing. I did try setting it using the ToWorldSpace method and instead using it like that however it seemed that the camera would rotate around the player if it rotated. I do not want this.

Any help is appreciated.

Here is the script I currently have as of now.

-- This is run every frame
local function UpdateCamera(DeltaTime)
    local MouseDelta = UserInputService:GetMouseDelta() * GameSettings.MouseSensitivity
	CameraRotation = CameraRotation + MouseDelta --  this was the variable I used previously to do the clamping.
	
	CameraRotationMatrix *= CFrame.new(0,0,0,rad(MouseDelta.Y),rad(MouseDelta.X),0,qW)
	
	SubjectPosition = SubjectPosition:Lerp(
		Character.PrimaryPart.Position,
		DeltaTime * 5
	)
	Camera.CFrame =  CFrame.lookAt(
		Camera.CFrame.Position:Lerp(
			Character.PrimaryPart.Position+(CameraRotationMatrix.LookVector * -CameraDistance),
			DeltaTime * 2
		),
		SubjectPosition,
		Camera.CFrame.UpVector:Lerp(
			CameraRotationMatrix.UpVector,
			DeltaTime * 3
		)
	)
end

I made something like this before, It was a long time ago but I can go back and look at my code to see what you have wrong.

The rotations of the camera are fine. I like them like that. However, I would like to know if there is a way I can clamp the movement of this camera relative to the Character.

This video is better frame rate

Currently it can go anywhere.

Never dealt with anything like that before, This might be able to help you

1 Like

Thanks for that post. It might work. I have to try it out first. I will see if this works and if so thanks.

1 Like

If it does not work get back to me

Have you found the answer?

I can’t seem to clamp my camera on the X or the Y Axis.

I've tried ToWorldSpace, XVector.X, Difference Between Head.CFrame and CurrentCamera.CFrame (closest I've gotten to get correct difference between camera and head, still read only)
self.Vector2_CameraRotation			+=	Service_UserInputService:GetMouseDelta() * self.number_CameraMouseRotateSpeed --Anti Invert
				
local cameraRotationCFrame			=	CFrame.Angles(0, self.Vector2_CameraRotation.X, 0) * CFrame.Angles(self.Vector2_CameraRotation.Y, 0, 0)
				
game.Workspace.CurrentCamera.CFrame	=	cameraRotationCFrame + Player_Local.Character.Head.Position

Same problem as yours

On the output you can see the difference between Head and Camera