A datatype/CFrame, brief for Coordinate Frame, is a data form used to rotate and position 3D objects. As either things property or a standalone unit, a CFrame contains worldwide X, Y, and also Z coordinates as well as rotation data because that each axis. In addition, CFrames contain helpful functions because that working with objects in the 3D space.

You are watching: How to rotate objects in roblox studio

Some instances of CFrame applications in a game can be:

Finding a distant target point for a projectile, like the place of an foe targeted by a player’s laser blaster.Moving the camera so the it concentrates on specific NPCs as a player interacts with them.Placing a condition indicator directly over a player’s head to display if they room paralyzed, boosted, poisoned, etc.

CFrame Basics

Positioning a CFrame

An north CFrame deserve to be created with datatype/CFrame|CFrame.new() in ~ a default position of 0, 0, 0 in the game world. To position a CFrame at a specific point however, girlfriend should provide X, Y, and also Z parameters to CFrame.new(). In the complying with example, the redBlock thing is re-positioned in ~ -2, 2, 4 by overwriting that is CFrame property with the worths stored in newCFrame.


local redBlock = game.Workspace.RedBlock-- Create brand-new CFramelocal newCFrame = CFrame.new(-2, 2, 4)-- Overwrite red block"s current CFrame with brand-new CFrameredBlock.CFrame = newCFrame

*
*

Alternatively, friend can provide a brand-new datatype/Vector3 place to CFrame.new() and accomplish the very same result:


local redBlock = game.Workspace.RedBlock-- Create new CFramelocal newVector3 = Vector3.new(-2, 2, 4)local newCFrame = CFrame.new(newVector3)-- Overwrite red block"s current CFrame with new CFrameredBlock.CFrame = newCFrame

Rotating a CFrame

To develop a rotated CFrame, use the datatype/CFrame|CFrame.Angles() constructor, providing a rotation edge in radians for the preferred axes:


local redBlock = game.Workspace.RedBlock -- Create brand-new rotated CFramelocal newCFrame = CFrame.Angles(0, math.rad(45), 0)-- Overwrite red block"s present CFrame with new CFrameredBlock.CFrame = newCFrame

*
*

As noted, parameters to CFrame.Angles() need to be specified in radians, no degrees. If you favor degrees, simply use the math.rad() converter as displayed in this example.

Facing toward a Point

One the the most powerful uses of CFrame.new() is the ability to allude the front surface of a CFrame in ~ a specific point in the world. Consider the following instance which places the redBlock component at 0, 3, 0 and then point out its front surface ar (marked by the white circle) in ~ the blueCube part:


local redBlock = game.Workspace.RedBlocklocal blueCube = game.Workspace.BlueCube-- develop a Vector3 for both the start position and also target positionlocal startPosition = Vector3.new(0, 3, 0)local targetPosition = blueCube.Position-- placed the red block at "startPosition" and suggest its front surface at "targetPosition"redBlock.CFrame = CFrame.new(startPosition, targetPosition)

*
*

Offsetting a CFrame

In part cases, you’ll have to offset an item by a specific variety of studs from its existing position. This have the right to be done by simply adding or subtracting a Vector3 to/from a brand-new CFrame created at the object’s position.


local redBlock = game.Workspace.RedBlockredBlock.CFrame = CFrame.new(redBlock.Position) + Vector3.new(0, 1.25, 0)

*
*

To get a properly-formatted Vector3 place of an object to use v CFrame.new(), as watched here, the Position building (redBlock.Position) is a practically shortcut.
The same method can be used to offset an item from the place of one more object. This time, a Vector3 is included to a brand-new CFrame created at the blue cube’s position instead of the block’s position.


local redBlock = game.Workspace.RedBlocklocal blueCube = game.Workspace.BlueCuberedBlock.CFrame = CFrame.new(blueCube.Position) + Vector3.new(0, 2, 0)

*
*

Dynamic CFrame Orientation

By themselves, the CFrame.new() and also CFrame.Angles() constructors are an effective but lock also very strict — they just position/rotate an item at a details orientation within the world. This is beneficial in some cases, however what if you can’t count on a fixed civilization position and also rotation angle? because that instance:

Placing a floating treasure directly in front of a player who might be standing all over in the world, facing any direction.Making a magical genie show up directly over a player’s right shoulder.

In this cases, CFrame functions room far an ext powerful than the strictly constructors.

Relative Position

The datatype/CFrame|CFrame:ToWorldSpace() duty transforms one object’s CFrame — respecting the own regional orientation — to a brand-new world orientation. This renders it appropriate for offsetting a part relative to itself or an additional object, nevertheless of just how it’s currently positioned/rotated.

In the code/image below, notice that the red block gets offset 2 studs family member to the Y axis the the blue cube (the green arrow pointing through it) and also not relative to the global Y axis pointing right up.


local redBlock = game.Workspace.RedBlocklocal blueCube = game.Workspace.BlueCubelocal offsetCFrame = CFrame.new(0, 2, 0)redBlock.CFrame = blueCube.CFrame:ToWorldSpace(offsetCFrame)

Relative Rotation

CFrame:ToWorldSpace() is additionally useful because that rotating things relative to itself, for circumstances rotating it 70 degrees counter-clockwise top top its existing Y axis and also 20 degrees clockwise on its current Z axis.


local redBlock = game.Workspace.RedBlocklocal rotatedCFrame = CFrame.Angles(0, math.rad(70), math.rad(20))redBlock.CFrame = redBlock.CFrame:ToWorldSpace(rotatedCFrame)

Facing a certain Surface toward a Point

As portrayed earlier, you have the right to make an object “look” at another object by supplying a Vector3 point as the 2nd parameter that CFrame.new(). This have the right to be limiting, however, because the procedure is connected with the object’s front face.

Fortunately, you have the right to use loved one rotation to make any face of the object point toward a Vector3 point. Consider this example which performs 2 consecutive CFrame operations:

Point the front surface (marked through the white circle) in ~ the target.Rotate the CFrame to do the top surface (marked through the black circle) allude toward the target.

local redBlock = game.Workspace.RedBlocklocal blueCube = game.Workspace.BlueCube-- produce a Vector3 for the target positionlocal targetPosition = blueCube.Position-- allude the red block"s front surface at "targetPosition"redBlock.CFrame = CFrame.new(redBlock.Position, targetPosition)-- revolve red block"s CFrame family member to chin so the its peak surface (not front) points toward the targetlocal 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, regularly referred to together lerp, you have the right to position a CFrame between two points. Because that instance, the complying with code positions the redBlock component directly in between the greenCube and cyanCube parts, making use of a worth of 0.7 to location it 70% the the street away from the eco-friendly cube.

See more: Quick Answer: What Does Nya Mean In Text Ing? What Does Nya Stand For In Texting


local redBlock = game.Workspace.RedBlocklocal greenCube = game.Workspace.GreenCubelocal cyanCube = game.Workspace.CyanCuberedBlock.CFrame = greenCube.CFrame:Lerp(cyanCube.CFrame, 0.7)
These pointers should gain you began on orienting objects in the game civilization via CFrame constructors and functions. Once you have a solid knowledge of this concepts, take into consideration exploring progressed CFrame manipulation in articles/CFrame mathematics Operations|CFrame mathematics Operations.