How to switch camera to first person when I click F and make it switch back to 3rd when I click f again

As of now, I am trying to make a system so that when the player presses the key “f”, the camera switches from a 2D view (Platformer view) to a 3D First Person view, and vice versa when they press the f key. The issue for instance is when I press the key “f” it switches to a 3D Third Person instead of a first-person view. Is there a way to make it first-person instead?

-- -- local camera = game.Workspace.CurrentCamera
local player = game.Players.LocalPlayer
local UserInputService = game:GetService("UserInputService")

local cameraDistance = 30
local cameraHeight = 0

local firstPersonMode = false

game:GetService("RunService").RenderStepped:Connect(function()
    local character = player.Character
    if character and character:FindFirstChild("Humanoid") and character:FindFirstChild("HumanoidRootPart") then
        local torso = character.HumanoidRootPart
        local cameraPosition
        
        if firstPersonMode then
            cameraPosition = torso.Position
        else
            cameraPosition = Vector3.new(torso.Position.X, torso.Position.Y + cameraHeight, torso.Position.Z - cameraDistance)
        end

        if torso:FindFirstChild("FastStart") == nil then
            camera.CFrame = CFrame.new(cameraPosition, torso.Position)
        else
            -- Lower camera for fast start
            camera.CFrame = CFrame.new(cameraPosition - Vector3.new(0, 15, 0), torso.Position)
        end
    end
end)

UserInputService.InputBegan:Connect(function(input, isProcessed)
    if isProcessed then return end

    if input.KeyCode == Enum.KeyCode.F then
		firstPersonMode = not firstPersonMode
    end
end)
12 Likes

If you want to force the player to go into 3rd person, you can use the CameraMinZoomDistance property in the Player instance and set it to the distance you want it to.
afbeelding

If you want to force the player into 1st person, you can set the CameraMode to Enum.CameraMode.LockFirstPerson.

3 Likes

I want to create a script where you can switch between first person and third person, when I tried Enum.CameraMode.LockFirstPerson in my script it just locked it to the first person only once I switched. The problem I am having is when I click f on my script, my camera just locks on my screen and stays in the third person, and when I click f again it goes back to third person and follow my character.

5 Likes

First of all, use ContextActionService for context actions like this. (This is not a context action, but still better to use in this case) As for the lockfirstperson, check if the camera is already set to that, if it is then set the cameramode to classic and it will auto focus the character.

This is a baseline, paste it in a localscript that’s in StarterPlayerScripts

local CAS = game:GetService("ContextActionService")
local Players = game:GetService("Players")

local plr = Players.LocalPlayer

local function toggleCameraMode(inputname, inputstate)
	if inputstate ~= Enum.UserInputState.Begin then return end
	local camera = game.Workspace.CurrentCamera
	if plr.CameraMode == Enum.CameraMode.Classic then
		plr.CameraMode = Enum.CameraMode.LockFirstPerson
	else
		plr.CameraMode = Enum.CameraMode.Classic
	end
end

CAS:BindAction("ToggleCamera", toggleCameraMode, true, Enum.KeyCode.F)

Note that when the cameramode becomes classic, it won’t automatically zoom out for them, you’ll have to do that manually with code if you want, all it does is let them zoom out.

I tried using putting your code in my, but it’s not working:

local camera = game.Workspace.CurrentCamera
local Players = game.Players.LocalPlayer
local CAS = game:GetService(“ContextActionService”)
local plr = Players.LocalPlayer

local cameraDistance = 30
local cameraHeight = 0

local firstPersonMode = false

game:GetService(“RunService”).RenderStepped:Connect(function()
local character = Players.Character
if character and character:FindFirstChild(“Humanoid”) and character:FindFirstChild(“HumanoidRootPart”) then
local torso = character.HumanoidRootPart
local cameraPosition

    if firstPersonMode then
        cameraPosition = torso.Position
    else
        cameraPosition = Vector3.new(torso.Position.X, torso.Position.Y + cameraHeight, torso.Position.Z - cameraDistance)
    end

    if torso:FindFirstChild("FastStart") == nil then
        camera.CFrame = CFrame.new(cameraPosition, torso.Position)
    else
        -- Lower camera for fast start
        camera.CFrame = CFrame.new(cameraPosition - Vector3.new(0, 15, 0), torso.Position)
    end
end

end)

local function toggleCameraMode(actionName, inputState, inputObject)
if inputState ~= Enum.UserInputState.Begin then return end
local camera = game.Workspace.CurrentCamera
if plr.CameraMode == Enum.CameraMode.Classic then
plr.CameraMode = Enum.CameraMode.LockFirstPerson
else
plr.CameraMode = Enum.CameraMode.Classic
end
end

CAS:BindAction(“ToggleCamera”, toggleCameraMode, true, Enum.KeyCode.F)

1 Like

what did you even just send… Can you put the script in a codeblock please?

--local camera = game.Workspace.CurrentCamera
local Players = game.Players.LocalPlayer
local CAS = game:GetService("ContextActionService")
local plr = Players.LocalPlayer

local cameraDistance = 30
local cameraHeight = 0

local firstPersonMode = false

game:GetService("RunService").RenderStepped:Connect(function()
    local character = Players.Character
    if character and character:FindFirstChild("Humanoid") and character:FindFirstChild("HumanoidRootPart") then
        local torso = character.HumanoidRootPart
        local cameraPosition

        if firstPersonMode then
            cameraPosition = torso.Position
        else
            cameraPosition = Vector3.new(torso.Position.X, torso.Position.Y + cameraHeight, torso.Position.Z - cameraDistance)
        end

        if torso:FindFirstChild("FastStart") == nil then
            camera.CFrame = CFrame.new(cameraPosition, torso.Position)
        else
            -- Lower camera for fast start
            camera.CFrame = CFrame.new(cameraPosition - Vector3.new(0, 15, 0), torso.Position)
        end
    end
end)
    
local function toggleCameraMode(actionName, inputState, inputObject)
    if inputState ~= Enum.UserInputState.Begin then return end
    local camera = game.Workspace.CurrentCamera
    if plr.CameraMode == Enum.CameraMode.Classic then
        plr.CameraMode = Enum.CameraMode.LockFirstPerson
    else
        plr.CameraMode = Enum.CameraMode.Classic
    end
end

CAS:BindAction("ToggleCamera", toggleCameraMode, true, Enum.KeyCode.F)

That’s not the script i gave you, try using

local CAS = game:GetService("ContextActionService")
local Players = game:GetService("Players")

local plr = Players.LocalPlayer

local function toggleCameraMode(inputname, inputstate)
	if inputstate ~= Enum.UserInputState.Begin then return end
	local camera = game.Workspace.CurrentCamera
	if plr.CameraMode == Enum.CameraMode.Classic then
		plr.CameraMode = Enum.CameraMode.LockFirstPerson
	else
		plr.CameraMode = Enum.CameraMode.Classic
	end
end

CAS:BindAction("ToggleCamera", toggleCameraMode, true, Enum.KeyCode.F)
2 Likes

1 Like

Try using the script I gave you as a baseline to start, then work off of it to how you want your camera physics to work.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.