Custom camera movement acting strangely

Hi!
I’ve made a custom camera movement for my new game that works good, until I wanna turn the camera. When that happens the movement is inverted and not smooth.

I will provide footage of it working properly and it not working properly.

WORKING FOOTAGE
https://gyazo.com/bc76c8ad92189cb25197d60515d40c21

WORKING SCRIPT

Mouse.Move:Connect(function(input)
		local MousePosition = Mouse.Hit.Position
		
		local cameraCFrame = CFrame.lookAt(ReplicatedStorage:WaitForChild("DefaultPosition").Position, MousePosition)
		local X, Y, Z = cameraCFrame:ToEulerAnglesXYZ()
		local maxRotation = math.rad(20)
		
		local newRotation = Vector3.new(
			math.clamp(X, -maxRotation, maxRotation),
			math.clamp(Y, -maxRotation, maxRotation),
			0
		)
		newRotation = CFrame.fromEulerAnglesXYZ(newRotation.X, newRotation.Y, newRotation.Z) 
		local finalCFrame = newRotation + ReplicatedStorage:WaitForChild("DefaultPosition").Position 
		
		local Tween = TweenService:Create(self.Camera, TweenInfo.new(0.4), {CFrame = finalCFrame})
		Tween:Play()
	end)

WHEN I TRY TO TURN IT
https://gyazo.com/e64817c0d5382ffc0e63ba276f552367

THE NOT WORKING SCRIPT

Mouse.Move:Connect(function(input)
		local MousePosition = Mouse.Hit.Position
		
		local cameraCFrame = CFrame.lookAt(ReplicatedStorage:WaitForChild("DefaultPosition").Position, MousePosition)
		local X, Y, Z = cameraCFrame:ToEulerAnglesXYZ()
		local maxRotation = math.rad(20)
		
		local newRotation = Vector3.new(
			math.clamp(X, -maxRotation, maxRotation),
			math.clamp(Y, -maxRotation, maxRotation),
			0
		)
		newRotation = CFrame.fromEulerAnglesXYZ(newRotation.X, newRotation.Y, newRotation.Z) 
		local finalCFrame = (newRotation + ReplicatedStorage:WaitForChild("DefaultPosition").Position) * CFrame.Angles(0, math.rad(180), 0) 
		
		local Tween = TweenService:Create(self.Camera, TweenInfo.new(0.4), {CFrame = finalCFrame})
		Tween:Play()
	end)

I basically want to turn the camera without the movement breaking.
Thanks for any help!

Don’t use tween for camera movement. Instead, use CFrame:Lerp()

1 Like

This is irrelevant to my issue

1 Like

My bad entirely, didn’t read the text thorougly. I thought it was that it was being slow and unresponsive.

1 Like

You added in the second script:
CFrame.Angles(0, math.rad(180), 0) on local finalCFrame

This bit right here is not going to work. You’re clamping the camera to +/- 20 degrees rotation on the y-axis from an identity orientation in world space.

This is not the same as:
math.clamp(Y, centerY-maxRotation, centerY+maxRotation)

where centerY would be the Y-axis angle from the camera’s initial look direction (i.e. the middle position)

What ease are you using for the tweenInfo

This did turn it slightly but did broke the movement again.
I used

local centerY = math.rad(180)
math.clamp(Y, centerY-maxRotation, centerY+maxRotation)

This is irrelevant to my issue