CFrameデータ型

データ型日本語解説あり

ひとことで言うと

位置と向き(回転)を1つにまとめて表すデータ型です。パーツを回転させたり、向きを変えながら動かしたりするときに使います。

公式の説明(英語):A data type that represents both a 3D position and orientation.

CFrame.new(x, y, z) で位置を作り、CFrame.Angles(x, y, z) で回転(ラジアン)を作ります。2つをかけ算すると「その位置で、その向きに回す」になります。

使用例

回り続けるパーツ
Script置く場所:パーツの中
local RunService = game:GetService("RunService")
local part = script.Parent :: BasePart
part.Anchored = true

RunService.Heartbeat:Connect(function(deltaTime)
	part.CFrame = part.CFrame * CFrame.Angles(0, math.rad(90) * deltaTime, 0)
end)
位置と距離を扱う
Script置く場所:ServerScriptService
local part = Instance.new("Part")
part.Anchored = true
part.Position = Vector3.new(0, 10, 0)
part.CFrame = CFrame.new(0, 10, 0) * CFrame.Angles(0, math.rad(45), 0)
part.Parent = workspace

local distance = (part.Position - Vector3.new(0, 0, 0)).Magnitude
print(distance)

スクリプトは、Luauの型チェック(luau-lsp)で確認しています。

コンストラクター(作り方)(16)

名前と書き方説明
new
CFrame.new()
位置(と向き)を指定してCFrameを作ります。Returns a blank identity CFrame.
new
CFrame.new(pos: Vector3)
位置(と向き)を指定してCFrameを作ります。Returns a CFrame with no rotation with the position of the provided Vector3.
new
CFrame.new(pos: Vector3, lookAt: Vector3)
位置(と向き)を指定してCFrameを作ります。Returns a CFrame with the position of the first Vector3 and an orientation pointed toward the second.
new
CFrame.new(x: number, y: number, z: number)
位置(と向き)を指定してCFrameを作ります。Returns a CFrame with a position comprised of the provided x, y, and z components.
new
CFrame.new(x: number, y: number, z: number, qX: number, qY: number, qZ: number, qW: number)
位置(と向き)を指定してCFrameを作ります。Returns a CFrame from position (x, y, z) and quaternion (qX, qY, qZ, qW).
new
CFrame.new(x: number, y: number, z: number, R00: number, R01: number, R02: number, R10: number, R11: number, R12: number, R20: number, R21: number, R22: number)
位置(と向き)を指定してCFrameを作ります。Returns a CFrame from position (x, y, z) with an orientation specified by the rotation matrix.
lookAt
CFrame.lookAt(at: Vector3, lookAt: Vector3, up: Vector3)
ある位置から、別の位置を向いたCFrameを作ります。Returns a CFrame with the position of the first Vector3 and an orientation pointed toward the second.
lookAlong
CFrame.lookAlong(at: Vector3, direction: Vector3, up: Vector3)
Returns a CFrame with the position of the first Vector3 and an orientation directed along the second.
fromRotationBetweenVectors
CFrame.fromRotationBetweenVectors(from: Vector3, to: Vector3)
Returns a CFrame representing the orientation needed to rotate from the first Vector3 to the second, with the position set to zero.
fromEulerAngles
CFrame.fromEulerAngles(rx: number, ry: number, rz: number, order: RotationOrder)
Returns a rotated CFrame from angles rx, ry, and rz in radians. Rotations are applied in the optional RotationOrder with a default of XYZ.
fromEulerAnglesXYZ
CFrame.fromEulerAnglesXYZ(rx: number, ry: number, rz: number)
Returns a rotated CFrame from angles rx, ry, and rz in radians using RotationOrder.XYZ.
fromEulerAnglesYXZ
CFrame.fromEulerAnglesYXZ(rx: number, ry: number, rz: number)
Returns a rotated CFrame from angles rx, ry, and rz in radians using RotationOrder.YXZ.
Angles
CFrame.Angles(rx: number, ry: number, rz: number)
X・Y・Z軸のまわりの回転(ラジアン)からCFrameを作ります。Equivalent to Datatype.CFrame.fromEulerAnglesXYZ()|fromEulerAnglesXYZ().
fromOrientation
CFrame.fromOrientation(rx: number, ry: number, rz: number)
Equivalent to Datatype.CFrame.fromEulerAnglesYXZ()|fromEulerAnglesYXZ().
fromAxisAngle
CFrame.fromAxisAngle(v: Vector3, r: number)
Returns a rotated CFrame from a unit Vector3 and a rotation in radians.
fromMatrix
CFrame.fromMatrix(pos: Vector3, vX: Vector3, vY: Vector3, vZ: Vector3)
Returns a CFrame from a translation and the columns of a rotation matrix.

プロパティ(12)

