🤔

Positions and directions#

It's important to distinguish between positions and directions, and the various spaces they are relative to.
Common mistakes relating to these concepts include:

So let's learn how to use vectors, and how to avoid using them incorrectly.

Vectors#

First, understand that a vector is just a series of numbers.

// (0, 100)
var a = new Vector2(0, 100);
// (0, 100, 42)
var b = new Vector3(0, 100, 42);

A vector has no meaning by itself, and if I gave you either of these vectors you would have to make assumptions regarding their meaning before performing any useful operations with them. Maybe these are positions in space, but maybe they're directions to move in, rotations, colours, or perhaps packed information ready to pass to an Animator or the GPU? Consider where you got the vector from, and what transformations have been performed on it to inform its current meaning.

Frames of reference#

Picture a map where treasure is buried on an island.

World space#

In our example, world space can be interpreted as the entire map, centered on its origin (bottom left in our example), with up aligned north, and right aligned east.

Local space#

If we instead center the coordinate system on our island, this is an example of a local space.

There can be many different local spaces, it's a frame of reference aligned to anything. In Unity a transform is a way to define a new local space. There's a local space relative to any object on our island; any tree, bird, rock, or boat has its own local space.

Positions#

Think of positions as an X, as in X marks the spot.

Positions are only useful with a frame of reference.

World space#

The world space position of our X is relative to the map.

Local space#

The local space position of our X is relative to a chosen object.

Take note that while the position appears to be the same, the vectors are different. All vectors have no meaning without a frame of reference. If you're given a position you can't know where it is without knowing the space it was provided in.

Directions#

Think of directions as an arrow. A direction is just a position from the origin of its frame of reference.

Directions towards positions#

A direction towards something has no meaning without a space and an origin to be relative to. If you applied the same arrow to another position on the map you would not find the treasure.

Global space

World relative

If someone else had a map without an X drawn on it, you would provide them with the world space position so they could annotate their map.

// The world space direction relative to the world is the same as its position.
Vector3 direction = map.TreasurePosition;

Relative directions#

What if we found the treasure, but lost our map! Using our compass we can head north to find the edge of the island the boat is moored on. Note how no matter where we are on the island, north is always the same vector. It does not need an origin to be relative to.

🚧 Diagram under construction 🚧

Normalisation#

A direction can have a magnitude (a length), or be normalised. A normalised direction has a magnitude of 1, and can be multiplied with a value to be scaled to that length. This is very helpful, because we can scale the vector based on various things to move at a certain speed, or to place something a specific distance away. Without normalisation, the length of the scaled vector would vary wildly based on the magnitude of the original.

🚧 Diagram under construction 🚧


🚧 This page is currently under construction 🚧


See Vectors in the Unity Manual to learn more.
See visual debugging to learn how to visualise these vectors in Unity.