PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

Focus

Camera

CFrame

The Camera Focus is a DataType/CFrame that determines the area in 3D space the graphics engine will prioritize.

Certain graphical operations Roblox performs, such as updating lighting, can take a lot of time or computational effort to complete. Focus tells Roblox the area in 3D space to prioritize when performing such operations. For example dynamic lighting from objects such as PointLight|PointLights may not render at distances far from the Focus.

The default Roblox camera scripts automatically set the Focus to follow the Camera/CameraSubject (usually a Humanoid). However, Focus will not be automatically updated in the following cases:

  • When the Camera/CameraType is set to ‘Scriptable’
  • When the default camera scripts are not being used

In these cases, you should update Focus every frame, using RunService/BindToRenderStep function at the ‘Camera’ Enum/RenderPriority.

Focus has no bearing on the positioning or orientation of the Camera (see Camera/CFrame|Camera.CFrame for this).


Code Samples


Following Camera

When the following code is placed inside a LocalScript named ‘CameraScript’ parented to StarterPlayerScripts|StarterPlayer.StarterPlayerScripts the default Roblox camera scripts are overwritten and replaced with a custom following camera.

-- Make variables for Roblox services
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
 
-- Make variable for the camera offset
local OFFSET = Vector3.new(20,20,20)
 
-- Make variables for the player and camera
local player = Players.LocalPlayer
local camera = game.Workspace.CurrentCamera
 
-- Detatch the character's rotation from the camera
UserSettings().GameSettings.RotationType = Enum.RotationType.MovementRelative

-- Function to call on the render step
local function onRenderStep()
	-- Check if the player's character exists and if that character'script
	-- HumanoidRootPart exists
	local character = player.Character
	if character then
		local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
		if humanoidRootPart then
			-- Update the position of the camera
			local playerPosition = humanoidRootPart.Position
			local cameraPosition = playerPosition + OFFSET
			camera.CoordinateFrame = CFrame.new(cameraPosition, playerPosition)
 
			-- Update the focus of the camera to follow the character
			camera.Focus = humanoidRootPart.CFrame
		end
	end
end
 
-- Binds function to render step at camera priority
RunService:BindToRenderStep("Camera", Enum.RenderPriority.Camera.Value, onRenderStep)