Vector3データ型

データ型日本語解説あり

ひとことで言うと

3つの数(X・Y・Z)で、位置・大きさ・方向などを表すデータ型です。Vector3.new(0, 10, 0) のように作ります。

公式の説明(英語):Represents a 3D value with a direction and magnitude.

Xは左右、Yは上下、Zは前後の方向を表します。Vector3どうしは足し算・引き算ができ、Magnitudeで長さ(距離)を求められます。

使用例

位置と距離を扱う
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)で確認しています。

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

名前と書き方説明
new
Vector3.new(x: number, y: number, z: number)
X・Y・Zを指定してVector3を作ります。Returns a new Vector3 from the given x, y, and z components.
FromNormalId
Vector3.FromNormalId(normal: NormalId)
Returns a new Vector3 in the given direction.
FromAxis
Vector3.FromAxis(axis: Axis)
Returns a new Vector3 for the given axis.

プロパティ(10)

名前と書き方説明
zero
Vector3.zero: Vector3
すべて0のベクトル。A Vector3 with a magnitude of 0.
one
Vector3.one: Vector3
A Vector3 with a value of 1 on every axis.
xAxis
Vector3.xAxis: Vector3
A Vector3 with a value of 1 on the **X** axis.
yAxis
Vector3.yAxis: Vector3
A Vector3 with a value of 1 on the **Y** axis.
zAxis
Vector3.zAxis: Vector3
A Vector3 with a value of 1 on the **Z** axis.
X
Vector3.X: number
X(左右)の値。The **X** coordinate of the Vector3.
Y
Vector3.Y: number
Y(上下)の値。The **Y** coordinate of the Vector3.
Z
Vector3.Z: number
Z(前後)の値。The **Z** coordinate of the Vector3.
Magnitude
Vector3.Magnitude: number
ベクトルの長さ。2点の差のMagnitudeで、距離が求められます。The length of the Vector3.
Unit
Vector3.Unit: Vector3
長さを1にしたベクトル(向きだけを表す)。A normalized copy of the Vector3 - one that has the same direction as the original but a magnitude of 1.

メソッド(11)

名前と書き方説明
Abs
Vector3:Abs(): Vector3
Returns a new vector from the absolute values of the original's components.
Ceil
Vector3:Ceil(): Vector3
Returns a new vector from the ceiling of the original's components.
Floor
Vector3:Floor(): Vector3
Returns a new vector from the floor of the original's components.
Sign
Vector3:Sign(): Vector3
Returns a new vector from the sign (-1, 0, or 1) of the original's components.
Cross
Vector3:Cross(other: Vector3): Vector3
Returns the cross product of the two vectors.
Angle
Vector3:Angle(other: Vector3, axis: Vector3): number
Returns the angle in radians between the two vectors. If you provide an axis, it determines the sign of the angle.
Dot
Vector3:Dot(other: Vector3): number
Returns a scalar dot product of the two vectors.
FuzzyEq
Vector3:FuzzyEq(other: Vector3, epsilon: number): bool
Returns true if the difference between the squared magnitude of the two vectors is within epsilon. epsilon is scaled relative to the magnitude, rather than an absolute epsilon.
Lerp
Vector3:Lerp(goal: Vector3, alpha: number): Vector3
Returns a Vector3 linearly interpolated between this Vector3 and the given goal by the given alpha.
Max
Vector3:Max(vector: Vector3): Vector3
Returns a Vector3 with each component as the highest among the respective components of both provided Vector3 objects.
Min
Vector3:Min(vector: Vector3): Vector3
Returns a Vector3 with each component as the lowest among the respective components of both provided Vector3 objects.

Vector3を使う記事

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

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