datatype/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 he/she is paralyzed, boosted, poisoned, etc.
Positioning a CFrame
An empty CFrame can be created with
datatype/CFrame|CFrame.new() at a default position of 0, 0, 0 in the game world. To position a CFrame at a specific point however, you should provide X, Y, and Z parameters to
CFrame.new(). In the following example, the
redBlock object is re-positioned at -2, 2, 4 by overwriting its CFrame property with the values stored in
local redBlock = game.Workspace.RedBlock -- Create new CFrame local newCFrame = CFrame.new(-2, 2, 4) -- Overwrite red block's current CFrame with new CFrame redBlock.CFrame = newCFrame
Alternatively, you can provide a new
datatype/Vector3 position to
CFrame.new() and achieve the same result:
local redBlock = game.Workspace.RedBlock -- Create new CFrame local newVector3 = Vector3.new(-2, 2, 4) local newCFrame = CFrame.new(newVector3) -- Overwrite red block's current CFrame with new CFrame redBlock.CFrame = newCFrame
Rotating a CFrame
To create a rotated CFrame, use the
datatype/CFrame|CFrame.Angles() constructor, providing a rotation angle in radians for the desired axes:
local redBlock = game.Workspace.RedBlock -- Create new rotated CFrame local newCFrame = CFrame.Angles(0, math.rad(45), 0) -- Overwrite red block's current CFrame with new CFrame redBlock.CFrame = newCFrame
CFrame.Angles()must be specified in radians, not degrees. If you prefer degrees, simply use the
math.rad()converter as shown in this example.
Facing Toward a Point
One of the most powerful uses of
CFrame.new() is the ability to point the front surface of a CFrame at a specific point in the world. Consider the following example which places the
redBlock part at 0, 3, 0 and then points its front surface (marked by the white circle) at the
local redBlock = game.Workspace.RedBlock local blueCube = game.Workspace.BlueCube -- Create a Vector3 for both the start position and target position local startPosition = Vector3.new(0, 3, 0) local targetPosition = blueCube.Position -- Put the red block at 'startPosition' and point its front surface at 'targetPosition' redBlock.CFrame = CFrame.new(startPosition, targetPosition)
Offsetting a CFrame
In some cases, you’ll need to offset an object by a specific number of studs from its current position. This can be done by simply adding or subtracting a Vector3 to/from a new CFrame created at the object’s position.
local redBlock = game.Workspace.RedBlock redBlock.CFrame = CFrame.new(redBlock.Position) + Vector3.new(0, 1.25, 0)
CFrame.new(), as seen here, its Position property (
redBlock.Position) is a convenient shortcut.
The same technique can be used to offset an object from the position of another object. This time, a Vector3 is added to a new CFrame created at the blue cube’s position instead of the block’s position.
local redBlock = game.Workspace.RedBlock local blueCube = game.Workspace.BlueCube redBlock.CFrame = CFrame.new(blueCube.Position) + Vector3.new(0, 2, 0)
Dynamic CFrame Orientation
By themselves, the
CFrame.Angles() constructors are powerful but they’re also very strict — they simply position/rotate an object at a specific orientation within the world. This is useful in some cases, but what if you can’t rely on a fixed world position and rotation angle? For instance:
- 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, CFrame functions are far more powerful than the strict constructors.
datatype/CFrame|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 code/image below, notice that the red block gets offset 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.
local redBlock = game.Workspace.RedBlock local blueCube = game.Workspace.BlueCube local offsetCFrame = CFrame.new(0, 2, 0) redBlock.CFrame = blueCube.CFrame:ToWorldSpace(offsetCFrame)
CFrame:ToWorldSpace() is also useful for rotating an object relative to itself, for instance rotating it 70 degrees counter-clockwise on its current Y axis and 20 degrees clockwise on its current Z axis.
local redBlock = game.Workspace.RedBlock local rotatedCFrame = CFrame.Angles(0, math.rad(70), math.rad(20)) redBlock.CFrame = redBlock.CFrame:ToWorldSpace(rotatedCFrame)
Facing a Specific Surface Toward a Point
As illustrated earlier, you can make an object “look” at another object by supplying a Vector3 point as the second parameter of
CFrame.new(). This can be limiting, however, since the operation is associated with the object’s front face.
Fortunately, you can use relative rotation to make any face of the object point toward a Vector3 point. Consider this example which performs two consecutive CFrame operations:
- Point the front surface (marked by the white circle) at the target.
- Rotate the CFrame to make the top surface (marked by the black circle) point toward the target.
local redBlock = game.Workspace.RedBlock local blueCube = game.Workspace.BlueCube -- Create a Vector3 for the target position local targetPosition = blueCube.Position -- Point the red block's front surface at 'targetPosition' redBlock.CFrame = CFrame.new(redBlock.Position, targetPosition) -- Rotate red block's CFrame relative to itself so that its top surface (not front) points toward the target local rotatedCFrame = CFrame.Angles(math.rad(-90), 0, 0) redBlock.CFrame = redBlock.CFrame:ToWorldSpace(rotatedCFrame)
Finding a Point Between Points
Using a method known as linear interpolation, often referred to as lerp, you can position a CFrame between two points. For instance, the following code positions the
redBlock part directly between the
cyanCube parts, using a value of
0.7 to place it 70% of the distance away from the green cube.
local redBlock = game.Workspace.RedBlock local greenCube = game.Workspace.GreenCube local cyanCube = game.Workspace.CyanCube redBlock.CFrame = greenCube.CFrame:Lerp(cyanCube.CFrame, 0.7)
These pointers should get you started on orienting objects in the game world via CFrame constructors and functions. Once you have a solid understanding of these concepts, consider exploring advanced CFrame manipulation in
articles/CFrame Math Operations|CFrame Math Operations.