---
title: ベクトルで移動するオブジェクト
product: Unity Editor
version: '6000.0'
latest_version: '7000.0'
status: This page documents version 6000.0. The latest version is 7000.0.
canonical: https://docs.unity.com/ja-jp/engine/6000.0/manual/scripting/object-oriented-development/programming-math/vectors
latest_url: https://docs.unity.com/ja-jp/engine/7000.0/manual/scripting/object-oriented-development/programming-math/vectors.md
generated_at: 2026-11-14T04:39:03.679Z
locale: ja-jp
---

# ベクトルで移動するオブジェクト

> Unityの2D、3D、4D VectorクラスでVector演算を実行して、オブジェクトの位置とスピード、オブジェクト間の距離を管理します。

ベクトルは数学の基本的な概念で、向きと大きさを記述できます。ゲームやアプリケーションでは、キャラクターの位置、何かが移動するスピード、2 つのオブジェクト間の距離などの基本的なプロパティを表すためにベクトルがよく使用されます。

Vector演算は、グラフィックス、物理演算、アニメーションなど、コンピューター プログラミングのさまざまな側面の基礎であり、深度で理解することでUnityを最大限に活用できます。

ベクトルは複数の次元で表現できます。Unity は 2D、3D、4D ベクトルを扱うために Vector2、Vector3、Vector4 クラスを提供します。これら 3 つのタイプの Vector クラスはすべて、magnitude などの同じ関数の多くを共有します。そのため、このページに記載されている情報のほとんどは、特に指定のない限り、3 つのタイプの Vector すべてに適用されます。

このページでは、Vector クラスの概要と、Vector クラスを使用してスクリプトを作成する際の一般的な使用方法を説明します。Vector クラスのすべてのメンバーの詳細なリファレンスについては、[Vector2](/engine/6000.0/script-reference/unityengine/vector2.md)、[Vector3](/engine/6000.0/script-reference/unityengine/vector3.md)、[Vector4](/engine/6000.0/script-reference/unityengine/vector4.md) のスクリプトリファレンスページを参照してください。

## Vector 演算について##understanding-vector-arithmetic

### 加算##addition

2 つのベクトルを加算すると、結果は元のベクトルを 1 つずつ "ステップ" として取るのと同じです。どちらの方法でも結果は同じであるため、2 つのパラメータの順序は関係ありません。


**Vector a is a 2D vector with x component 3 and y component 5. Vector b is a 2D vector with x component 2 and y component -1. Adding a and b results in vector c, a 2D vector with x component 5 and y component 4. The relation of the three vectors to the origin point forms a scalene triangle.:**
![](/api/media?file=/engine/6000.0/media/images/VectorAdd.png)

最初のVectorがスペース内の点として取得される場合、2番目のVectorはその位置からのオフセットまたは"ジャンプ"として解釈できます。例えば、地上の位置から 5 ユニット上の点を求めるには、以下の計算を使用します。

```
 var pointInAir = pointOnGround + new Vector2(0, 5);
```

ベクトルが力を表す場合は、向きと大きさで考える方が直感的です(大きさは力の大きさを示します)。2 つの力ベクトルを加えると、力の組み合わせと同等の新しい Vector が作成されます。この概念は、複数の別々のコンポーネントが同時に作用して力を適用する場合に、しばしば役立ちます (例えば、フォワード推進するロケットも横風の影響を受ける場合があります)。

この例では 2D ベクトルを使用していますが、同じ概念が 3D および 4D ベクトルにも当てはまります。

### 減算##subtraction

Vector Subtraction は、あるオブジェクトから別のオブジェクトへの向きと距離を求めるのに最もよく使用されます。減算では、2 つのパラメータの順序が重要**です**。


**2D vector a is subtracted from 2D vector b resulting in a 2D vector with x component 5 and y component 4. This is illustrated as a single line with directional arrow from point a to point b.:**
![](/api/media?file=/engine/6000.0/media/images/VectorSubtract.png)

```lang-cs
// The vector d has the same magnitude as c but points in the opposite direction.
var c = b - a;
var d = a - b;
```

数値と同様に、Vector の負の値の加算は、正の値の減算と同じです。

```lang-cs
// These both give the same result.
var c = a - b;
var c = a + -b;
```

Vector の負の値は元の大きさと同じで、同じ線に沿って、ただしまったく逆の向きを指します。

## あるオブジェクトから別のオブジェクトへの向きと距離##direction-and-distance-from-one-object-to-another

スペース内の 1 点を別の点から引くと、結果は、1 つのオブジェクトからもう 1 つのオブジェクトを "指す" Vector になります。

```lang-cs
// Gets a vector that points from the player's position to the target's.
var heading = target.position - player.position;
```

