Building Camera flips over

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
  • I want my camera to not flip over whenever i look down or up.
  1. What is the issue? Include screenshots / videos if possible!

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
  • I did but none were by my topic.

This is my code.

RunService.RenderStepped:Connect(function()
	if CM then
		local Mouse = P.Owner.Value:GetMouse()
		
		Mouse.WheelForward:Connect(function()
			workspace.CurrentCamera.CFrame = workspace.CurrentCamera.CFrame * CFrame.new(0, 0, -.01)
		end)
		
		Mouse.WheelBackward:Connect(function()
			workspace.CurrentCamera.CFrame = workspace.CurrentCamera.CFrame * CFrame.new(0, 0, .01)
		end)
		
		if B2D then
			UIS.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
			
			local Delta = UIS:GetMouseDelta()
			
			local limit = 80

			workspace.CurrentCamera.CFrame = workspace.CurrentCamera.CFrame * CFrame.Angles(-math.rad(Delta.Y), -math.rad(Delta.X), 0)
		else
			UIS.MouseBehavior = Enum.MouseBehavior.Default
		end
		
		if W then
			workspace.CurrentCamera.CFrame = workspace.CurrentCamera.CFrame * CFrame.new(0, 0, -.3)
		end
		
		if A then
			workspace.CurrentCamera.CFrame = workspace.CurrentCamera.CFrame * CFrame.new(-.3, 0, 0)
		end
		
		if S then
			workspace.CurrentCamera.CFrame = workspace.CurrentCamera.CFrame * CFrame.new(0, 0, .3)
		end
		
		if D then
			workspace.CurrentCamera.CFrame = workspace.CurrentCamera.CFrame * CFrame.new(.3, 0, 0)
		end
		
		if E then
			workspace.CurrentCamera.CFrame = workspace.CurrentCamera.CFrame * CFrame.new(0,.3,0)
		end
		
		if Q then
			workspace.CurrentCamera.CFrame = workspace.CurrentCamera.CFrame * CFrame.new(0,-.3,0)
		end
	end
end)

I hope someone could help.

3 Likes

Compare the camera’s currentX rotation with the new one, and if its over (or under) a threshold set (in radians), then just set that rotation to the max rotation instead of adding to it. Otherwise, you can add as normal.

You can get the current camera’s X rotation component with

local cameraXRads = select(2, camera.CFrame:ToOrientation())
2 Likes

Well, i’ve seen that code in some topics and i saw that the building camera worked for others with that piece of code and some changes, but i do not know how to use it.

1 Like

If you cannot do it from that explanation, here is the code I use. You can adapt it to your needs.

camOrientationX = if math.abs(camOrientationX) > MAX_ANGLE_X then MAX_ANGLE_X * math.sign(camOrientationX) else camOrientationX
desiredCamCFrameRotation = CFrame.fromEulerAnglesYXZ(camOrientationX, camOrientationY, 0) 
2 Likes