liveBook · Manning

Vector normalization

As explained in High Performance Unity Game Development

The operation of scaling a nonzero vector to have a magnitude of one while preserving its direction.

Normalizing a direction vector gives it a magnitude of one. Enemy movement uses this to keep velocity, rather than direction-vector magnitude, in control of speed. section 4.4.7

Listing 4.13 Moving enemies
static void moveEnemies(GameData gameData, Balance balance, float dt)
{
    for (int i = 0; i < balance.NumEnemies; i++)
    {
        Vector2 dir = -gameData.EnemyPosition[i].normalized;
        gameData.EnemyPosition[i] = gameData.EnemyPosition[i] + dir * balance.EnemyVelocity * dt;
    }
}

See how each enemy's normalized direction is multiplied by its velocity and the frame delta time to update its position. section 4.4.7

How High Performance Unity Game Development covers it

How deep it goes
Explains unit magnitude and its effect on speed, without deriving the normalization operation.
Examples
Demonstrates enemies moving toward a player positioned at the origin.
What you can do after
Apply Vector2.normalized to produce an enemy direction before scaling movement by velocity and delta time.
How it connects
Connects direction vectors to enemy velocity and frame delta time.

Also explained in

Compare how 2 books explain vector normalization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of High Performance Unity Game Development. The section links point to the parts of the book it draws on.