Table of Contents

When you start creating games, you usually dive straight into the code, and if you are using Unity, you may not need to understand Linear Algebra concepts at the very start, as many of the operations are handled nicely in built-in functions; this being said, you may reach a point wen you need to understand more how linear algebra can be applied in Unity so that you can unlock some pretty cool features that you will be able to use to embellish and customize your game. However, this can be the start of endless headaches, and this the reason why I thought I would just compile the some tips and code so hat you can get started right away with Linear Algebra in Unity.

# 1 – Vector vs Scalar

In Unity, you will be using vectors to move and transform objects in a 2D or 3D environment. Most of this operations are using what is called an object’s **Transform**. A transform will store, for example, the position, scale, or rotation of your object. In most cases, these are stored using a vector notation.

For example: the position of an object is accessible using

**gameobject.transform.position**

This is a vector that has three components: x, y, and z and can be gathered using gameobject.transform.position.x, gameobject.transform.position.y, or gameobject.transform.position.z. The code would look like this:

var position:Vector3 = gameObject.transform.position; var x:float = position.x;

# 2 – Adding Vectors and Forces

In Unity you can generate forces on a solid, and, for example, simulate a force applied to a solid or a net force (sum of several forces).

Since forces can be represented as vectors, you can add several forces to calculate the net force and apply it to a Game Object that has a rigid body component. The code could look like this:

var v1:Vector3; var v2:Vector3; var v3:Vector3; function Start () { v1 = new Vector3 (0,0,1); v2 = new Vector3 (1,0,0); v3 = v1+v2; } function FixedUpdate () { rigidbody.AddForce(v3); }

# 3 – Multiplying Vectors by Scalars

In Unity, you can for instance, move/translate an object forward. However, while the movement is forward, you may need to specify the speed. Thankfully Unity provides a built-in vector called **transform.forward**, so by multiplying this vector by a scalar (a number) we can specify by how much the object is to go forward. This is because the vector **forward** has a magnitude of 1 by default (its also called a unit vector). So let’s say, if our velocity is based on the vector forward, its magnitude will be 1 (1 meter per second) and its direction will be forward.

So, to increase the speed but keep the same direction, we could just multiply this vector forward by a number. A code that does just that could be as follows:

//The velocity is used to move the NPC forward var velocity:Vector3; function Update () { //The player will be moving 1 meter forwards every second //Time.deltaTime corresponds to the number of seconds elapsed since the last frame transform.Translate(velocity * Time.deltaTime*1); }

# 4 – Rotations

You may sometimes need to rotate a game object from your script, and you can do so using Quaternions and Euler Angles. To Rotate an object, you would typically use the syntax: **transform.rotation = Quaternion.Euler(x, y, z);**

This means that we rotate x degrees around the x-axis, Y degrees around the y-axis an z degrees around the z-axis.

Put simply, these tools make it possible to specify the axis of rotation as well as the angle in Degrees. If we use the previous example and add a rotation at the start, we obtain the following code:

//The velocity is used to move the NPC forward var velocity:Vector3; function Start () { //By default the player is looking in the direction of the positive z axis //we rotate the player around the y axis so that its path crosses the NPC's field of view transform.rotation = Quaternion.Euler(0,-45,0); //we set the direction of the player using local coordinates velocity = Vector3.forward; } function Update () { //The player will be moving 1 meter forwards every second //Time.deltaTime corresponds to the number of seconds elapsed since the last frame transform.Translate(velocity * Time.deltaTime*1); }

# 5 – Dot Products

A dot product effectively tells us about the angle between these vectors and to what extent they are aligned; for example, a positive dot product indicates that the angle between the two vectors is between -90 and 90 degrees, a null dot product indicates that they are perpendicular to each-other. In Unity, the Vector3 built-in classes make it easier to use the dot product, as demonstrated below:

var v1:Vector3 = new vector (1,1,1); var v2:Vector3 = new vector (-2,-2,-2); var productOfV1AndV2:float =vector3.Dot(v1,v2);

The dot product can also make it possible to know whether vectors are aligned or pointing in the same direction, if we have initially normalized these vectors first (i.e., reduce their magnitude to 1).

This way, if they are in the exact same direction, the dot product will be 1; and if they are in the opposite direction, the dot product will be -1. This is because when we calculate the dot product, if the magnitudes of both vectors are 1, the dot product will be equal to the cosine of the angle between these two. Because the cosine is equal to one if the angle is 0 and -1 if the angle is 180, it is now easier if these vectors are aligned. In Unity, we could do this as follows:

var v1:Vector3 = new vector (1,1,1); var v2:Vector3 = new vector (-2,-2,-2); var productOfV1AndV2:float =vector3.Dot(v1.normalized,v2. normalized);

- The first two lines are similar to the previous code
- Line3: we calculate the dot product of these vectors; in this case, we will find that the dot product equals -1 (vectors are pointing to opposite directions

If you want to know more about Linear Algebra, Check my extended tutorial on Solving Your Headaches with Linear Algebra.