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)
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.
If you want to force the player into 1st person, you can set the CameraMode to Enum.CameraMode.LockFirstPerson.
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.
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
--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)
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)