Reference API Roblox

Engine API

Website

Related

Reference API Roblox

Camera

A class which defines a view of the 3D world.

This class is not replicated. Its interface does not cross the network boundary.
Tags: [NotReplicated]

Member index 35

HistoryMember
553CFrame: CFrame
553CameraSubject: Instance
553CameraType: CameraType
580DiagonalFieldOfView: float
580FieldOfView: float
553FieldOfViewMode: FieldOfViewMode
553Focus: CFrame
553HeadLocked: bool
553HeadScale: float
580MaxAxisFieldOfView: float
553NearPlaneZ: float
577VRTiltAndRollEnabled: bool
553ViewportSize: Vector2
553focus: CFrame
648GetLargestCutoffDistance(ignoreList: Instances): float
582GetPanSpeed(): float
648GetPartsObscuringTarget(castPoints: Array, ignoreList: Instances): Instances
462GetRenderCFrame(): CFrame
462GetRoll(): float
582GetTiltSpeed(): float
573Interpolate(endPos: CFrame, endFocus: CFrame, duration: float): null
573PanUnits(units: int): null
486ScreenPointToRay(x: float, y: float, depth: float = 0): Ray
598SetCameraPanMode(mode: CameraPanMode = Classic): null
573SetImageServerView(modelCoord: CFrame): null
573SetRoll(rollAngle: float): null
462TiltUnits(units: int): bool
486ViewportPointToRay(x: float, y: float, depth: float = 0): Ray
486WorldToScreenPoint(worldPoint: Vector3): Tuple
486WorldToViewportPoint(worldPoint: Vector3): Tuple
462Zoom(distance: float): bool
573ZoomToExtents(boundingBoxCFrame: CFrame, boundingBoxSize: Vector3): null
462FirstPersonTransition(entering: bool)
462InterpolationFinished()
inherited from Instance
553Archivable: bool
635Capabilities: SecurityCapabilities
553Name: string
553Parent: Instance
635Sandboxed: bool
616UniqueId: UniqueId
576AddTag(tag: string): null
573ClearAllChildren(): null
462Clone(): Instance
573Destroy(): null
486FindFirstAncestor(name: string): Instance
486FindFirstAncestorOfClass(className: string): Instance
486FindFirstAncestorWhichIsA(className: string): Instance
486FindFirstChild(name: string, recursive: bool = false): Instance
486FindFirstChildOfClass(className: string): Instance
486FindFirstChildWhichIsA(className: string, recursive: bool = false): Instance
486FindFirstDescendant(name: string): Instance
563GetActor(): Actor
486GetAttribute(attribute: string): Variant
462GetAttributeChangedSignal(attribute: string): RBXScriptSignal
631GetAttributes(): Dictionary
648GetChildren(): Instances
462GetDebugId(scopeLength: int = 4): string
486GetDescendants(): Array
486GetFullName(): string
641GetStyled(name: string): Variant
576GetTags(): Array
576HasTag(tag: string): bool
486IsAncestorOf(descendant: Instance): bool
486IsDescendantOf(ancestor: Instance): bool
580IsPropertyModified(name: string): bool
573Remove(): null
576RemoveTag(tag: string): null
580ResetPropertyToDefault(name: string): null
573SetAttribute(attribute: string, value: Variant): null
462WaitForChild(childName: string, timeOut: double): Instance
648children(): Instances
553clone(): Instance
573destroy(): null
553findFirstChild(name: string, recursive: bool = false): Instance
648getChildren(): Instances
553isDescendantOf(ancestor: Instance): bool
573remove(): null
462AncestryChanged(child: Instance, parent: Instance)
462AttributeChanged(attribute: string)
462ChildAdded(child: Instance)
462ChildRemoved(child: Instance)
462DescendantAdded(descendant: Instance)
462DescendantRemoving(descendant: Instance)
500Destroying()
553childAdded(child: Instance)
inherited from Object
647ClassName: string
647className: string
647GetPropertyChangedSignal(property: string): RBXScriptSignal
647IsA(className: string): bool
650isA(className: string): bool
647Changed(property: string)

Removed member index 1

HistoryMember
118SetCameraInputMode(mode: CameraInputMode = Classic): void

Description

The Camera object defines a view of the 3D world. In a running experience, each client has its own Camera object which resides in that client's local Workspace, accessible through the Workspace.CurrentCamera property.

