# Unity: How to Normalize a Vector

May 30, 2023 ## Overview

Vectors play a fundamental role in various fields, including mathematics, physics, computer science, and game development. A vector represents both magnitude and direction, and normalization is a fairly standard operation that allows us to normalize (hehe) vectors to simplify comparison in various use cases. In this article, I’ll help you understand what vector normalization is, understand why it is important, and walk through some methods to normalize a vector in C# and in a shader in Unity.

## Introduction

Before diving into vector normalization, let’s first take a second to break down what a vector is and why we might want to normalize it. In simple terms, a vector is an object that represents the magnitude and direction of a line in space. Vectors can exist in two-dimensional, three-dimensional, or n-dimensional space. Developers often use vectors to describe things like velocity, force, and position. But, they can also be used to describe directions like view direction, forward direction, and relative position.

Vector normalization is the process of transforming any vector into a unit vector. A unit vector is a vector with a length (or magnitude) of 1 unit. By normalizing a vector, we throw away the original magnitude but keep all of the directional information. Why would we want to discard some information? Well, this helps us when using operations like the dot product to compare the directionality of two vectors for lighting calculations and helps us to normalize keyboard input to ensure player movement is even across all directions.

## Understanding Vector Normalization

To better understand how normalizing a vector works, let’s take a quick math detour. To normalize a vector, you need to divide each component of the vector by the length of the vector. The magnitude (length) of a vector represents its length or size. You can calculate the length using the Pythagorean theorem.

The formula for normalizing a vector v with components (x, y, z) can be expressed as:

[.c]vectorNormalized = v / ||v||[.c]

Where vectorNormalized is the resulting unit vector, v is the original vector, and ||v|| represents the magnitude (length) of vector v.

## Steps to Normalize a Vector

Normalizing a vector involves a few simple steps. Let's go through them to understand the process clearly.

### Step 1: Calculate the Magnitude of the Vector

The magnitude of a vector can be calculated using the following formula:

[.c]vectorLength = Mathf.Sqrt(x*x + y*y + z*z)[.c]

Here, x, y, and z represent the components of the vector.

The Magnitude is also sometimes called the Length of the vector. As you can see, this calculation involves a square root.

Sometimes game developers look to avoid square roots and so they compare the results of the squared magnitude (i.e., before calculating the square root) to avoid the square root call. This lets you compare the length of two vectors to understand which is longer without performing the square root call. I use this approach when comparing the distance to the nearest lights and fog volumes in Buto, my volumetric fog asset.

### Step 2: Divide Each Component by the Magnitude

Next, we divide each component of the vector by the magnitude of the entire vector:

[.c]xNormalized = x / vectorLength[.c]

[.c]yNormalized = y / vectorLength[.c]

[.c]zNormalized = z / vectorLength[.c]

### Step 3: Normalize the Vector

Finally, we obtain the normalized vector by combining the normalized components:

[.c]vectorNormalized = new Vector3(xNormalized , yNormalized , zNormalized )[.c]

## Methods to Normalize a Vector in Unity using C#

Vectors in C# in Unity are special types part of the UnityEngine library. Unity includes static methods for normalizing Vector2, Vector3, and Vector4 types as well as a normalized property for the struct.

To call the static method, you can simply call Vector3.Normalize(x), like this:

[.c]Vector3 vector = new Vector3(1,0,0);[.c]

[.c]Vector3 normalizedVector = Vector3.Normalize(vector);[.c]

To use the property accessor instead, you can simply access the .normalized property, like this:

[.c]Vector3 vector = new Vector3(1,0,0);[.c]

[.c]Vector3 normalizedVector = vector.normalized;[.c]

## Methods to Normalize a Vector in Unity in a Shader

Vectors in HLSL in Unity are simply basic types (like floats or ints) with multiple components, like a float2 or a float3. You also see these types pop up in the Unity mathematics library.

To normalize a vector like this in HLSL, you can simply call the normalize(x) intrinsic, like this:

[.c]float3 vector = float3(1,0,0);[.c]

[.c]float3 normalizedVector = normalize(vector);[.c]

The intrinsic works on any size input, so you can pass in a float2, float3, or even a float4.

It works by dividing the input vector (x) by the length of x.

## Why Normalize Vectors in Unity?

Vector normalization offers several benefits in various applications. Let's explore some of these advantages:

### Simplify Calculations

Normalized vectors simplify calculations involving direction-dependent operations, such as dot products or angle calculations. When the magnitude of a vector is 1, you no longer need to re-scale the vector relative to the comparison vector. This makes it easier and faster to compute alignment between two vectors, for example.

### Ensure Consistent Scaling

By normalizing vectors, we remove their original magnitudes, resulting in vectors with a fixed length. This property is useful when we want to ensure consistent scaling of vectors in applications like computer graphics or physics simulations.

### Facilitate Comparisons

Normalized vectors provide a common reference point for comparison. When vectors are normalized, their direction becomes the primary focus, enabling easier comparisons between vectors based on their orientations.

Think about it like this - if you’re trying to compare two vectors and they’re different magnitudes, it can be difficult to tell which one points more along the x axis, for example. When they’re normalized, it can be much easier to identify things like this.

## Applications of Vector Normalization

The concept of vector normalization finds applications in various fields.

### Graphics and Rendering

In graphics and rendering, developers often use normalized vectors for lighting, surface normal, and reflection calculations. One common operation is the dot product, which tells you something about how aligned two vectors are.

When you use the dot product with two normalized vectors, you get back a scalar value between -1 and 1 that tells you the extent to which the two vectors are aligned (1 meaning they point in the same direction, -1 meaning they point in opposite directions). For example, you can use this information to understand how lit the surface of an object is.

So, by using normalized vectors, developers can author shaders that create realistic lighting and reflections in games.

### Movement and Physics

In movement and physics, you will often want to normalize your movement input vectors so that players don’t move faster on diagonal axes. By normalizing your movement input vectors before applying them to your character’s velocity and ensuring that your final movement is normalized relative to your maximum speed, you can avoid movement bugs.

## Conclusion

Vector normalization is a standard operation when working with vectors. This operation transforms any vector into a unit vector. This action helps to simplify calculations, ensure consistent scaling, and simplifies comparisons between two vectors. By following the steps outlined in this article, you can easily normalize any vector using Unity in both C# and in your shaders. Thanks for reading!

## FAQs

### Q: Does normalizing a vector affect its direction?

A: No, vector normalization preserves the direction of a vector while adjusting each of its components so that the final vector has a magnitude of 1.

### Q: Is vector normalization the same as vector scaling?

A: No, vector normalization adjusts the vector such that it has a magnitude of one, while vector scaling adjusts each component of the vector according to the corresponding scaling factor.

### Q: Can I normalize a vector with zero magnitude?

A: No, you can’t normalize a vector that has zero magnitude. Since vector normalization involves dividing each component by the magnitude, you would be trying to divide each component by zero. Division by zero is undefined, so the result of this operation is also undefined. In practice, Unity’s Normalize methods will set the vector to zero and return back a new vector with zero magnitude as well rather than throw an error. ##### Michael Sacco
###### Founder & CEO

Michael Sacco is the Founder and CEO of OccaSoftware where he specializes in developing game assets for Unity game developers. With a background ranging from startups to American Express, he's been building great products for more than 10 years.

##### Tags:
programming
resources
technical art