WEBVTT

00:03.990 --> 00:09.740
In the previous lecture, we looked at calculating distance using Pythagoras theorem.

00:09.750 --> 00:15.780
We're going to now look at some other ways that unity can do it with its built in methods.

00:15.810 --> 00:21.450
Okay, so let's go back into our code so we can add to it.

00:21.450 --> 00:25.560
So this is our calculate distance and this is Pythagoras.

00:25.590 --> 00:31.950
Now underneath here we'll create another float and we'll call it U Distance.

00:32.760 --> 00:35.450
So this is Unity's distance calculation.

00:35.460 --> 00:36.900
Now, unity is distance.

00:36.900 --> 00:43.500
Calculation can take two positions in space and work out the difference between them.

00:43.530 --> 00:48.810
Okay, so we're actually doing this with the fuel and also with our tank.

00:48.810 --> 00:58.830
So in unity we can go vector three distance because it's actually in the vector three class and then

00:58.830 --> 01:05.430
give it to position values which are also stored as vectors because it's an X, Y and z value.

01:05.430 --> 01:16.230
So we can go fuel dot transform dot position and transform position.

01:16.710 --> 01:21.780
It doesn't matter which way around you have these because you're working at the distance, not a direction.

01:21.970 --> 01:23.850
Okay, so there we go.

01:23.850 --> 01:26.190
There's our u distance.

01:26.190 --> 01:31.760
And then we can add another debug log underneath here just so that we can see what it is.

01:31.770 --> 01:36.270
So we'll put you distance is equal to you.

01:36.780 --> 01:39.630
Distance like that in the calculations.

01:39.630 --> 01:48.840
Okay, so we'll save this and we'll go back into Unity Press play and we'll see how accurate our two

01:48.840 --> 01:50.490
calculations are.

01:50.700 --> 01:54.780
So with play pressed, we're going to.

01:55.670 --> 01:58.040
Press space to start with.

01:58.070 --> 02:01.360
Now let's have a look at what we're doing down here.

02:01.370 --> 02:05.240
We've got 49, which is our distance we've calculated with Pythagoras.

02:05.240 --> 02:09.350
And then we have our unity distance, which says 53.

02:09.500 --> 02:12.200
Why are these different?

02:12.230 --> 02:16.160
You probably were taking notice as I was typing that out.

02:16.160 --> 02:18.410
Let me find that code for you.

02:18.950 --> 02:24.020
Remember that this distance here is only in two dimensions X and Z.

02:24.020 --> 02:30.620
This distance here is calculating the distance in three dimensions, essentially, because you've got

02:30.620 --> 02:34.520
the three dimension of that position and the three dimension of that position.

02:34.520 --> 02:42.950
So if we go back into unity and have a look at our fuel, what we're quite interested in here is the

02:42.950 --> 02:50.240
Y value of the fuel because it's now being included in the calculation of Unity's distance.

02:50.240 --> 02:54.020
So the fuel is actually, in my case, 9.8.

02:54.020 --> 02:56.390
And if you haven't moved it, it should still be there.

02:56.390 --> 03:03.590
The tank, if we look at that, the tank's position, look at its Y position is -11.3.

03:03.680 --> 03:11.510
So it's giving you different values that it's calculating its distance based on.

03:12.020 --> 03:19.730
If you really don't care about the heights of these things, of course you could set them to the same

03:19.730 --> 03:23.690
thing or you could actually force it in the code.

03:23.690 --> 03:25.070
Let me show you how to do that.

03:25.070 --> 03:26.630
So stop playing.

03:27.400 --> 03:38.570
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

03:38.570 --> 03:43.880
actually say instead vector three and this will be our fuel.

03:43.910 --> 03:57.590
Pause equals new vector three and we can set this to field transformed position x zero for the Y and

03:57.590 --> 04:09.500
then fuel dot transform dot position dot z like that and we can do a vector three for our tank.

04:09.500 --> 04:14.480
Pause equals pretty much the same thing, so we'll just grab this.

04:15.310 --> 04:21.360
And put it down here and just change this to transform and transform.

04:21.370 --> 04:25.840
So now these two positions have zero in the Y.

04:26.020 --> 04:32.110
They will not affect the location of your tank game object or the fuel object.

04:32.110 --> 04:35.920
You just grab the values out of them and using them by setting the Y to zero.

04:35.950 --> 04:41.350
This is completely legitimate and sometimes you really don't care what the height is.

04:41.350 --> 04:46.030
You're more interested in the horizontal distance between things and that's how you would do it.

04:46.180 --> 04:55.990
Okay, so now in working out the you distance down here, it's actually going to be fuel pause and tank

04:56.350 --> 04:58.480
pause like that.

04:58.480 --> 05:05.050
Okay, so save this and we'll go and try it out and see if that fixes up our calculations.

