Camera Rotation When a Button Is Pressed

Hello there, so I’ve recently started up a new project for myself to try making a 2.75d puzzle game that has a rewind mechanic however right now I need help in making a camera rotation mechanic to where if the player presses Q then the camera rotates 90 Degrees to the left, E will make the camera rotate 90 Degrees to the right

workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable

game:GetService("RunService").RenderStepped:Connect(function()
	
	    workspace.CurrentCamera.CFrame = CFrame.new(script.Parent.PrimaryPart.Position) * CFrame.Angles(math.rad(-32.5),0,0) + Vector3.new(0, 10, 13.5)
end)

Heres the script I have right now, I need help in making a rotation mechanic based on the keys you press on your keyboard

1 Like

You can use UserInputService to detect these keyUp and keyDown events (suggested only if plan on emulating this for PC) using inputBegan and inputEnded. You can set a variable called “left” and “right” that the renderstep will check to move your camera.

local left = false
local right = false

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(inputObject, gameProcessed)
	if gameProcessed then return end
	if inputObject.KeyCode == Enum.KeyCode.Q then
		left = true
	elseif inputObject.KeyCode = Enum.KeyCode.E then
		right = true
	end
    end
end

UserInputService.InputEnded:Connect(function(inputObject, gameProcessed)
	if gameProcessed then return end
	if inputObject.KeyCode == Enum.KeyCode.Q then
		left = false
	elseif inputObject.KeyCode == Enum.KeyCode.E then
		right = false
	end
end
-- UserInputService that change the Boolean on "left" and "right"

local function moveLeft()
	workspace.CurrentCamera.CFrame = CFrame.new(script.Parent.PrimaryPart.Position) * CFrame.Angles(math.rad(-32.5),0,0) + Vector3.new(0, 10, 13.5)
end

local function moveRight()
	workspace.CurrentCamera.CFrame = CFrame.new(script.Parent.PrimaryPart.Position) * CFrame.Angles(math.rad(-32.5),0,0) + Vector3.new(0, 10, 13.5)
end
-- CHANGE THESE TO MATCH THE ANGLE YOU WANT

workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
game:GetService("RunService").RenderStepped:Connect(function()
	if left == right then return end
	if left then
		moveLeft()
	elseif right then
		moveRight()
	end
end)

Hey, so I’m pretty much doing the same thing. But I achieved the same result as you guys. But when I double-click Q, it rotates 90* twice, so the camera turns around. How can I fix this?

(SORRY I’M LATE)

Use math.clamp() for that.
I hope this helps