Directions in Unity

Direction Vectors in Unity

In Movement by John FrenchPublished Leave a Comment

A direction in Unity is typically described using a normalised vector, otherwise known as a unit vector, which is simply a vector that only describes a direction and always has a length of 1.

For example the unit vector (0,0,1) describes the forward direction in world space, while (0,0,-1) would be backwards.

Directions are extremely important in Unity, and can be used to create movement, position offsets, and to define the orientation of Raycasts

Most of the time, you’ll use direction vectors to calculate distance and speed.

For example, you could multiply a unit vector by 10 to get a position that’s ten units away in a certain direction.

Like this:

// Create an offset 10 units in front of this object.
Vector3 offset = transform.position + (Vector3.forward * 10);

Or you can multiply a forward vector by a speed value and by delta time to create movement at an exact speed in units per second.

Like this:

public float speed = 5;

void Update()
{
    // Moves an object forward, relative to its own rotation.
    transform.position += Vector3.forward * speed * Time.deltaTime;
}

In both of these examples, you might have noticed that I’m not typing the forward vector out manually.

This is because, in Unity, you’ll find a number of shorthand expressions for common directional vectors in the Vector 3 class already.

Global Vector 3 directions

The Vector 3 class in Unity contains ready-made direction values that can be used in place of a manually typed vector 3.

Essentially, they are shorthand vectors for directions you’re likely to use all the time, such as forward, back, up, down, left, and right.

Like this:

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

These relate to basic directions in world space and are typically easier to use than creating a new Vector 3.

However, the Transform component also provides a number of direction vectors for forward, up and right in local space, that are relative to an object’s orientation.

Local Transform directions

Local Transform directions are built-in unit vectors in Unity that describe direction relative to an object’s transform component, in local space.

They can be extremely useful for defining directions that would otherwise be difficult to work out manually, such as the direction an object is facing, for example.

While their opposites, the negative values of their directions, can be used to create directional vectors for back, down and left.

Like this:

transform.forward  // object forward
-transform.forward // object back
transform.up       // object up
-transform.up      // object down
transform.right    // object right
-transform.right   // object left

These ready-made directions can be extremely useful for creating object-relative movement.

For example, all you need to do to move an object forward in the direction it’s facing, is multiply its local forward direction by the speed you want the object to move, scaled by delta time.

Like this:

public float speed = 2;

void Update()
{
    // Moves an object forward, relative to its own rotation.
    transform.position += transform.forward * speed * Time.deltaTime;
}

However…

While using a transform’s local directions can be useful for getting the orientation of a specific object, some functions already operate in local space by default.

Such as the Translate Function, which applies movement relative to the object’s local position and orientation, unless you set otherwise.

What this means is, that, if you use a local direction with the Translate function, you’ll get a compound effect, where the local offset is applied twice, which can cause problems with direction and orientation that can be confusing to solve.

While ready made direction vectors can be useful for offsetting a position or moving an object in a particular direction, sometimes you may need to create your own direction value.

Such as pointing towards another object, for example.

How to get the direction towards another object in Unity

It’s possible to calculate the direction between two objects in Unity by subtracting their positions.

Specifically, by subtracting the position of the direction’s target from its origin.

Like this:

Vector3 direction = (transform.position - directionTarget.position).normalized;

Normalising the result means that the length of the vector is one, meaning that you can calculate distance along that direction, just like with any other unit vector, more easily, by multiplying it by a unit value.

John French profile picture

by John French

Game audio professional and a keen amateur developer.

Get Game Development Tips, Straight to Your inbox

Get helpful tips & tricks and master game development basics the easy way, with deep-dive tutorials and guides.

How to CODE in Unity. Available to Pre Order Now. Game Dev Beginner.

Leave a Comment