Math in Unity : Vectors (Part V)
Now things get a little more interesting.
In the previous article we saw that with this type of code, the two sprites arrive at the same instant.
The question in this case is:
how can we give the same speed of movement to both sprites?
At present it does not work because by multiplying the two vectors, we obtain different results.
The logic would be to have two Vectors of the SAME LENGTH even if they are in different points.
The method or property we use today is called NORMALIZED.
P.S. I said method or property because it can be accessed either as a PROPERTY or as a STATIC method of the Vector3 Class (just like the DISTANCE method).
Here’s the reference to the Unity manual for Vector3
As I said before, both Distance and Normalized are two static methods, in fact you can find them in this part
While the property we can see it here
The description of NORMALIZE says:
Makes this vector have a magnitude of 1.
Put simply, this method will ALWAYS return the length of the Vector (i.e. the MAGNITUDE) to 1.
This method is very useful especially when we need to multiply our Vectors by a certain “speed”.
Before seeing it in code,
the mathematical formula for calculating a Normal Vector is:
Don’t panic, it’s much less difficult than it may seem.
Exactly like || v || represents the Vector, in mathematics the v with that small symbol represents the normalized Vector.
Let’s break this formula down into small parts.
The calculation is basically:
given a Vector, we first calculate its length (remember the formula of the previous articles?)
After that, to calculate its normalized form, we need to take its vx (i.e. its x) and divide it by its length (or magnitude) and do the same thing with its vy (i.e. its y).
Let’s take an example:
if we have a Vector 3, 4
first we calculate its length
magnitude = square root of 3 * 3 + 4 * 4
9 + 16 = 25
square root of 25 = 5.
Now let’s apply the above formula to calculate the Normalized Vector
Our X will be 3/5
Our Y will be 4/5
Vector Normalized = 0.6, 0.8
In the next article we will see all of this in action in Unity!