The most important camera properties are:

  • CFrame which represents the position and orientation of the camera.

  • CameraType which is read by the experience's camera scripts and determines how the camera should update each frame.

  • CameraSubject which is read by the experience's camera scripts and determines what object the camera should follow.

  • FieldOfView which represents the visible extent of the observable world.

  • Focus which represents the point the camera is looking at. It's important this property is set, as certain visuals will be more detailed and will update more frequently depending on how close they are to the focus point.

See Customizing the Camera for more information on how to adjust and customize the camera's behavior.

History 144

Members 35

CFrame

TypeDefault
CFrame

This property is the CFrame of the Camera, defining its position and orientation in the 3D world. Note that some transformations, such as the rotation of the head when using VR devices, are not reflected in this property, so you should use GetRenderCFrame() to obtain the "true" CFrame of the camera.

You can move the camera by setting this property. However, the default camera scripts also set it, so you should either:

  • Set the camera CameraType to CameraType.Scriptable so that the default camera scripts will not update the camera's CFrame. This method is simplest and recommended in most cases.

  • Completely replace the default camera scripts with alternatives. This approach is only recommended if you do not need any default camera functionality.

The most intuitive way to position and orient the Camera is by using the CFrame.lookAt() constructor. In the following example, the Camera is positioned at Vector3.new(0, 10, 0) and is oriented to be looking towards Vector3.new(10, 0, 0).

1
2
3
4
5
6
7
8
9
local Workspace = game:GetService("Workspace")

local camera = Workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable

local pos = Vector3.new(0, 10, 0)
local lookAtPos = Vector3.new(10, 0, 0)

Workspace.CurrentCamera.CFrame = CFrame.lookAt(pos, lookAtPos)

Although the camera can be placed in the manner demonstrated above, you may want to animate it to move smoothly from one CFrame to another. For this, you can either:

  • Set the camera's position/orientation every frame with RunService:BindToRenderStep() and the CFrame:Lerp() method.

  • Create and play a Tween that animates the position/orientation of the camera:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    local Players = game:GetService("Players")
    local TweenService = game:GetService("TweenService")
    local Workspace = game:GetService("Workspace")
    
    local camera = Workspace.CurrentCamera
    camera.CameraType = Enum.CameraType.Scriptable
    
    local player = Players.LocalPlayer
    local character = player.Character
    if not character or character.Parent == nil then
    	character = player.CharacterAdded:Wait()
    end
    
    local pos = camera.CFrame * Vector3.new(0, 20, 0)
    local lookAtPos = character.PrimaryPart.Position
    local targetCFrame = CFrame.lookAt(pos, lookAtPos)
    
    local tween = TweenService:Create(camera, TweenInfo.new(2), {CFrame = targetCFrame})
    
    tween:Play()
    

History 4

CameraSubject

TypeDefault
Instance

CameraSubject accepts a variety of Instances. The default camera scripts respond differently to the available settings:

  • By default, the camera scripts follow the local character's Humanoid, factoring in the humanoid's current state and Humanoid.CameraOffset.

  • When set to a BasePart, the camera scripts follow its position, with a vertical offset in the case of VehicleSeats.

CameraSubject cannot be set to nil. Attempting to do so will revert it to its previous value.

To restore CameraSubject to its default value, set it to the local character's Humanoid:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")

local localPlayer = Players.LocalPlayer
local camera = Workspace.CurrentCamera

local function resetCameraSubject()
	if camera and localPlayer.Character then
		local humanoid = localPlayer.Character:FindFirstChildWhichIsA("Humanoid")
		if humanoid then
			camera.CameraSubject = humanoid
		end
	end
end

History 5

CameraType

TypeDefault
CameraTypeFixed

The default Roblox camera scripts have several built-in behaviors. Setting this property toggles between the various CameraType behaviors. Note that some camera types require a valid CameraSubject to work correctly.

The default camera scripts will not move or update the camera if CameraType is set to CameraType.Scriptable. For more information on positioning and orienting the camera manually, see CFrame.

For all CameraType settings except CameraType.Scriptable, the CameraSubject property represents the object whose position the camera's Focus is set to.

History 4

DiagonalFieldOfView

TypeDefault
float88.8765335

