CFrames

A CFrame, short for Coordinate Frame, is a data type used to rotate and position 3D objects. As either an object property or a standalone unit, a CFrame contains global x-, y-, and z-coordinates as well as rotation data for each axis. In addition, CFrames contain helpful functions for working with objects in the 3D space.

Some examples of CFrame applications in a game might be:

  • Finding a far-off target point for a projectile, like the position of an enemy targeted by a player's laser blaster.
  • Moving the camera so that it focuses on specific NPCs as a player interacts with them.
  • Placing a status indicator directly above a player's head to show if they are paralyzed, boosted, poisoned, etc.

CFrame Basics

Positioning a CFrame

You can create an empty CFrame at the default position of (0, 0, 0) by using CFrame.new(). To position a CFrame at a specific point, provide x-, y-, and z-coordinates as arguments to CFrame.new(). In the following example, the redBlock part's CFrame property changes to newCFrame, repositioning it to (-2, 2, 4).


1local redBlock = game.Workspace.RedBlock
2
3-- Create new CFrame
4local newCFrame = CFrame.new(-2, 2, 4)
5
6-- Overwrite redBlock's current CFrame with new CFrame
7redBlock.CFrame = newCFrame
8
Before
After

Alternatively, you can provide a new Vector3 position to CFrame.new() and achieve the same result:


1local redBlock = game.Workspace.RedBlock
2
3-- Create new CFrame
4local newVector3 = Vector3.new(-2, 2, 4)
5local newCFrame = CFrame.new(newVector3)
6
7-- Overwrite redBlock's current CFrame with new CFrame
8redBlock.CFrame = newCFrame
9

Rotating a CFrame

To create a rotated CFrame, use the CFrame.Angles() constructor, providing a rotation angle in radians for the desired axes. The parameters to CFrame.Angles() is in radians, not degrees. If you prefer degrees, use math.rad() to convert degrees to radians. In the following example, the redBlock part rotates 45 degrees counterclockwise on its y-axis.


1local redBlock = game.Workspace.RedBlock
2
3-- Create new rotated CFrame
4local newCFrame = CFrame.Angles(0, math.rad(45), 0)
5
6-- Overwrite redBlock's current CFrame with new CFrame
7redBlock.CFrame = newCFrame
8
Before
After

Facing Toward a Point

You can use CFrame.new() to point the front surface of a CFrame at a specific point in the world. In the following example, redBlock part positions at (0, 3, 0) and points its front surface, marked by the white circle, at the blueCube part.


1local redBlock = game.Workspace.RedBlock
2local blueCube = game.Workspace.BlueCube
3
4-- Create a Vector3 for both the start position and target position
5local startPosition = Vector3.new(0, 3, 0)
6local targetPosition = blueCube.Position
7
8-- Put the redBlock at 'startPosition' and point its front surface at 'targetPosition'
9redBlock.CFrame = CFrame.new(startPosition, targetPosition)
10
Before
After

Offsetting a CFrame

To offset an object by a specific number of studs from its current position, add or subtract a Vector3 to or from a new CFrame at the object's position. To get a properly-formatted Vector3 position of an object to use with CFrame.new(), as seen here, its Position property (redBlock.Position) is a convenient shortcut.


1local redBlock = game.Workspace.RedBlock
2
3redBlock.CFrame = CFrame.new(redBlock.Position) + Vector3.new(0, 1.25, 0)
4
Before
After

You can use the same technique to offset an object from the position of another object. In the following example, a Vector3 adds to a new CFrame created at the blue cube's position instead of the block's position.


1local redBlock = game.Workspace.RedBlock
2local blueCube = game.Workspace.BlueCube
3
4redBlock.CFrame = CFrame.new(blueCube.Position) + Vector3.new(0, 2, 0)
5
Before
After

Dynamic CFrame Orientation

The CFrame.new() and CFrame.Angles() constructors reposition or rotate an object at a specific orientation within the world, but you sometimes can't rely on a fixed world position and rotation angle. For example:

  • Placing a floating treasure directly in front of a player who may be standing anywhere in the world, facing any direction.
  • Making a magical genie appear directly above a player's right shoulder.

In these cases, use CFrame methods instead of their constructors.

Relative Position

The CFrame:ToWorldSpace() function transforms an object's CFrame — respecting its own local orientation — to a new world orientation. This makes it ideal for offsetting a part relative to itself or another object, regardless of how it's currently positioned/rotated.

In the the following example, the redBlock part offsets 2 studs relative to the y-axis of the blue cube (the green arrow pointing through it) and not relative to the global y-axis pointing straight up.


1local redBlock = game.Workspace.RedBlock
2local blueCube = game.Workspace.BlueCube
3
4local offsetCFrame = CFrame.new(0, 2, 0)
5redBlock.CFrame = blueCube.CFrame:ToWorldSpace(offsetCFrame)
6
Before
After

Relative Rotation

You can also use CFrame:ToWorldSpace() to rotate an object relative to itself. In the following example, the redBlock part rotates 70 degrees counterclockwise on its y-axis and 20 degrees clockwise on its z-axis.


1local redBlock = game.Workspace.RedBlock
2
3local rotatedCFrame = CFrame.Angles(0, math.rad(70), math.rad(20))
4redBlock.CFrame = redBlock.CFrame:ToWorldSpace(rotatedCFrame)
5
Before
After

Facing a Specific Surface Toward a Point

You can make the front of an object face another object by supplying a Vector3 point as the second parameter of CFrame.new(). You can also use relative rotation to make any face of the object point toward a Vector3 point. The following example performs two consecutive CFrame operations:

  1. Point the front surface, marked by the white circle, at the target.
  2. Rotate the CFrame to make the top surface, marked by the black circle, point toward the target.

1local redBlock = game.Workspace.RedBlock
2local blueCube = game.Workspace.BlueCube
3
4-- Create a Vector3 for the target position
5local targetPosition = blueCube.Position
6
7-- Point the redBlock's front surface at 'targetPosition'
8redBlock.CFrame = CFrame.new(redBlock.Position, targetPosition)
9
10-- Now the redBlock's front surface (white circle) is pointing towards the blueCube
11
12-- Rotate redBlock's CFrame relative to itself so that its top surface (not front) points toward the target
13local rotatedCFrame = CFrame.Angles(math.rad(-90), 0, 0)
14redBlock.CFrame = redBlock.CFrame:ToWorldSpace(rotatedCFrame)
15
16-- Now the redBlock's top surface (black circle) is pointing towards the blueCube (as seen in After below)
17
Before
After

Finding a Point Between Points

You can use linear interpolation, or lerp, to position a CFrame between two points. In the following example, the redBlock part repositions between the greenCube and cyanCube parts. The value of 0.7 places it 70% of the distance away from the green cube.


1local redBlock = game.Workspace.RedBlock
2local greenCube = game.Workspace.GreenCube
3local cyanCube = game.Workspace.CyanCube
4
5redBlock.CFrame = greenCube.CFrame:Lerp(cyanCube.CFrame, 0.7)
6
Before
After