Making it for the camera can't turn backwards

Hello, I’m scripting a single player game which is locked in first person. I’m trying to make it for the camera can’t face backwards, or it can only rotate to a certain degree (ex: can only rotate 180 degrees but then can’t rotate the other 180 degrees). I’ve tried a bunch of methods like CFrame.Angles and ToWorldSpace with the Camera CFrame, but I can never get a solution that works. Any help is appreciated!

3 Likes

Please excuse me asking but have you tried a search in your favourite browser like
roblox limit camera rotation
and seen if that helps you?

2 Likes

Yes I have (not in those exact words but similar) and I find articles like this:How to clamp the player's camera rotation on one axis? but I have no idea how to incorporate them into my problem.

The forum has a search bar, but the “browser” (i.e., google, yahoo, etc…) will give you the information you need. The wiki actually has a good camera documentation last time I checked.

Yea it has a good camera documentation, but I’ve already been over it several times. I’m not sure if I’m just missing the point of the article or it doesn’t have anything I can use as a solution, but I would rather just be given advice then be told to go to the wiki… (usually I would use the wiki, but I am not great at CFrame math so that’s why I’m asking here)

ARTICLE: Customizing the Camera | Documentation - Roblox Creator Hub

Alright, I understand. Would you mind posting the code you have so I can get a feel for it?

Also, this might be of use as well Camera | Documentation - Roblox Creator Hub

Yea sure. I made this script after I had looked at a few other articles, but it seems to limit your turning ability (on the x axis) to just 1 degree so you can’t turn your head atall but it gives movement freedom on the Y axis.
indent preformatted text by 4 spaces

    local RunService = game:GetService("RunService")
    local camera = workspace.CurrentCamera
    RunService:BindToRenderStep("UpdateLoop", Enum.RenderPriority.Camera.Value, function()

local rX, rY, rZ = camera.CFrame:ToOrientation()
print(rX,rY,rZ)
local limY = math.clamp(math.deg(rX),-45, 45)
camera.CFrame = CFrame.new(camera.CFrame.p) * CFrame.fromOrientation(rX,math.rad(limY), rZ)
end)

You could do CameraType = “Attach”.
The api reference reads:
Attach: Camera moves with the Camera.CameraSubject at a fixed offset and will rotate as the subject rotates.

Just set the offset to nothing or where ever you want.

The player rotates their character and so does the camera.

Hope this helps!
Someperson576

You’re clamping the X to the Y axis, which is the source of your problem.

local RunService = game:GetService("RunService")
local camera = workspace.CurrentCamera
RunService:BindToRenderStep("UpdateLoop", Enum.RenderPriority.Camera.Value, function()
local rX, rY, rZ = camera.CFrame:ToEulerAnglesXYZ() -- what i prefer doing
    local limX = math.clamp(math.deg(rX),-45, 45)
    camera.CFrame = CFrame.new(camera.CFrame.Position) * CFrame.Angles(limX, rY, rZ) -- fromOrientation is literally the same as .Angles
end) 

If I’m not certain, the X axis is up and down with CFrame.

Oh thanks! Your script seems to be making ovals for me though lol: https://gyazo.com/2dd7194ab06bb8478fed7b0a0e5637da

Also, how would I be able to use this to limit how much you can turn your camera right to left? Would I subsisute the rY and limY with this to make it do that?

local RunService = game:GetService("RunService")
local camera = workspace.CurrentCamera
RunService:BindToRenderStep("UpdateLoop", Enum.RenderPriority.Camera.Value, function()
 local rX, rY, rZ = camera.CFrame:ToEulerAnglesXYZ() -- what i prefer doing
 local limY = math.clamp(45,-math.deg(rY), 45)
 camera.CFrame = CFrame.new(camera.CFrame.Position) * CFrame.Angles(rX, limY, rZ) 
  end)

Do you want to limit how fast they can turn their camera? I’d imagine you’d just do something like this:

local limY = math.clamp(rX, rX - math.pi/4, rX + math.pi/4) -- in radians

No, I’m trying to make it for the player can only turn their camera 180 degrees frontwards and can’t turn it anywhere in the other 180 degrees which is behind them. I basically want it for they can’t turn their camera backwards (its a first person locked game)

That’s relatively simple.

local limY = math.clamp(rY, 0, math.pi) -- radians again

Keep in mind, this might only work in one axis, so if you want to base it of where your camera is already sitting then you can do this:

local RunService = game:GetService("RunService")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait() -- just incase the player doesn't spawn, then you'd wait here.
local camera = workspace.CurrentCamera

local _, oldY, _ = camera.CFrame:ToEulerAnglesXYZ()

RunService:BindToRenderStep("UpdateLoop", Enum.RenderPriority.Camera.Value, function()
    local rX, rY, rZ = camera.CFrame:ToEulerAnglesXYZ()
    local limY = math.clamp(rY, oldY - math.pi / 2, oldY + math.pi / 2)-- a full 180 degrees
    camera.CFrame = CFrame.new(camera.CFrame.Position) * CFrame.Angles(rX, limY, rZ)
end)
4 Likes

Oh, that worked perfectly! Thanks a lot, been trying to figure this for days.

1 Like