Sets how many degrees in the diagonal direction (from one corner of the viewport to its opposite corner) the camera can view. See FieldOfView for a more general explanation of field of view.

Note that DiagonalFieldOfView represents the field of view that is visible by the Camera rendering into the fullscreen area which may be occluded by notches or screen cutouts on some devices. See ViewportSize for more information.

This property is not replicated. Its interface does not cross the network boundary.

History 8

Tags: [NotReplicated]

FieldOfView

TypeDefault
float70

The FieldOfView (FOV) property sets how many degrees in the vertical direction the camera can view. This property is clamped between 1 and 120 degrees and defaults at 70. Very low or very high fields of view are not recommended as they can be disorientating to players.

Note that uniform scaling is enforced, meaning the vertical and horizontal field of view are always related by the aspect ratio of the screen.

Suggested uses for FieldOfView include:

  • Reducing FOV to give the impression of magnification, for example when using binoculars.
  • Increasing FOV when the player is "sprinting" to give the impression of a lack of control.

Note that FieldOfView represents the field of view that is visible by the Camera rendering into the fullscreen area which may be occluded by notches or screen cutouts on some devices. See ViewportSize for more information.

History 6

FieldOfViewMode

TypeDefault
FieldOfViewModeVertical

The camera's FieldOfView (FOV) must be updated to reflect ViewportSize changes. The value of FieldOfViewMode determines which FOV value will be kept constant.

For example, when this property is set to FieldOfViewMode.Vertical, the horizontal FOV is updated when the viewport is resized, but the vertical FOV is kept constant. If this property is set to FieldOfViewMode.Diagonal, both horizontal and vertical FOV might be changed to keep the diagonal FOV constant.

History 4

FirstPersonTransition

Parameters (1)
enteringbool

History 3

Focus

TypeDefault
CFrame

Certain graphical operations the engine performs, such as updating lighting, can take time or computational effort to complete. The camera's Focus property tells the engine which area in 3D space to prioritize when performing such operations. For example, dynamic lighting from objects such as PointLights may not render at distances far from the focus.

The default Roblox camera scripts automatically set Focus to follow the CameraSubject (usually a Humanoid). However, Focus will not automatically update when CameraType is set to CameraType.Scriptable or when the default camera scripts are not being used. In these cases, you should update Focus every frame, using RunService:BindToRenderStep() method at the RenderPriority.Camera priority.

Focus has no bearing on the position or orientation of the camera; see CFrame for this.

History 4

  • 553 Change Default of Focus from to
  • 486 Change ThreadSafety of Focus from ReadOnly to ReadSafe
  • 462 Change ThreadSafety of Focus from to ReadOnly
  • 47 Add Focus

GetLargestCutoffDistance

Parameters (1)
ignoreListInstances
Returns (1)
float

This method is used by PopperCam in the default camera scripts to ensure obstructions do not come between the Camera and its subject.

This method will check all BaseParts and Terrain in the Workspace with the following exceptions:

Note, as this method requires an ignoreList to run, you should pass an empty table when none is required.

This function is deprecated. It exists only for backward compatibility, and should not be used for new work.

History 4

Tags: [Deprecated]

GetPanSpeed

Parameters (0)
No parameters.
Returns (1)
float

This method is broken and should not be used.

This method returns the current pan speed of the Camera.

The pan speed of the Camera describes the speed at which the Camera is rotating around its Focus around the Y axis.

This function is deprecated. It exists only for backward compatibility, and should not be used for new work.

History 4

Tags: [Deprecated]

GetPartsObscuringTarget

Parameters (2)
castPointsArray
ignoreListInstances
Returns (1)
Instances

This method returns an array of BaseParts that are obscuring the lines of sight between the camera's CFrame and Vector3 positions in the castPoints array. Any Instances included in the ignoreList array will be ignored, along with their descendants.

The castPoints parameter is given as an array of Vector3 positions. Note that the array of BaseParts returned is in an arbitrary order, and no additional raycast data is provided. If you need data such as hit position, hit material, or surface normal, you should opt for the WorldRoot:Raycast() method.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
local Workspace = game:GetService("Workspace")

local camera = Workspace.CurrentCamera

local castPoints = {
	Vector3.new(0, 10, 0),
	Vector3.new(0, 15, 0)
}
local ignoreList = {}

