Let’s continue our “mathematical” journey talking about the DoT Product.

The Dot Product is the sum of the multiplication of the components of a Vector.

In mathematics the written formula is this:

The small square between the v and the w is the mathematical symbol of the Dot

Let’s take an example to better understand:
if we have two Vectors V (3, 9) and W (2, 7), applying the Dot formula the result is this:

d = vx * wx + vy * wy
d = 3 * 2 + 9 * 7
d = 62

An important thing to know is that even if we are doing calculations between Vectors, the result, that is the Dot Product, IS NOT A VECTOR BUT A REAL NUMBER.

Today we finish this little test, maybe I went too far even considering that these are basic notions.

Let’s get back to us.
As I said, to try to make everything work with our new methods we still have to make a few small changes.

In my script I have…

Now let’s move on to Distance ()
Inside we pass two vectors, and calculate the distance.
The formula to be applied is the one seen in the last article, therefore

we will need the sum of

posOneX -posTwoX +
posOneY -posTwoY +
posOneZ -posTwoZ

then, the square root of the result

Let’s put it into practice!

First we calculate the difference between all points.


Now let’s try to code everything we’ve seen.
We will use a new Unity Class, called MATHF, which contains, as the word itself says, the mathematical part that we will need.

To have some fun, we will write some formulas ourselves and use MathF just to make testing easier.


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…

In this article we will slightly update the previous code.
We have seen that by calculating the Vector v and adding it to our position, our sprite (in the Update ()), moves at a constant speed and stops exactly above the circle.
Everything good. …

In the previous article we used a speed variable to give our sprite a “speed”.

In that case, multiplying the Vector by a value, its DIRECTION will always remain the same, but its LENGTH will change (MAGNITUDE).

If our Vector were (1f, 1f), multiplying it by a number greater than…

Now let’s do a couple of practical examples on how to move our sprite.
I created a simple script called SpriteMove

For example, if we had a Vector 0.5f, 0.5f, 0

Before we move on, we need to understand this :
what is a Vector?

If I said that a Vector is a point in a Cartesian plane, would that be right?

For example, if in any 2D project I create a sprite, and put it in this position:

As for the second problem, we should calculate some sort of “offset” and draw the lines based on that offset multiplied by the size of the grid.

For example: if we have our dimension of 13, our offset will be, in this case, 160 divided by 13.
Remember: our size…

Matteo Lo Piccolo

Always in love with programming, even if late (I'm already 39 years old) I decided to follow my dream! We will see how far my passion will take me!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store