05:05.380 --> 05:14.440
So let's now press play and we'll hit the old space bar immediately and let's have a look at our calculations

05:14.440 --> 05:18.430
much, much better and far more accurate.

05:18.610 --> 05:25.660
And then, of course, if I come and move my tank around, it will recalculate both of them to be in

05:25.660 --> 05:26.710
the same spot.

05:26.740 --> 05:27.310
Great.

05:27.310 --> 05:28.330
That works well.

05:28.330 --> 05:33.130
So that's finding the distance between two points.

05:33.520 --> 05:41.410
If we calculate the vector between this point and this point, we can also then find out the length

05:41.410 --> 05:45.160
of the vector, which is the distance between these two points.

05:45.160 --> 05:52.690
Just like we used magnitude before when we had a little villager and the pigs chasing him around.

05:52.690 --> 05:56.380
So let me show you how we would do that.

05:56.620 --> 06:03.670
Back in the code again, we're going to calculate another vector, this case.

06:03.970 --> 06:14.620
So vector three and we will use these two positions to do the vector between, okay, because we know

06:14.620 --> 06:16.840
that they've zeroed on the Y.

06:16.990 --> 06:30.670
So this will be a tank to fuel and that's going to equal the fuel pause minus the tank, pause like

06:30.670 --> 06:31.120
that.

06:31.360 --> 06:33.550
Then we just get the magnitude of this.

06:33.850 --> 06:40.840
So let's now do debug log and we'll go vector.

06:40.840 --> 06:52.600
So like v magnitude and in here we'll put tank to fuel dot magnitude like that, which is what we used

06:52.600 --> 06:53.830
previously.

06:53.830 --> 06:54.280
Okay.

06:54.280 --> 06:58.060
So saving, let's go back into unity.

06:58.060 --> 07:02.920
We're going to press play and have a look at our calculations.

07:04.150 --> 07:05.860
So we press play.

07:06.460 --> 07:13.930
And then I hit the spacebar and we get three values that are all exactly the same, but we've got different

07:13.930 --> 07:16.960
ways of actually calculating them.

07:17.740 --> 07:18.550
Great.

07:18.610 --> 07:20.590
So that's how you can work out the distance.

07:20.590 --> 07:23.050
And we've already seen magnitude before.

07:23.260 --> 07:33.220
Before we go, one thing I'd like to point out with this tank to fuel, let's also grab this line and

07:33.220 --> 07:39.340
I want to put it down here because you might have seen this method come up before is getting the squared

07:39.340 --> 07:40.000
magnitude.

07:40.000 --> 07:46.180
Now let me just put SKU magnitude down here tank to fuel instead of magnitude.

07:46.180 --> 07:51.100
What I'm going to do is go dot square magnitude like that.

07:51.100 --> 07:51.520
Okay.

07:51.520 --> 07:54.670
And then I'll talk about this in a moment after we've had a look at it.

07:54.670 --> 08:03.070
So just save that switch back to unity and again, we will press play.

08:03.190 --> 08:03.700
Okay.

08:03.700 --> 08:07.600
So let's get a little bit closer to this tank.

08:08.550 --> 08:15.230
So when I hit the spacebar, we can see our squared magnitude and our normal magnitude.

08:15.240 --> 08:17.610
So the normal magnitude is ten.

08:17.640 --> 08:24.660
The squared magnitude is around 100, which is ten times ten, or the square of ten.

08:24.660 --> 08:27.720
Ten to the power of two gives you the square magnitude.

08:27.960 --> 08:32.100
Why on earth do you need the square magnitude?

08:32.370 --> 08:40.350
Well, the square magnitude is a much faster operation than magnitude, because magnitude requires you

08:40.350 --> 08:46.110
to perform a square root operation, because that's what's in the formula.

08:46.110 --> 08:50.520
But if we take that square root out, we end up with the squared magnitude.

08:50.550 --> 08:57.570
Now, it's obviously a bigger value, but you're saving yourself that square root operation.

08:57.930 --> 09:05.820
So in the case with our villager and the pigs before, when we might have said that the direction dot

09:05.880 --> 09:15.690
magnitude is greater than two, or we could also have said direction squared magnitude is greater than

09:15.690 --> 09:21.480
four in our code and it's actually a faster operation to perform.

09:21.570 --> 09:26.370
So just keep that in mind, especially when you're doing something over and over again inside of an

09:26.370 --> 09:29.580
update loop is that you can use square magnitude.

09:29.580 --> 09:35.550
And if you look at a lot of the tools in unity and the code that's available for characters and it's

09:35.550 --> 09:40.650
working at distance and thing, you'll see that square magnitude used over and over again.

09:40.650 --> 09:42.270
And that is why.