local partsObscuringTarget = camera:GetPartsObscuringTarget(castPoints, ignoreList)

If Terrain obscures a cast point, BaseParts obscuring the cast point between the obscuring Terrain and the cast point will not be returned.

History 4

GetRenderCFrame

Parameters (0)
No parameters.
Returns (1)
CFrame

This method returns the actual CFrame of the Camera as it is rendered, including the impact of VR (VR head transformations are not applied to the CFrame property, so it is best practice to use GetRenderCFrame() to obtain the "true" CFrame of a player's view).

For example, when using VR, the Camera is actually rendered at the following CFrame:

1
2
3
4
5
6
7
8
local UserInputService = game:GetService("UserInputService")
local Workspace = game:GetService("Workspace")

local camera = Workspace.CurrentCamera

local headCFrame = UserInputService:GetUserCFrame(Enum.UserCFrame.Head)
headCFrame = headCFrame.Rotation + headCFrame.Position * camera.HeadScale
renderCFrame = camera.CFrame * headCFrame

The camera's render CFrame will only be changed to account for the head when the HeadLocked property is true.

History 2

GetRoll

Parameters (0)
No parameters.
Returns (1)
float

This method returns, in radians, the current roll applied to the Camera using SetRoll(). Roll is defined as rotation around the camera's Z-axis.

This method only returns roll applied using the SetRoll() method. Roll manually applied to the camera's CFrame is not accounted for. To obtain the actual roll of the Camera, including roll manually applied, you can use the following snippet:

1
2
3
4
5
6
7
8
local function getActualRoll()
	local camera = workspace.CurrentCamera

	local trueUp = Vector3.new(0, 1, 0)
	local cameraUp = camera:GetRenderCFrame().upVector

	return math.acos(trueUp:Dot(cameraUp))
end

History 2

GetTiltSpeed

Parameters (0)
No parameters.
Returns (1)
float

This method is broken and should not be used.

This method returns the current tilt speed of the Camera.

The tilt speed of the Camera describes the speed at which the Camera is rotating around its Focus around the camera's X axis.

This function is deprecated. It exists only for backward compatibility, and should not be used for new work.

History 4

Tags: [Deprecated]

HeadLocked

TypeDefault
booltrue

Toggles whether the camera will automatically track the head motion of a player using a VR device. When true (default), the engine combines CFrame with the UserCFrame of the user's head to render the player's view with head tracking factored in. The view will be rendered at the following CFrame:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
local UserInputService = game:GetService("UserInputService")
local Workspace = game:GetService("Workspace")

local camera = Workspace.CurrentCamera

local headCFrame = UserInputService:GetUserCFrame(Enum.UserCFrame.Head)
headCFrame = headCFrame.Rotation + headCFrame.Position * camera.HeadScale

-- This will be equivalent to Camera:GetRenderCFrame()
local renderCFrame = camera.CFrame * headCFrame

It is recommended to not disable this property for the following reasons:

  • Players may experience motion sickness if an equivalent head tracking solution is not added.
  • The Roblox engine performs latency optimizations when HeadLocked is true.

See Also

History 4

HeadScale

TypeDefault
float1

HeadScale is the scale of the user's perspective of the world when using VR.

The size of 1 stud in VR is 0.3 meters / HeadScale, meaning that larger HeadScale values equate to the world looking smaller from the user's perspective when using VR devices. For example, a part that's 1 stud tall appears to be 0.6 meters tall to a VR player with a HeadScale of 0.5.

This property is automatically controlled by VRService.AutomaticScaling to align the player's perspective with the size of their avatar. If you intend to control HeadScale yourself or use custom characters, toggle VRService.AutomaticScaling to VRScaling.Off.

This property should not be confused with Humanoid.HeadScale which is a NumberValue parented to a Humanoid to control its scaling.

History 4

Interpolate

Parameters (3)
endPosCFrame
endFocusCFrame
durationfloat
Returns (1)
null

This method tweens the Camera in a linear fashion towards a new CFrame and Focus over a given duration, for example:

1
2
3
4
5
6
7
8
local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable

camera:Interpolate(
	CFrame.new(0, 10, 100),
	CFrame.new(0, 0, 100),
	5
)

Throughout the tween, the camera's CFrame will be orientated towards the camera's Focus.

When the tween has completed, the camera's InterpolationFinished event will fire.

If this method is called while the Camera is already tweening, the older tween will be stopped (without firing InterpolationFinished) and overridden by the new tween.

Interpolate can only be used if the current CameraType is Scriptable, regardless of whether the default camera scripts are being used. If it is used with any other CameraType an error will be thrown.

You are advised to use TweenService to tween the Camera instead as it is more reliable and offers a variety of easing styles. See below for an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
local TweenService = game:GetService("TweenService")

local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable

local tween = TweenService:Create(
	camera,
	TweenInfo.new(5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out),
	{
		CFrame = CFrame.new(0, 10, 100),
		Focus = CFrame.new(0, 0, 100)
	}
)

tween:Play()
This function is deprecated. It exists only for backward compatibility, and should not be used for new work.

History 4

Tags: [Deprecated]

InterpolationFinished

Parameters (0)
No parameters.

This event fires when the Camera has finished interpolating using the Camera:Interpolate() method. It will not fire if a tween is interrupted due to Camera:Interpolate() being called again.

You are advised to use TweenService to animate the Camera instead, as it is more reliable and provides more options for easing styles.

History 2

MaxAxisFieldOfView

TypeDefault
float70

The MaxAxisFieldOfView property sets how many degrees along the longest viewport axis the camera can view.

When the longest axis is the vertical axis, this property will behave similar to the FieldOfView property. This is generally the case when a device is in a portrait orientation. In a landscape orientation, the longest axis will be the horizontal axis; in this case, the property describes the horizontal field of view of the Camera.

This property is not replicated. Its interface does not cross the network boundary.

History 8

Tags: [NotReplicated]

NearPlaneZ

TypeDefault
float-0.5

The NearPlaneZ property describes how far away the camera's near clipping plane is, in studs. The near clipping plane is a geometric plane that sits in front of the camera's CFrame. Anything between this plane and the camera will not render, creating a cutaway view when viewing objects at very short distances. The value of NearPlaneZ varies across different platforms and is currently always between -0.1 and -0.5.

Diagram showing how the NearPlaneZ clips (does not render) 3D content between the plane and the camera.
This property is not replicated. Its interface does not cross the network boundary.
This property is read-only. Its value can be read, but it cannot be modified.

History 4

Tags: [ReadOnly, NotReplicated]

PanUnits

Parameters (1)
unitsint
Returns (1)
null

This method pans the Camera around the Focus in 45 degree increments around the Y axis.

The rotation is applied to the camera's CFrame property.

This method pans the Camera in 45 degree increments, for example:

1
2
workspace.CurrentCamera:PanUnits(1) -- 45 degrees
workspace.CurrentCamera:PanUnits(-2) -- -90 degrees

PanUnits does not require the CameraType to be Scriptable.

This function is deprecated. It exists only for backward compatibility, and should not be used for new work.

History 5

Tags: [Deprecated]

ScreenPointToRay

Parameters (3)Default
xfloat
yfloat
depthfloat0
Returns (1)
Ray

This method creates a unit Ray from a 2D position on the screen (defined in pixels), accounting for the GUI inset. The Ray originates from the Vector3 equivalent of the 2D position in the world at the given depth (in studs) away from the Camera.

As this method acknowledges the GUI inset, the offset applied to GUI elements (such as from the top bar) is accounted for. This means the screen position specified will start in the top left corner below the top bar. For an otherwise identical method that does not account for the GUI offset, use ViewportPointToRay().

As the Ray created is a unit ray, it is only one stud long. To create a longer ray, you can do the following:

1
2
3
4
5
6
local Workspace = game:GetService("Workspace")

local camera = Workspace.CurrentCamera
local length = 500
local unitRay = camera:ScreenPointToRay(100, 100)
local extendedRay = Ray.new(unitRay.Origin, unitRay.Direction * length)

This method only works for the current Workspace camera. Other cameras, such as those you create for a ViewportFrame, have an initial viewport size of (1, 1) and are only updated after you set them to Workspace.CurrentCamera. The mismatch in viewport size causes the camera to return a ray with an incorrect Ray.Direction.

History 5

SetCameraPanMode

Parameters (1)Default
modeCameraPanModeClassic
Returns (1)
null

This method sets the CameraPanMode to be used by the Camera on mobile devices.

When the *'EdgeBump' CameraPanMode is used, swipe to pan is disabled and the edge bump camera controls are enabled.

SetCameraPan mode has no effect on Windows or Mac users.

This function is deprecated. It exists only for backward compatibility, and should not be used for new work.

History 4

Tags: [Deprecated]

SetImageServerView

Parameters (1)
modelCoordCFrame
Returns (1)
null

History 3

SetRoll

Parameters (1)
rollAnglefloat
Returns (1)
null

This method is outdated and no longer considered best practice.

This method sets the current roll, in radians, of the Camera. The roll is applied after the CFrame and represents the rotation around the camera's Z-axis.

For example, the following would invert the Camera:

1
workspace.CurrentCamera:SetRoll(math.pi) -- math.pi radians = 180 degrees

SetRoll has no effect on any roll applied using the CFrame property. Roll applied using SetRoll is not reflected in the CFrame property but is reflected in the CFrame returned byGetRenderCFrame().

This method can only be used when the CameraType is set to Scriptable, regardless of whether the default camera scripts are being used. If it is used with any other CameraType a warning is given in the output.

Any roll applied using this method will be lost when the CameraType is changed from Scriptable.

To obtain the roll set using this method use GetRoll().

As this method is outdated, you are advised to instead apply roll to the Camera using the CFrame property. For example:

1
2
3
local currentCFrame = workspace.CurrentCamera.CFrame
local rollCFrame = CFrame.Angles(0, 0, roll)
workspace.CurrentCamera.CFrame = currentCFrame * rollCFrame

History 3

TiltUnits

Parameters (1)
unitsint
Returns (1)
bool

This method tilts the Camera by rotating it around the Focus around the camera's X axis by a given multiple of 10 degrees.

The rotation is applied to the camera's CFrame property and is constrained between -81.05 and 81.05 degrees.

This function is deprecated. It exists only for backward compatibility, and should not be used for new work.

History 4

Tags: [Deprecated]

VRTiltAndRollEnabled

TypeDefault
boolfalse

This property toggles whether to apply tilt and roll from the CFrame property while the player is using a VR device.

To prevent motion sickness, the horizon should remain level. Tilting and rolling the player's view while using a VR device can cause a disconnect between the player's physical space and the virtual space they are viewing. Changing the apparent downwards direction can cause players to lose balance or experience dizziness.

For these reasons, it is generally advisable to leave this property disabled, unless you have extensively tested your experience for these effects. Even with tilt and roll enabled, you may want to ensure the player always has a stable reference frame, such as the interior of a vehicle or a floor that can help the player ground themselves in their physical space.

History 1

ViewportPointToRay

Parameters (3)Default
xfloat
yfloat
depthfloat0
Returns (1)
Ray

This method creates a unit Ray from a 2D position in device safe viewport coordinates, defined in pixels. The ray originates from the Vector3 equivalent of the 2D position in the world at the given depth (in studs) away from the Camera.

As illustrated below, (0, 0) corresponds to the top‑left point of the Roblox top bar. This means that the input 2D position does not account for the CoreUISafeInsets inset, but it does account for any DeviceSafeInsets.

Diagram showing the origin of the device safe area viewport coordinate system.

Note that UI instances use a different coordinate system (GuiObject.AbsolutePosition uses the CoreUISafeInsets viewport coordinate system while this method uses the DeviceSafeInsets viewport coordinate system). If you would like to specify position in core UI coordinates, please use ScreenPointToRay().

Also note that this method only works for the Workspace.CurrentCamera camera. Other cameras, such as those you create for a ViewportFrame, have an initial viewport size of (1, 1) and are only updated after you set them to CurrentCamera. The mismatch in viewport size causes the camera to return a ray with an incorrect Ray.Direction.

This method can be used in conjunction with the ViewportSize property to create a ray from the centre of the screen, for example:

1
2
3
4
5
6
local Workspace = game:GetService("Workspace")

local camera = Workspace.CurrentCamera

local viewportPoint = camera.ViewportSize / 2
local unitRay = camera:ViewportPointToRay(viewportPoint.X, viewportPoint.Y, 0)

As the Ray created is a unit ray, it is only one stud long. To create a longer ray, you can do the following:

1
2
3
4
5
6
7
local Workspace = game:GetService("Workspace")

local camera = Workspace.CurrentCamera

local length = 500
local unitRay = camera:ScreenPointToRay(100, 100)
local extendedRay = Ray.new(unitRay.Origin, unitRay.Direction * length)

History 3

ViewportSize

TypeDefault
Vector21, 1

ViewportSize returns the dimensions of the device safe area on the current screen. This area is a rectangle which includes the Roblox top bar area but does not include any device notches or screen cutouts. The units of ViewportSize are Roblox UI offset units which may be different from native display pixels.

Mobile device screen with cutout showing device safe area.

As noted above, ViewportSize is not equal to the fullscreen area size on displays with cutouts or notches. To obtain the fullscreen area size on all displays, you can query the AbsoluteSize property of a ScreenGui with ScreenInsets set to None. See ScreenInsets for a more information about how screen areas are defined.

Finally, note that ViewportSize is not the actual viewport size the camera uses for rendering (the camera renders in the fullscreen area). Also, the FieldOfView and DiagonalFieldOfView properties are based on the fullscreen area, not ViewportSize.

Camera Updates

Only the Camera currently referred to by Workspace.CurrentCamera has its ViewportSize updated each frame during the PreRender step. The ViewportSize of all other cameras in your experience won't be updated, including those used for ViewportFrames.

This property is not replicated. Its interface does not cross the network boundary.
This property is read-only. Its value can be read, but it cannot be modified.

History 6

Tags: [ReadOnly, NotReplicated]

WorldToScreenPoint

Parameters (1)
worldPointVector3
Returns (1)
Tuple

This method returns the screen location and depth of a Vector3 worldPoint and whether this point is within the bounds of the screen.

This method takes in account the current GUI inset, such as the space occupied by the top bar, meaning that the 2D position returned is in the same term as GUI positions and can be used to place GUI elements. For an otherwise identical method that ignores the GUI inset, see WorldToViewportPoint().

1
2
3
4
5
6
7
8
9
local Workspace = game:GetService("Workspace")

local camera = Workspace.CurrentCamera

local worldPoint = Vector3.new(0, 10, 0)
local vector, onScreen = camera:WorldToScreenPoint(worldPoint)

local screenPoint = Vector2.new(vector.X, vector.Y)
local depth = vector.Z

Note this method does not perform any raycasting and the boolean indicating whether worldPoint is within the bounds of the screen will be true regardless of whether the point is obscured by BaseParts or Terrain.

History 5

WorldToViewportPoint

Parameters (1)
worldPointVector3
Returns (1)
Tuple

This method returns the screen location and depth of a Vector3 worldPoint and whether this point is within the bounds of the screen.

This method does not take in account the current GUI inset, such as the space occupied by the top bar, meaning that the 2D position returned is taken from the top left corner of the viewport. Unless you are using ScreenGui.IgnoreGuiInset, this position is not appropriate for placing GUI elements.

For an otherwise identical method that accounts for the GUI inset, see WorldToScreenPoint().

1
2
3
4
5
6
7
8
9
local Workspace = game:GetService("Workspace")

local camera = Workspace.CurrentCamera

local worldPoint = Vector3.new(0, 10, 0)
local vector, onScreen = camera:WorldToViewportPoint(worldPoint)

local viewportPoint = Vector2.new(vector.X, vector.Y)
local depth = vector.Z

Note this method does not perform any raycasting and the boolean indicating whether worldPoint is within the bounds of the screen will be true regardless of whether the point is obscured by BaseParts or Terrain.

History 3

Zoom

Parameters (1)
distancefloat
Returns (1)
bool

History 2

ZoomToExtents

Parameters (2)
boundingBoxCFrameCFrame
boundingBoxSizeVector3
Returns (1)
null

History 2

focus

TypeDefault
CFrame
This property is deprecated. It exists only for backward compatibility, and should not be used for new work. Focus should be used instead.
This property is not replicated. Its interface does not cross the network boundary.

History 5

  • 553 Change PreferredDescriptor of focus from to Focus
  • 553 Change Default of focus from to
  • 486 Change ThreadSafety of focus from ReadOnly to ReadSafe
  • 462 Change ThreadSafety of focus from to ReadOnly
  • 57 Add focus
Tags: [NotReplicated, Deprecated]

Removed members 1

SetCameraInputMode

Parameters (1)Default
modeCameraInputModeClassic
Returns (1)
void

History 2

Settings