WEBVTT

00:07.050 --> 00:15.150
In the previous lecture, we added in the ability to drive the red tank around and the turret here would

00:15.150 --> 00:21.990
actually move up and down based on where it calculates it would need to be in order to shoot the shell,

00:22.020 --> 00:26.100
given the shells, velocity or speed.

00:26.310 --> 00:32.280
But as you can see, our shells don't have a velocity or speed at the moment, so we need to put those

00:32.280 --> 00:34.620
in to complete this exercise.

00:34.650 --> 00:36.600
Let's go into fire shell.

00:36.720 --> 00:41.020
The shells are being created up here in Create Bullet.

00:41.040 --> 00:48.960
Now, before the shells had movement code on them in the shell script, we've replaced it with the shell

00:48.960 --> 00:51.680
script, which has no movement on it whatsoever.

00:51.690 --> 00:58.500
We want to rely on part of the physics system and unity to actually push our shells through the air

00:58.500 --> 00:59.100
for us.

00:59.100 --> 01:07.920
So I'm going to create a game object here called Shell that actually grabs hold of the shell being created.

01:07.920 --> 01:16.350
Then once we've got it, we're actually going to set its velocity of its rigid body to the speed that

01:16.350 --> 01:18.290
we're wanting to set it to.

01:18.300 --> 01:31.950
So we're going to say in here, shell dot get component, rigid body dot velocity equals speed multiplied

01:31.950 --> 01:34.410
by a direction to make it into a velocity.

01:34.410 --> 01:39.300
And in this case, the direction will be the way that that turret base is rotated.

01:39.300 --> 01:43.560
So turret base dot forward.

01:44.250 --> 01:51.180
So that'll be the forward vector or the z vector of that turret base to set that initial speed.

01:51.630 --> 01:52.110
Okay.

01:52.110 --> 01:57.210
So let's try this out, save it and we'll go back into unity.

01:58.920 --> 02:08.340
And once that's loaded, remember, we're still firing the green tank with the spacebar.

02:08.730 --> 02:11.940
Now notice that it's not perfectly accurate.

02:11.940 --> 02:16.800
The further I get away, the better the hit rate is.

02:19.580 --> 02:22.010
Might happen to get even further out here, I guess.

02:22.610 --> 02:27.830
Now, the reason that that is happening is because of the way that we're calculating the distance between

02:27.830 --> 02:34.160
the tanks, because we're calculating the distance of the tanks sort of from here to here, rather from

02:34.160 --> 02:36.080
the bullet to there.

02:36.080 --> 02:41.630
So what she can do is put a little bit of a tweak factor in there if you can't get the bullet starting

02:41.630 --> 02:47.870
from the exact position of the tank and if we come back into the code, we can come down into calculate

02:47.870 --> 02:55.010
angle where x remember that is our distance between the two objects, which is the magnitude here.

02:55.100 --> 02:57.860
You can actually make that a little bit smaller.

02:57.860 --> 03:02.150
So put like a fudge factor in there of one and save that.

03:02.150 --> 03:10.580
Let's come back into unity and press play and have a look at the effect of that now.

03:11.570 --> 03:13.340
So we're actually hitting the tank.

03:14.930 --> 03:18.500
And I must say only just it almost just goes over the top of the tank.

03:21.270 --> 03:24.870
Now, remember, the first lot of bullets you saw just then didn't hit the tank because the tank was

03:24.870 --> 03:26.670
actually still turning, okay?

03:26.750 --> 03:28.560
Because it's got a sort of slow turn rate.

03:28.560 --> 03:33.840
So it might start shooting in the wrong direction before it's actually lined up.

03:33.870 --> 03:38.130
Now, what if we use the other angle?

03:38.130 --> 03:43.470
Remember, got two angles, got a low angle, and we have a high angle and we've chosen in this case

03:43.470 --> 03:48.960
for true let's put false in here, which will use the high angle that gets calculated.

03:48.960 --> 03:50.430
So let's save that.

03:50.820 --> 03:54.990
Go back into here and we'll have another go.

03:54.990 --> 04:00.510
So let's press play now the turret, look at that angle on that.

04:00.510 --> 04:07.350
That's a high angle and the bullets are going to go all the way up and all the way down and then hit

04:07.470 --> 04:08.580
our tank.

04:09.390 --> 04:12.900
Let me just move out a bit now.

04:12.900 --> 04:18.360
I can also get out of the road, which is a good thing, I guess, if you're playing against an eye

04:18.390 --> 04:19.200
character.

04:19.380 --> 04:27.750
Okay, now the bullets currently do not go in the direction that they are travelling and they should.

04:27.750 --> 04:29.730
So let's also fix that up.

04:29.730 --> 04:34.080
Now that's going to be in the eye shell code.

04:34.350 --> 04:42.510
The shell has that rigid body on it and it's the rigid body's velocity that we are setting back in fire

04:42.510 --> 04:52.050
shell here so we can grab that velocity and make the bullets body or I guess modelled body turn in that

04:52.050 --> 04:53.880
same direction that it's travelling.

04:53.880 --> 04:58.110
So back in here we're going to add in a rigid body

05:00.660 --> 05:06.180
bait and we're going to link that up to the actual rigid body on the bullet so we don't have to keep