ターゲット オブジェクトの向きを指すだけでなく、この Vector の大きさは 2 つの位置間の距離と等しくなります。ターゲットへの向きを示す「正規化された」Vectorが必要な場合がありますが、距離は固定されています(砲弾の向きなど)。Vector をその Vector の大きさで割ることで正規化できます。

```lang-cs
var distance = heading.magnitude;
var direction = heading / distance; // This is now the normalized direction.
```

この方法は、magnitude と正規化されたされたプロパティの両方を別々に使用するよりも望ましいです。なぜなら、両方とも CPU にかなり負荷がかかるからです (どちらもルートの計算を伴うからです)。

比較に距離だけを使用する場合 (例えば、近接度チェック)、大きさの計算を完全に回避できます。`sqrMagnitude` プロパティは、 magnitude 値の正方形を指定します。 magnitude と同じように計算されますが、時間のかかる正方形のルート演算は必要ありません。大きさを既知の距離と比較するのではなく、大きさの 2 乗を距離の 2 乗と比較できます。

```lang-cs
if (heading.sqrMagnitude < maxRange * maxRange) {
	// Target is within range.
}
```

これは、比較で true magnitude を使用するよりもはるかに効率的です。

3D で作業するときに、ターゲットへの "地上の見出し" が必要な場合があります。例えば、地面に立っているプレイヤーが、空中に浮かんでいるターゲットに近づく必要があるとします。ターゲットからプレイヤーの位置を差し引くと、結果のVectorはターゲットに向かって上向きになります。プレイヤーの Transform の方向付けにも適していません。なぜなら、プレイヤーの位置からターゲットの真下の地上の位置までの Vector が実際に必要だからです。これは、減算の結果を取得し、Y 座標を 0 に設定すると得られます。

```lang-cs
var heading = target.position - player.position;
heading.y = 0;	// This is the overground heading.
```

## スカラー乗算と除算##scalar-multiplication-and-division

ベクトルについて説明する場合、通常の数(例えば Float 値)をスカラーと呼ぶのが一般的です。この意味は、スカラーは "スケール" または magnitude のみを持ち、Vector は magnitude と向きの両方を持つということです。

Vector にスカラーを乗算すると、元と同じ向きを指す Vector になります。ただし、新しい Vector の大きさは、元の大きさにスカラー値を乗算したものと等しくなります。

同様に、スカラー除算は元の Vector の大きさをスカラーで除算します。

これらの演算は、Vector が移動のオフセットまたは力を表す場合に便利です。Vector の向きに影響を与えずに Vector の大きさを変更できます。

Vector を自身の magnitude で割った結果は、magnitude が 1 の Vector になります。これは正規化された Vector と呼ばれます。正規化された Vector にスカラーを乗算すると、結果の大きさはそのスカラー値と等しくなります。これは、力の向きは定数だが強さは制御可能な場合に役立ちます (例えば、車のホイールからの力は常に前に押し出されるが、力はドライバーによって制御される)。

## ドット積##dot-product

ドット積は 2 つのベクトルを取り、スカラーを返します。このスカラーは、2 つのベクトルを乗算した大きさと、ベクトル間の角度の余弦を乗算した結果と等しくなります。両方のベクトルが正規化されたされている場合、余弦は本質的に最初のVectorが 2 番目のベクトルの向きにどれだけ伸びるかを示します (またはその逆。パラメーターの順序は関係ありません)。


**Two 2D vectors represented as directional lines extend from the same origin point. Vector a extends straight upward with a positive y component but no x component. Vector b extends upward by a smaller y component than vector a and to the right with a positive x component. A line describing the region of the y axis covered by both vectors is labelled as a multiplied by b.:**
![](/api/media?file=/engine/6000.0/media/images/DotProduct.png)

リファレンス Vector と比較したさまざまな角度の Vector が、 1 から -1 の間のドット積の値を返す方法の比較を次に示します 。


**A series of vectors radiate out at angles between 0 and 180 degrees from a reference vector. Between 0 and 90 degrees from the reference vector, the dot product is between 1 and 0, and decreases as the angle increases. Between 90 and 180 degrees from the reference vector, the dot product is between 0 and -1, and decreases as the angle increases.:**
![](/api/media?file=/engine/6000.0/media/images/CosineValues.png)

ドット積は余弦を計算するよりも数学的に簡単な演算です。そのため、状況によっては `Mathf.Cos` 関数や Vector magnitude 演算の代わりに使用できます (まったく同じ働きをするわけではありませんが、同等のエフェクトが得られる場合もあります)。ただし、ドット積機能の計算は CPU 時間をはるかに短縮するため、有用な最適化になります。

ドット積は、ある Vector の大きさが別の Vector の向きにある量を計算したい場合に便利です。

