In the previous lecture, we looked at calculating distance using Pythagoras theorem.
We're going to now look at some other ways that unity can do it with its built in methods.
Okay, so let's go back into our code so we can add to it.
So this is our calculate distance and this is Pythagoras.
Now underneath here we'll create another float and we'll call it U Distance.
So this is Unity's distance calculation.
Now, unity is distance.
Calculation can take two positions in space and work out the difference between them.
Okay, so we're actually doing this with the fuel and also with our tank.
So in unity we can go vector three distance because it's actually in the vector three class and then
give it to position values which are also stored as vectors because it's an X, Y and z value.
So we can go fuel dot transform dot position and transform position.
It doesn't matter which way around you have these because you're working at the distance, not a direction.
Okay, so there we go.
There's our u distance.
And then we can add another debug log underneath here just so that we can see what it is.
So we'll put you distance is equal to you.
Distance like that in the calculations.
Okay, so we'll save this and we'll go back into Unity Press play and we'll see how accurate our two
calculations are.
So with play pressed, we're going to.
Press space to start with.
Now let's have a look at what we're doing down here.
We've got 49, which is our distance we've calculated with Pythagoras.
And then we have our unity distance, which says 53.
Why are these different?
You probably were taking notice as I was typing that out.
Let me find that code for you.
Remember that this distance here is only in two dimensions X and Z.
This distance here is calculating the distance in three dimensions, essentially, because you've got
the three dimension of that position and the three dimension of that position.
So if we go back into unity and have a look at our fuel, what we're quite interested in here is the
Y value of the fuel because it's now being included in the calculation of Unity's distance.
So the fuel is actually, in my case, 9.8.
And if you haven't moved it, it should still be there.
The tank, if we look at that, the tank's position, look at its Y position is -11.3.
So it's giving you different values that it's calculating its distance based on.
If you really don't care about the heights of these things, of course you could set them to the same
thing or you could actually force it in the code.
Let me show you how to do that.
So stop playing.
We'll go back into our code and in here we want to set these values with a Y value of zero so we can
actually say instead vector three and this will be our fuel.
Pause equals new vector three and we can set this to field transformed position x zero for the Y and
then fuel dot transform dot position dot z like that and we can do a vector three for our tank.
Pause equals pretty much the same thing, so we'll just grab this.
And put it down here and just change this to transform and transform.
So now these two positions have zero in the Y.
They will not affect the location of your tank game object or the fuel object.
You just grab the values out of them and using them by setting the Y to zero.
This is completely legitimate and sometimes you really don't care what the height is.
You're more interested in the horizontal distance between things and that's how you would do it.
Okay, so now in working out the you distance down here, it's actually going to be fuel pause and tank
pause like that.
Okay, so save this and we'll go and try it out and see if that fixes up our calculations.
So let's now press play and we'll hit the old space bar immediately and let's have a look at our calculations
much, much better and far more accurate.
And then, of course, if I come and move my tank around, it will recalculate both of them to be in
the same spot.
Great.
That works well.
So that's finding the distance between two points.
If we calculate the vector between this point and this point, we can also then find out the length
of the vector, which is the distance between these two points.
Just like we used magnitude before when we had a little villager and the pigs chasing him around.
So let me show you how we would do that.
Back in the code again, we're going to calculate another vector, this case.
So vector three and we will use these two positions to do the vector between, okay, because we know
that they've zeroed on the Y.
So this will be a tank to fuel and that's going to equal the fuel pause minus the tank, pause like
that.
Then we just get the magnitude of this.
So let's now do debug log and we'll go vector.
So like v magnitude and in here we'll put tank to fuel dot magnitude like that, which is what we used
previously.
Okay.
So saving, let's go back into unity.
We're going to press play and have a look at our calculations.
So we press play.
And then I hit the spacebar and we get three values that are all exactly the same, but we've got different
ways of actually calculating them.
Great.
So that's how you can work out the distance.
And we've already seen magnitude before.
Before we go, one thing I'd like to point out with this tank to fuel, let's also grab this line and
I want to put it down here because you might have seen this method come up before is getting the squared
magnitude.
Now let me just put SKU magnitude down here tank to fuel instead of magnitude.
What I'm going to do is go dot square magnitude like that.
Okay.
And then I'll talk about this in a moment after we've had a look at it.
So just save that switch back to unity and again, we will press play.
Okay.
So let's get a little bit closer to this tank.
So when I hit the spacebar, we can see our squared magnitude and our normal magnitude.
So the normal magnitude is ten.
The squared magnitude is around 100, which is ten times ten, or the square of ten.
Ten to the power of two gives you the square magnitude.
Why on earth do you need the square magnitude?
Well, the square magnitude is a much faster operation than magnitude, because magnitude requires you
to perform a square root operation, because that's what's in the formula.
But if we take that square root out, we end up with the squared magnitude.
Now, it's obviously a bigger value, but you're saving yourself that square root operation.
So in the case with our villager and the pigs before, when we might have said that the direction dot
magnitude is greater than two, or we could also have said direction squared magnitude is greater than
four in our code and it's actually a faster operation to perform.
So just keep that in mind, especially when you're doing something over and over again inside of an
update loop is that you can use square magnitude.
And if you look at a lot of the tools in unity and the code that's available for characters and it's
working at distance and thing, you'll see that square magnitude used over and over again.
And that is why.