CFrame Camera Orientation Issue

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 would like the player’s camera to rotate ninety degrees on the Y axis when the player presses ‘Q’, while being oriented slightly downwards.

  2. What is the issue? Include screenshots / videos if possible!
    The camera is angled slightly downwards when its Y orientation is 0 or 180, as it is meant to be, but when the camera’s Y orientation is 90 or -90, it is not oriented downwards at all.
    Screenshots:
    The following is what appears when the camera is oriented at 0 or 180 degrees on the Y axis (This is intended to happen):


    The following is what appears when the camera is oriented at 90 degrees on the Y axis (This is not intended to happen):

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried switching the X and Z arguments for the CFrame.Angles constructor, but this yields the same result. I searched the Developer Hub and Developer Forum for similar issues or potential solutions, but I was unable to find any working solution.

The following is a relevant portion of the script responsible for setting the orientation of the player’s camera:

local camera = game.Workspace.CurrentCamera
wait()
camera.CameraType = Enum.CameraType.Scriptable
local cameraOrientation = Vector3.new(-25, 0, 0)
camera.CFrame = CFrame.new(0, 50, 0)
game:GetService("UserInputService").InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q then
		if cameraOrientation.Y > -2.5 and cameraOrientation.Y < 2.5 then
			camera.CFrame = CFrame.new(camera.CFrame.X, camera.CFrame.Y, camera.CFrame.Z) * CFrame.Angles(0, math.rad(90), math.rad(25))
			cameraOrientation = Vector3.new(0, 90, 25)
		elseif cameraOrientation.Y > 87.5 and cameraOrientation.Y < 92.5 then
			camera.CFrame = CFrame.new(camera.CFrame.X, camera.CFrame.Y, camera.CFrame.Z) * CFrame.Angles(math.rad(25), math.rad(180), 0)
			cameraOrientation = Vector3.new(25, 180, 0)
		elseif cameraOrientation.Y > 177.5 then
			camera.CFrame = CFrame.new(camera.CFrame.X, camera.CFrame.Y, camera.CFrame.Z) * CFrame.Angles(0, math.rad(-90), math.rad(-25))
			cameraOrientation = Vector3.new(0, -90, -25)
		elseif cameraOrientation.Y > -92.5 and cameraOrientation.Y < -87.5 then
			camera.CFrame = CFrame.new(camera.CFrame.X, camera.CFrame.Y, camera.CFrame.Z) * CFrame.Angles(math.rad(-25), 0, 0)
			cameraOrientation = Vector3.new(-25, 0, 0)
		end
	end
end)

Switching line 9 with the following yields exactly the same result:

camera.CFrame = CFrame.new(camera.CFrame.X, camera.CFrame.Y, camera.CFrame.Z) * CFrame.Angles(math.rad(25), math.rad(90), 0)

Any help is greatly appreciated.

CFrame angles are applied in ZYX order, which means that you won’t be able to get the desired result with just one CFrame.Angles. If you want more control over this, I suggest splitting up the angles into a different order by applying them separately. If you wanted to do a YXZ order for example, use

CFrame.Angles(0, y, 0) * CFrame.Angles(x, 0, 0) * CFrame.Angles(0, 0, z)
3 Likes