Vector3
TypeStruct (value type)
NamespaceUnityEngine
RelatedVector2 (2D), Vector3Int

Vector3

A Vector3 is three numbers: x, y and z. Unity uses it for two different things: a position (a point in the world) and a direction with a length (like velocity: "this way, this fast").

A position is a pin on a map. A direction is an arrow drawn on the map: it points somewhere and has a length, but you can draw it anywhere. Subtracting two pins (b - a) gives you the arrow from pin a to pin b.

Constructors

Vector3 v = new Vector3(1f, 2f, 3f);

Static Properties (shortcuts)

NameValue
Vector3.zero(0, 0, 0)
Vector3.one(1, 1, 1)
Vector3.up / down(0, 1, 0) / (0, -1, 0)
Vector3.right / left(1, 0, 0) / (-1, 0, 0)
Vector3.forward / back(0, 0, 1) / (0, 0, -1)

These are world directions. For the direction an object is facing, use transform.forward.

Properties

NameTypeDescription
x, y, zfloatThe three components.
magnitudefloatThe length of the arrow. For velocity, this is the speed.
sqrMagnitudefloatLength squared. Faster (skips a square root). Good for comparing distances.
normalizedVector3Same direction, length 1. Use when you only care about direction.

Static Methods

MethodReturnsDescription
Distance(a, b)floatDistance between two points.
Lerp(a, b, t)Vector3A point part-way from a to b. t = 0 is a, 0.5 is halfway, 1 is b.
MoveTowards(current, target, maxDistanceDelta)Vector3Steps from current toward target by at most that distance. Never overshoots.
SmoothDamp(current, target, ref velocity, smoothTime)Vector3Moves smoothly, like a spring that slows down as it arrives. Great for cameras.
Dot(a, b)floatHow much two directions agree. With normalized vectors: 1 = same way, 0 = at right angles, -1 = opposite.
Cross(a, b)Vector3A direction perpendicular to both.
Angle(from, to)floatAngle between two directions in degrees (0 to 180).
ClampMagnitude(v, maxLength)Vector3Shortens v if it's longer than maxLength.
ProjectOnPlane(v, planeNormal)Vector3Flattens a vector onto a surface. E.g. make a camera direction horizontal.
Reflect(dir, normal)Vector3Bounces a direction off a surface.

Operators

CodeMeaning
a + bMove point a by arrow b.
b - aThe arrow from a to b.
v * 2fTwice as long, same direction.
-vOpposite direction.
a == bTrue if approximately equal (tiny floating point differences are ignored).

Code Samples

Chase the player

using UnityEngine;

public class Chaser : MonoBehaviour
{
    [SerializeField] private Transform player;
    [SerializeField] private float speed = 3f;

    void Update()
    {
        // Step toward the player. MoveTowards stops exactly on target, no jitter.
        transform.position = Vector3.MoveTowards(
            transform.position, player.position, speed * Time.deltaTime);
    }
}

Direction and distance

Vector3 toPlayer = player.position - transform.position; // arrow from me to player
float distance = toPlayer.magnitude;                     // how far
Vector3 direction = toPlayer.normalized;                 // which way (length 1)
a (enemy)b (player)b - a(b - a).magnitude = distance(b - a).normalized:same direction, length 1
b - a is the arrow from a to b (highlighted). Its length is the distance between them; normalizing keeps the direction but sets the length to 1, which is what you multiply by speed.

Fast range check

float range = 10f;
// Compare squared values: avoids a square root every frame.
if ((player.position - transform.position).sqrMagnitude < range * range)
{
    Debug.Log("In range");
}

Is the player in my field of view?

Vector3 toPlayer = (player.position - transform.position).normalized;
float dot = Vector3.Dot(transform.forward, toPlayer);
if (dot > 0.5f) // roughly within 60 degrees either side of forward
{
    Debug.Log("I can see you");
}
60°forwardyouDot = 1 (ahead)Dot = 0.5Dot = 0 (to the side)Dot = -1 (behind)Dot > 0.5:inside the cone
Vector3.Dot(transform.forward, toTarget.normalized) for targets around you. Checking dot > 0.5f means the target is within 60° either side of where you're facing.

Diagonal movement that isn't faster

Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
// Pressing W+D gives (1, 0, 1), which has length 1.41. That's 41% faster diagonally.
input = Vector3.ClampMagnitude(input, 1f);
transform.position += input * speed * Time.deltaTime;

What happens at runtime

Vector3 is a struct, which means it's copied whenever you assign it or pass it to a method, like an int. Changing a copy doesn't change the original:

Vector3 p = transform.position; // p is a copy
p.y = 10f;                      // changes only the copy
transform.position = p;         // now the object moves

Vector3 math is very cheap and creates no garbage, so it's fine to use freely in Update.

Common Mistakes

See also