名前と書き方説明
identity
CFrame.identity: CFrame
An identity CFrame with no translation or rotation.
Position
CFrame.Position: Vector3
CFrameの位置(Vector3)。The 3D position of the CFrame.
Rotation
CFrame.Rotation: CFrame
A copy of the CFrame with no translation.
X
CFrame.X: number
The **X** coordinate of the position.
Y
CFrame.Y: number
The **Y** coordinate of the position.
Z
CFrame.Z: number
The **Z** coordinate of the position.
LookVector
CFrame.LookVector: Vector3
CFrameが向いている前の方向。The forward-direction component of the CFrame object's orientation, equivalent to the negated form of Datatype.CFrame.ZVector|ZVector.
RightVector
CFrame.RightVector: Vector3
The right-direction component of the CFrame object's orientation.
UpVector
CFrame.UpVector: Vector3
The up-direction component of the CFrame object's orientation.
XVector
CFrame.XVector: Vector3
Equivalent to Datatype.CFrame.RightVector|RightVector.
YVector
CFrame.YVector: Vector3
Equivalent to Datatype.CFrame.UpVector|UpVector.
ZVector
CFrame.ZVector: Vector3
The Z component of the CFrame object's orientation. Equivalent to the third column of the rotation matrix.

メソッド(18)

名前と書き方説明
Inverse
CFrame:Inverse(): CFrame
Returns the inverse of the CFrame.
Lerp
CFrame:Lerp(goal: CFrame, alpha: number): CFrame
Returns a CFrame interpolated between itself and goal by the fraction alpha.
Orthonormalize
CFrame:Orthonormalize(): CFrame
Returns an orthonormalized copy of the CFrame.
ToWorldSpace
CFrame:ToWorldSpace(...: Tuple<CFrame>): Tuple<CFrame>
Receives one or more CFrame objects and returns them transformed from object to world space.
ToObjectSpace
CFrame:ToObjectSpace(...: Tuple<CFrame>): Tuple<CFrame>
Receives one or more CFrame objects and returns them transformed from world to object space.
PointToWorldSpace
CFrame:PointToWorldSpace(...: Tuple<Vector3>): Tuple<Vector3>
Receives one or more Vector3 objects and returns them transformed from object to world space.
PointToObjectSpace
CFrame:PointToObjectSpace(...: Tuple<Vector3>): Tuple<Vector3>
Receives one or more Vector3 objects and returns them transformed from world to object space.
VectorToWorldSpace
CFrame:VectorToWorldSpace(...: Tuple<Vector3>): Tuple<Vector3>
Receives one or more Vector3 objects and returns them rotated from object to world space.
VectorToObjectSpace
CFrame:VectorToObjectSpace(...: Tuple<Vector3>): Tuple<Vector3>
Receives one or more Vector3 objects and returns them rotated from world to object space.
GetComponents
CFrame:GetComponents(): Tuple
Returns the values x, y, z, R00, R01, R02, R10, R11, R12, R20, R21, and R22, where x y z represent the position of the CFrame and R00‑R22 represent its 3&times;3 rotation matrix.
ToEulerAngles
CFrame:ToEulerAngles(order: RotationOrder): number, number, number
Returns approximate angles that could be used to generate the CFrame using the optional RotationOrder.
ToEulerAnglesXYZ
CFrame:ToEulerAnglesXYZ(): number, number, number
Returns the approximate angles (in radians) that could be used to generate the CFrame using RotationOrder.XYZ.
ToEulerAnglesYXZ
CFrame:ToEulerAnglesYXZ(): number, number, number
Returns the approximate angles (in radians) that could be used to generate the CFrame using RotationOrder.YXZ.
ToOrientation
CFrame:ToOrientation(): number, number, number
Returns the approximate angles (in radians) that could be used to generate the CFrame using the YXZ rotation order.
ToAxisAngle
CFrame:ToAxisAngle(): Vector3, number
Returns a tuple containing a unit Vector3 axis and a rotation angle in radians.
components
CFrame:components(): Tuple
Equivalent to Datatype.CFrame:GetComponents().
FuzzyEq
CFrame:FuzzyEq(other: CFrame, epsilon: number): bool
Returns true if the other CFrame is sufficiently close to this CFrame in both position and rotation.
AngleBetween
CFrame:AngleBetween(other: CFrame): number
Returns the angle, in radians, between the orientation of one CFrame and another.

CFrameを使う記事

英語の説明とメンバーの一覧は、Roblox Creator Documentation(CFrame)をもとにしています(CC BY 4.0)。日本語の解説は、このサイトで独自に書いたものです。

執筆:鈴木孝昌官公庁のWeb・システム開発を担当する現役プロジェクトマネージャー。沖縄マイクラ部 クロスウェーブで、ロブロックス(Roblox)・マインクラフト(マイクラ)・Scratchなどのプログラミングを指導。マイクラカップ沖縄代表チームを指導(第6回・第7回、第7回TBS賞)。
沖縄マイクラ部で、子どもたちにロブロックス(Roblox)やマインクラフト(マイクラ)のプログラミングを教えています。プロフィールと編集方針
Instagramで教室の様子を見る
noteで記事を読む