How to lock the camera in first person with a script

Hello, im making hiding spots for my game and im faced with a problem.

I want it so that when the player hide under the table, the camera get locked in first person and get back to normal when the player get out of the hiding spot. How do I achieve that ?

Here is the script if it help:

local TweenService = game:GetService("TweenService")
local TweenInfo = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0)

local Camera = workspace.CurrentCamera
local HideCameraPos = workspace.House.Floor1.DiningRoom.DiningTable.Table.HideCameraPos
local PlayerPos = HideCameraPos.Parent.PlayerPos
local CamPos = HideCameraPos.Parent.CamPos

local Prompt = HideCameraPos.Attachment.HidePrompt

local Hidden = false

Prompt.Triggered:Connect(function(player)
	if Hidden == false then
		local HideTween = TweenService:Create(Camera, TweenInfo, {CFrame = HideCameraPos.CFrame})
		
		PlayerPos.CFrame = player.Character:FindFirstChild("Head").CFrame
		CamPos.CFrame = Camera.CFrame
		Camera.CameraType = Enum.CameraType.Scriptable
		
		Camera.CameraSubject = script.Parent:FindFirstChild("Head")
		
		HideTween:Play()
		wait(1)
		
		Camera.CameraType = Enum.CameraType.Custom
		
		Prompt.Enabled = true
		Prompt.ActionText = "Get out"
		
		Hidden = true
		
	elseif Hidden == true then
		local GetOutTween = TweenService:Create(Camera,TweenInfo, {CFrame = CamPos.CFrame})
		
		GetOutTween:Play()
	
		Prompt.ActionText = "Hide"
		wait(1)
		
		Camera.CameraSubject = script.Parent:FindFirstChildWhichIsA("Humanoid")
		
		Camera.CameraType = Enum.CameraType.Custom
		PlayerPos.CFrame = HideCameraPos.CFrame
		CamPos.CFrame = HideCameraPos.CFrame
		
		Hidden = false
	end
end)

You can set the player’s CameraMode.

--"player" is the Player object
player.CameraMode = Enum.CameraMode.Custom
--or
player.CameraMode = Enum.CameraMode.LockFirstPerson

Note this will only set the CameraMode - If Custom is enabled after LockFirstPerson was previously enabled, it will appear zoomed in, but the camera can be moved out of first-person mode.

1 Like

Oh ok, I thought the CameraMode was directly on the Camera, that’s why I did not understand, thank you !

1 Like

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