Can't rotate the player's camera

Hi, I’ve been trying for days to make a game where the 2d camera becomes at some point in the game a 3d camera (the defaut roblox camera, but in first person).
The problem is that the camera is stuck in a certain position and there is no way to rotate it.

This is the script I used to reset the camera:

local player = game.Players.LocalPlayer
local camera = game.workspace.CurrentCamera

camera.CameraType = Enum.CameraType.Custom
camera.CameraSubject = player.Character.Humanoid

This is the 2d camera script:

local player = game.Players.LocalPlayer
local camera = game.Workspace.CurrentCamera

player.CharacterAdded:Wait()
player.Character:WaitForChild("HumanoidRootPart")

camera.CameraSubject = player.Character.HumanoidRootPart
camera.CameraType = Enum.CameraType.Attach

game:GetService('RunService').Stepped:Connect(function()
camera.CFrame = CFrame.new(player.Character.HumanoidRootPart.Position * CFrame.new(0, 0, 15)
end)

2d camera script is disabled when camera reset script is enabled.

(Sorry if I did something wrong in the post, it’s the first time I’ve done one)

1 Like

Sometimes there is a problem when you set the CameraType to Custom it doesn’t change (at least I remember this happening).
If my assumption is right, and this is the reason why this is happening, considering repeatdly changing the CameraType until it is set to Custom.

camera.CameraType =  Enum.CameraType.Scriptable
repeat task.wait() camera.CameraType = Enum.CameraType.Scriptable until (camera.CameraType == Enum.CameraType.Scriptable)
1 Like

I would assume that this is where you are setting the angle for rotation, if so, try using CFrame.Angles() or CFrame.fromEulerAnglesXYZ() instead (They do the same thing). You will also want to make sure that you are using radians for that.

1 Like

I tried it but it didn’t work :confused:

1 Like

I’m kind of new to scripting, do you know any links or videos where I can see how radians work?

1 Like

Radians have nothing to do with scripting, they are just another form of measuring angles.

A radian is a circles radius put around the arc of the circle. It fits exactly 2π (pi) times around the circle.

A normal circle has 360 degrees in it, which is also 2π radians in it. The conversion for radians to degrees is (angle in radians) * 180/π or math.deg(angle) and from degrees to radians is (angle in degrees) * π/180 or math.rad(angle)

here is a simple diagram showing this:
image

this shows the simple conversions from the most common angles to their radian form.
This video also explains what a radian is pretty well.

In ROBLOX, when you use CFrame angles, is takes in a number in radians, so you will just need to make sure to use the radians when you input an angle that you want to put in.

Hope this will help you to understand! Have a merry Christmas! :christmas_tree:

1 Like

Sorry for the late reply, but I didn’t quite understand what to do. I just want to reset the camera to put it in first person, but I can’t find the solution.

To force a player into first person, you can just change the players camera mode by doing

game.Players.LocalPlayer.CameraMode = Enum.CameraMode.LockFirstPerson
1 Like

I know, but the camera is still stuck in one place :frowning:

Do you mean the camera is stuck at one position every time you spawn in, and it still gets stuck there even if you disable the script (like you’ve mentioned)?

If your local script is named CameraScript, try renaming it to something else.

Hey, I changed the script a little bit and it looks like it’s improved a little bit:

local player = game.Players.LocalPlayer
local camera = game.Workspace.CurrentCamera
local TScript = game.ReplicatedStorage.TScript

player.CharacterAdded:Wait(0.1)
player.Character:WaitForChild("HumanoidRootPart")

camera.CameraSubject = player.Character.HumanoidRootPart
camera.CameraType = Enum.CameraType.Attach

game:GetService('RunService').Stepped:Connect(function()
camera.CFrame = CFrame.new(player.Character.HumanoidRootPart.Position * CFrame.new(0, 0, 15)
end)

repeat wait() until TScript.Disabled == false

if TScript.disabled == false then
camera.CameraSubject = player.Character.Humanoid
camera.CameraType = Enum.CameraType.Custom

The camera follows the player and is no longer stuck, but I can’t move the camera with the mouse or zoom.

The camera was stuck when I put its type as custom, but now I’ve solved it. The problem is that now I can’t move the camera, nor can I put it in first person

Simply put the custom as scriptable cause you can’t script it if it is custom.

repeat wait() until TScript.Disabled == false

if TScript.disabled == false then
camera.CameraSubject = player.Character.Humanoid
camera.CameraType = Enum.CameraType.Custom

Is this part of your code meant to reset the camera?

Yes, I put everything in the same code because it wasn’t working

I tried it but it still didn’t work.

In that case, I think you need to disconnect the Stepped connection so that the camera’s CFrame isn’t constantly being set to their back. This will let them move their camera around normally.

Something like…

local connection = game:GetService('RunService').Stepped:Connect(function()
    camera.CFrame = CFrame.new(player.Character.HumanoidRootPart.Position * CFrame.new(0, 0, 15)
end)
...
if TScript.Disabled == false then
    connection:Disconnect()
    ...
end

…, where “...” is whatever code you need to have, should solve the problem of the camera snapping back.


I also noticed this line in particular doesn’t seem right because you can’t multiply a Vector3 with a CFrame, and there’s a “)” missing.

camera.CFrame = CFrame.new(player.Character.HumanoidRootPart.Position * CFrame.new(0, 0, 15)

Did you mean something like this instead? I added a bracket to close the first CFrame.new() around the HumanoidRootPart’s position.

camera.CFrame = CFrame.new(player.Character.HumanoidRootPart.Position) * CFrame.new(0, 0, 15)

I’m not sure how you’re making your game look 2D (top-down experience etc.), but I hope the connection part helps.

Thanks for trying to help me :), but unfortunately it didn’t work. On the line where you said I wrote it wrong, I just wrote it wrong here, in studio it’s normal.