05:06.180 --> 05:11.880
going, get component, get component, get component every basic loop that we do because it's very

05:11.880 --> 05:13.950
quite process a heavy to do that.

05:14.250 --> 05:22.110
Okay so we'll go this dot get component rigid body like that.

05:22.110 --> 05:31.740
And then in the update we will say that this dot transform dot forward equals r b.

05:34.000 --> 05:34.420
Don't.

05:36.730 --> 05:38.710
Velocity, so it'll point in that direction.

05:38.720 --> 05:46.600
Okay, so save that and we'll switch back into unity and press play.

05:47.710 --> 05:49.060
And now.

05:52.240 --> 05:56.410
You can see that the bullets go up and they'll turn around and when they come down, they're actually

05:56.410 --> 05:58.810
facing in the right direction.

06:01.640 --> 06:01.960
All right.

06:02.030 --> 06:07.520
To finish off this section on projectiles, we're going to have a little bit of fun with our tank.

06:07.520 --> 06:11.480
So what I've got here, you can see a continually shooting tank.

06:11.480 --> 06:17.570
Now, some of those bullets aren't particularly accurate because don't forget, as they're getting instantiated,

06:17.570 --> 06:21.150
they can collide with each other, but most of them seem to hit the target.

06:21.170 --> 06:25.670
What I'd like to do, though, is to program this so that if the tank gets out of range and there's

06:25.670 --> 06:31.190
no angle that can be calculated, that the little green tank will go after the red tank and actually

06:31.190 --> 06:32.030
move.

06:32.030 --> 06:36.830
So even though it's not hitting its mark there, it knows it's close enough that if it turns around

06:36.830 --> 06:38.030
correctly, it can.

06:38.030 --> 06:42.530
But then if I move all the way over here and it gets out of range, as far as distance goes, it will

06:42.530 --> 06:45.500
stop shooting and then it will come after us again.

06:46.010 --> 06:50.720
I can't get far enough away on the screen that you won't be able to see the red tank anymore from here.

06:50.960 --> 06:51.680
Oh, there we go.

06:51.710 --> 06:54.740
Okay, so once I come back close, he'll start shooting again, right?

06:54.740 --> 07:00.230
So let's go into our fire shell script and add the code in that will allow us to do this.

07:01.430 --> 07:05.300
First of all, we're going to add in a movement speed for our tank.

07:05.300 --> 07:10.820
So float move speed and we'll set that to say three meters per second.

07:11.000 --> 07:18.110
Now we need to test whether we've got an angle to determine whether we can move or whether we can fire.

07:18.110 --> 07:22.850
And that means that this rotate turret is going to have to return our angle here.

07:22.850 --> 07:31.760
So let's put this as a returning float question mark and then under here we will return angle.

07:32.000 --> 07:34.700
So this will come back into our update.

07:34.730 --> 07:41.030
Down in the update, we're going to create a float.

07:43.690 --> 07:45.790
Angle equals that.

07:45.790 --> 07:48.640
And then instead of get key down, we're going to go.

07:48.640 --> 07:55.870
If angle does not equal null, then we can create a bullet.

07:56.450 --> 07:58.540
Else we can move the tank.

07:58.540 --> 08:02.020
Now we can move the tank quite easily with a translate.

08:02.020 --> 08:05.830
So this dot transform, dot translate.

08:06.280 --> 08:09.790
Because remember, we're turning the tank to face the enemy anyway.

08:09.790 --> 08:12.100
So all we got to do is just give it a little bit of a push forward.

08:12.100 --> 08:21.250
So zero zero and in the Z direction, time dot delta time multiplied by our move speed that we've got

08:21.310 --> 08:22.120
there.

08:22.420 --> 08:23.710
Okay, great.

08:23.710 --> 08:28.150
So in fact, before I actually show you this in action because you've just seen it in action, I just

08:28.150 --> 08:30.400
want to change our speed up here.

08:30.400 --> 08:32.020
Let's turn that down to like one.

08:32.020 --> 08:37.240
So it's a bit slower to catch up, but five, so it's quicker to turn around.

08:37.240 --> 08:38.980
Okay, so save that.

08:38.980 --> 08:43.270
Let's go back into unity and try this out.

08:44.410 --> 08:51.550
So we'll press play and then just start driving my tank around.

08:52.090 --> 08:56.590
So now that I'm out of range, he'll take a long time to catch up to me.

08:56.590 --> 09:00.460
But notice he's still turning towards the red tank.

09:00.460 --> 09:06.250
Now, soon as I get back into range, even though he might not be rotated correctly, he's still trying

09:06.250 --> 09:11.410
to fire and eventually he will rotate around to the point where he can fire at me.

09:11.440 --> 09:12.580
Okay, great.

09:12.580 --> 09:12.910
Right.

09:12.910 --> 09:16.750
Well, that ends this section on projectiles, but there's so much you can do.

09:16.750 --> 09:22.840
And I hope this is sort of inspired you to have a look into the mathematics of this and a whole variety

09:22.840 --> 09:26.590
of other types of projectiles that you can produce.

09:26.590 --> 09:32.740
And now you should know how to put together a really simple little A.I. character that can fire at its

09:32.740 --> 09:33.520
enemy.