例えば、車のスピードメーターは通常、タイヤの回転スピードを測定することで機能します。車は真正面にフォワード移動していない場合があります (例えば横滑りなど)。この場合、モーションの一部は車が向いている向きにならず、スピードメーターによって測定されません。オブジェクトの `rigidbody.velocity` Vector の大きさは、全体的な動きの向きのスピードを与えますが、フォワード向きのスピードを分離するには、ドット積を使用します。

```lang-cs
var fwdSpeed = Vector3.Dot(rigidbody.velocity, transform.forward);
```

向きは好きなように設定できますが、向きVectorはこの計算のために常に正正規化されたされている必要があります。結果は速度の大きさよりも正確であるだけでなく、大きさを求めるための遅い正方形ルート演算も回避されます。

## クロス積##cross-product

クロス積は 3D ベクトルに対してのみ意味があります。2 つの 3DD ベクトルを入力として受け取り、別の 3DD Vectorを結果として返します。

結果 Vector は 2 つの入力 Vector に垂直です。"右手の法則" を使用して、入力 Vector の順序から出力 Vector の向きを記憶できます。入力 Vector の順に指をカールすると、親指は出力 Vector の向きを指します。パラメータの順序が逆の場合、結果の Vector は正確に反対向きを指しますが、大きさは同じになります。

結果の大きさは、ともに乗算した入力ベクトルの大きさに、それらの間の角度の正弦を乗算した値と等しくなります。正弦機能の便利な値を次に示します。


**A series of vectors radiate out at angles between 0 and 180 degrees from a reference vector. Between 0 and 90 degrees from the reference vector, the cross product is between 0 and 1, and increases as the angle increases. Between 90 and 180 degrees from the reference vector, the cross product is between 1 and 0, and decreases as the angle increases.:**
![](/api/media?file=/engine/6000.0/media/images/SineValues.png)

クロス積は、その戻り値にいくつかの有用な情報を組み合わせるため、複雑に見える場合があります。ただし、ドット積と同様に数学的に非常に効率的であり、正弦や余弦などの低速の超越関数に依存するコードの最適化に使用できます。

## 法線/垂直 Vector の計算##computing-a-normal/perpendicular-vector

"法線" Vector (つまり、平面に垂直な Vector) は、メッシュの生成時に頻繁に必要とされ、パス フォローやその他の状況でも役立ちます。平面上の 3 つの点、例えばメッシュ三角形の角の点から、次のように法線を求めることができます。

* 3 つのポイントのいずれかを選択します。
* 他の 2 点から別々に引き算します (その結果、2 つの新しいベクトル "Side 1" と "Side 2")。
* ベクトル "Side 1" と "Side 2" のクロス積を計算します。
* クロス積の結果は、 3 つの元の点がある平面に垂直な新しい Vector 、つまり "法線" です 。


**Three points; a, b, and c, in 3D coordinate space form the corner points of a triangle. Vector subtraction of b and c from a gives the long sides of the triangle, side 1 from a to b, and side 2 from a to b. The cross product of the vectors side 1 and side 2 gives a new vector perpendicular to the plane on which the three original points lie.:**
![](/api/media?file=/engine/6000.0/media/images/CalculateNormal.png)

```lang-cs
Vector3 a;
Vector3 b;
Vector3 c;

Vector3 side1 = b - a;
Vector3 side2 = c - a;

Vector3 normal = Vector3.Cross(side1, side2);
```


**A left hand demonstrates the left hand rule for determining the input order of vectors for calculating a cross product. The forefinger and thumb are extended at right angles to one another while the middle finger stretches out in a third plane at right angles to the forefinger. The forefinger and thumb represent the first and second input vectors to the cross product calculation respectively. The middle finger represents the resulting normal.:**
![](/api/media?file=/engine/6000.0/media/images/LeftHandRuleDiagram.png)

"左手の法則" は、2 つのベクトルをクロス機能数に渡す順序を決定するために使用できます。サーフェスの上側の外観(法線が外側を向く位置)を見ると、最初の Vector は 2 番目の Vector に右回りでスイープします。

入力ベクトルの順序が逆の場合、結果は正確に反対の向きを指します。

メッシュの場合、法線Vectorも正正規化されたする必要があります。これは正規化されたプロパティで行うことができますが、別のテクニックが役立つことがあります。垂直Vectorを大きさで割って正規化することもできます。

```lang-cs
float perpLength = perp.magnitude;
perp /= perpLength;
```

もう 1 つの便利な点は、三角形の面積は `perpLength` / 2 に等しいことです。これは、メッシュ全体のサーフェス領域を求める場合や、三角形を相対的な領域に基づいてランダムに選択したい場合に便利です。

## 追加リソース##additional-resources

* [Vector2 APIリファレンス](/engine/6000.0/script-reference/unityengine/vector2.md)
* [Vector3 API reference](/engine/6000.0/script-reference/unityengine/vector3.md)
* [Vector4 APIリファレンス](/engine/6000.0/script-reference/unityengine/vector4.md)
