Class · inherits PVInstance
Camera
A class which defines a view of the 3D world.
About Camera
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:
CFramewhich represents the position and orientation of the camera.CameraTypewhich is read by the experience's camera scripts and determines how the camera should update each frame.CameraSubjectwhich is read by the experience's camera scripts and determines what object the camera should follow.FieldOfViewwhich represents the visible extent of the observable world.Focuswhich 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.
Storing Multiple Cameras
Note that when changing Workspace.CurrentCamera to a new
Camera, all other Cameras directly descending from
Workspace will be destroyed. If you need to store multiple cameras and
swap between them on demand, it's recommended that you store them in a
Folder or Model under Workspace, inside which they
will remain even when CurrentCamera is
changed.
Properties 13
CameraSubject
: InstanceTheHumanoidorBasePartthat is theCamerasubject.CameraSubjectaccepts a variety ofInstances. 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 andHumanoid.CameraOffset.When set to a
BasePart, the camera scripts follow its position, with a vertical offset in the case ofVehicleSeats.
CameraSubjectcannot be set tonil. Attempting to do so will revert it to its previous value.To restore
CameraSubjectto its default value, set it to the local character'sHumanoid:CameraType
: CameraTypeSpecifies theCameraTypeto be read by the camera scripts.The default Roblox camera scripts have several built-in behaviors. Setting this property toggles between the various
CameraTypebehaviors. Note that some camera types require a validCameraSubjectto work correctly.The default camera scripts will not move or update the camera if
CameraTypeis set toCameraType.Scriptable. For more information on positioning and orienting the camera manually, seeCFrame.For all
CameraTypesettings exceptCameraType.Scriptable, theCameraSubjectproperty represents the object whose position the camera'sFocusis set to.CFrame
: CFrameTheCFrameof theCamera, defining its position and orientation in the 3D world.This property is the
CFrameof theCamera, 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 useGetRenderCFrame()to obtain the "true"CFrameof 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
CameraTypetoCameraType.Scriptableso that the default camera scripts will not update the camera'sCFrame. 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
Camerais by using theCFrame.lookAtconstructor. In the following example, theCamerais positioned atDatatype.Vector3.new(0, 10, 0)and is oriented to be looking towardsDatatype.Vector3.new(10, 0, 0).local camera = workspace.CurrentCamera camera.CameraType = Enum.CameraType.Scriptable local pos = Vector3.new(0, 10, 0) local lookAtPos = Vector3.new(10, 0, 0) camera.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
CFrameto another. For this, you can either:Set the camera's position/orientation every frame with
RunService.BindToRenderStepand theCFrame.Lerpmethod.Create and play a
Tweenthat animates the position/orientation of the camera:local Players = game:GetService("Players") local TweenService = game:GetService("TweenService") 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()
DiagonalFieldOfView
: floatNotReplicatedSets the angle of the camera's diagonal field of view.Sets how many degrees in the diagonal direction (from one corner of the viewport to its opposite corner) the camera can view. See
FieldOfViewfor a more general explanation of field of view.Note that
DiagonalFieldOfViewrepresents the field of view that is visible by theCamerarendering into the fullscreen area which may be occluded by notches or screen cutouts on some devices. SeeViewportSizefor more information.FieldOfView
: floatSets the angle of the camera's vertical field of view.The
FieldOfView(FOV) property sets how many degrees in the vertical direction the camera can view. This property is clamped between1and120degrees and defaults at70. 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
FieldOfViewinclude:- 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
FieldOfViewrepresents the field of view that is visible by theCamerarendering into the fullscreen area which may be occluded by notches or screen cutouts on some devices. SeeViewportSizefor more information.FieldOfViewMode
: FieldOfViewModeDetermines the FOV value of theCamerathat's invariant under viewport size changes.The camera's
FieldOfView(FOV) must be updated to reflectViewportSizechanges. The value ofFieldOfViewModedetermines 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 toFieldOfViewMode.Diagonal, both horizontal and vertical FOV might be changed to keep the diagonal FOV constant.Focus
: CFrameSets the area in 3D space that is prioritized by Roblox's graphical systems.Certain graphical operations the engine performs, such as updating lighting, can take time or computational effort to complete. The camera's
Focusproperty tells the engine which area in 3D space to prioritize when performing such operations. For example, dynamic lighting from objects such asPointLightsmay not render at distances far from the focus.The default Roblox camera scripts automatically set
Focusto follow theCameraSubject(usually aHumanoid). However,Focuswill not automatically update whenCameraTypeis set toCameraType.Scriptableor when the default camera scripts are not being used. In these cases, you should updateFocusevery frame, usingRunService.BindToRenderStepmethod at theRenderPriority.Camerapriority.Focushas no bearing on the position or orientation of the camera; seeCFramefor this.HeadLocked
: booleanToggles whether the camera will automatically track the head motion of a player using a VR device.Toggles whether the camera will automatically track the head motion of a player using a VR device. When
true(default), the engine combinesCFramewith theUserCFrameof the user's head to render the player's view with head tracking factored in. The view will be rendered at the followingCFrame:local UserInputService = game:GetService("UserInputService") 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 * headCFrameIt 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
HeadLockedis true.
See Also
VRService.GetUserCFramewhich can be used to obtain theCFrameof the head.VRService.RecenterUserHeadCFramewhich is used to recenter the head to the current position and orientation of the VR device.- The
GetRenderCFrame()method which returns theCFramecombined with theCFrameof the user's head.
HeadScale
: floatSets the scale of the user's perspective of the world when using VR.HeadScaleis 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 largerHeadScalevalues 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 aHeadScaleof0.5.This property is automatically controlled by
VRService.AutomaticScalingto align the player's perspective with the size of their avatar. If you intend to controlHeadScaleyourself or use custom characters, toggleVRService.AutomaticScalingtoVRScaling.Off.This property should not be confused with
Humanoid.HeadScalewhich is aNumberValueparented to aHumanoidto control its scaling.MaxAxisFieldOfView
: floatNotReplicatedSets the angle of the camera's field of view along the longest viewport axis.The
MaxAxisFieldOfViewproperty 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
FieldOfViewproperty. 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 theCamera.NearPlaneZ
: floatReadOnlyNotReplicatedDescribes the negative Z offset, in studs, of the camera's near clipping plane.The
NearPlaneZproperty 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'sCFrame. Anything between this plane and the camera will not render, creating a cutaway view when viewing objects at very short distances. The value ofNearPlaneZvaries across different platforms and is currently always between-0.1and-0.5.ViewportSize
: Vector2ReadOnlyNotReplicatedThe dimensions of the device safe area on a Roblox client.ViewportSizereturns 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 ofViewportSizeare Roblox UI offset units which may be different from native display pixels.As noted above,
ViewportSizeis 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 theAbsoluteSizeproperty of aScreenGuiwithScreenInsetsset toNone. SeeScreenInsetsfor a more information about how screen areas are defined.Finally, note that
ViewportSizeis not the actual viewport size the camera uses for rendering (the camera renders in the fullscreen area). Also, theFieldOfViewandDiagonalFieldOfViewproperties are based on the fullscreen area, notViewportSize.Camera Updates
Only the
Cameracurrently referred to byWorkspace.CurrentCamerahas itsViewportSizeupdated each frame during thePreRenderstep. TheViewportSizeof all other cameras in your experience won't be updated, including those used forViewportFrames.VRTiltAndRollEnabled
: booleanToggles whether to apply tilt and roll from theCFrameproperty while the player is using a VR device.This property toggles whether to apply tilt and roll from the
CFrameproperty 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.
Methods 7
GetPartsObscuringTarget
(castPoints: Array, ignoreList: Instances) → InstancesReturns an array ofBasePartsthat are obscuring the lines of sight between the camera'sCFrameand the cast points.This method returns an array of
BasePartsthat are obscuring the lines of sight between the camera'sCFrameandVector3positions in thecastPointsarray. AnyInstancesincluded in theignoreListarray will be ignored, along with their descendants.The
castPointsparameter is given as an array ofVector3positions. Note that the array ofBasePartsreturned 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 theWorldRoot.Raycastmethod.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
Terrainobscures a cast point,BasePartsobscuring the cast point between the obscuringTerrainand the cast point will not be returned.GetRenderCFrame
() → CFrameReturns the actualCFramewhere theCamerais being rendered, accounting for any roll applied and the impact of VR devices.This method returns the actual
CFrameof theCameraas it is rendered, including the impact of VR (VR head transformations are not applied to theCFrameproperty, so it is best practice to useGetRenderCFrame()to obtain the "true"CFrameof a player's view).For example, when using VR, the
Camerais actually rendered at the followingCFrame:local UserInputService = game:GetService("UserInputService") local camera = workspace.CurrentCamera local headCFrame = UserInputService:GetUserCFrame(Enum.UserCFrame.Head) headCFrame = headCFrame.Rotation + headCFrame.Position * camera.HeadScale renderCFrame = camera.CFrame * headCFrameThe camera's render
CFramewill only be changed to account for the head when theHeadLockedproperty is true.ScreenPointToRay
(x: float, y: float, depth: float = 0) → RayCreates a unitRayfrom a position on the screen (in pixels), at a set depth from theCameraorientated in the camera's direction. Accounts for the GUI inset.This method creates a unit
Rayfrom a 2D position on the screen (defined in pixels), accounting for the GUI inset. TheRayoriginates from theVector3equivalent of the 2D position in the world at the given depth (in studs) away from theCamera.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
Raycreated is a unit ray, it is only one stud long. To create a longer ray, you can do the following: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
Workspacecamera. Other cameras, such as those you create for aViewportFrame, have an initial viewport size of(1, 1)and are only updated after you set them toWorkspace.CurrentCamera. The mismatch in viewport size causes the camera to return a ray with an incorrectRay.Direction.ViewportPointToRay
(x: float, y: float, depth: float = 0) → RayCreates a unitRayfrom a position on the viewport (in pixels), at a given depth from theCamera, orientated in the camera's direction. Does not account for theCoreUISafeInsetsinset.This method creates a unit
Rayfrom a 2D position in device safe viewport coordinates, defined in pixels. The ray originates from theVector3equivalent of the 2D position in the world at the given depth (in studs) away from theCamera.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 theCoreUISafeInsetsinset, but it does account for anyDeviceSafeInsets.Note that UI instances use a different coordinate system (
GuiObject.AbsolutePositionuses theCoreUISafeInsetsviewport coordinate system while this method uses theDeviceSafeInsetsviewport coordinate system). If you would like to specify position in core UI coordinates, please useScreenPointToRay().Also note that this method only works for the
Workspace.CurrentCameracamera. Other cameras, such as those you create for aViewportFrame, have an initial viewport size of(1, 1)and are only updated after you set them toCurrentCamera. The mismatch in viewport size causes the camera to return a ray with an incorrectRay.Direction.This method can be used in conjunction with the
ViewportSizeproperty to create a ray from the centre of the screen, for example:local camera = workspace.CurrentCamera local viewportPoint = camera.ViewportSize / 2 local unitRay = camera:ViewportPointToRay(viewportPoint.X, viewportPoint.Y, 0)As the
Raycreated is a unit ray, it is only one stud long. To create a longer ray, you can do the following:local camera = workspace.CurrentCamera local length = 500 local viewportPoint = camera.ViewportSize / 2 local unitRay = camera:ViewportPointToRay(viewportPoint.X, viewportPoint.Y, 0) local extendedRay = Ray.new(unitRay.Origin, unitRay.Direction * length)WorldToScreenPoint
(worldPoint: Vector3) → TupleReturns the screen location and depth of aVector3worldPointand whether this point is within the bounds of the screen. Accounts for the GUI inset.This method returns the screen location and depth of a
Vector3worldPointand 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().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.ZNote this method does not perform any raycasting and the boolean indicating whether
worldPointis within the bounds of the screen will betrueregardless of whether the point is obscured byBasePartsorTerrain.WorldToViewportPoint
(worldPoint: Vector3) → TupleReturns the screen location and depth of aVector3worldPointand whether this point is within the bounds of the screen. Does not account for the GUI inset.This method returns the screen location and depth of a
Vector3worldPointand 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().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.ZNote this method does not perform any raycasting and the boolean indicating whether
worldPointis within the bounds of the screen will betrueregardless of whether the point is obscured byBasePartsorTerrain.ViewportFrame Cameras
Only
Workspace.CurrentCamerahas its internal viewport dimensions updated by the engine each frame. Cameras used inside aViewportFramedo not receive viewport updates, so the X and Y components returned by this method will be in the range [0, 1] rather than in pixels. In practice, this means the returned values can be used as scale coordinates (for example,UDim2.fromScale(vector.X, vector.Y)) to position GUI elements relative to the ViewportFrame.ZoomToExtents
(boundingBoxCFrame: CFrame, boundingBoxSize: Vector3) → ()Adjusts theCFrameso that the specified bounding box is fully visible within the camera's viewport.This method adjusts the
CFrameso that a specified bounding box is fully visible within the camera's viewport, without cropping any part of the box.The bounding box is defined by its center and orientation (
boundingBoxCFrame) and its size (boundingBoxSize).This can be used to focus the camera on a 3D object or model, such as when framing content in a
ViewportFrame, taking screenshots, or smoothly transitioning the camera to highlight specific content.The method automatically accounts for the camera's field of view and viewport aspect ratio to ensure the entire bounding box fits.
Camera also has every member of PVInstance. Deprecated and Roblox-only members are hidden.
Reference text from the Roblox Creator Documentation, licensed CC BY 4.0, reorganized by rbxtoolshub.com · Studio 0.743.0.7431133.