1
00:00:03,500 --> 00:00:08,840
In this lecture, we're going to pick up where we left off last time and create our pathfinding script

2
00:00:08,840 --> 00:00:10,280
to get our enemies moving.

3
00:00:10,490 --> 00:00:13,970
So let's jump back into Unity and create our next script.

4
00:00:14,300 --> 00:00:18,680
Let's right click create a new C-sharp script called Pathfinder.

5
00:00:20,190 --> 00:00:27,660
And then over in our prefabs, let's attach this pathfinder to our enemy prefab so we can add our component

6
00:00:27,660 --> 00:00:30,060
and find our Pathfinder script.

7
00:00:30,240 --> 00:00:32,369
Let's open that Pathfinder script up.

8
00:00:33,250 --> 00:00:39,760
And what this script is going to do is set our enemy to the first waypoint in our list and then iteratively

9
00:00:39,760 --> 00:00:43,120
move it towards the next waypoint that it hasn't visited yet.

10
00:00:43,420 --> 00:00:48,370
Once it reaches the end of the path and there are no more waypoints in our list, we can go ahead and

11
00:00:48,370 --> 00:00:49,540
destroy our enemy.

12
00:00:49,930 --> 00:00:54,130
So let's clean up the comments above our start and update to begin with.

13
00:00:54,960 --> 00:00:59,760
For all variables we're going to need reference to a wave config script will object.

14
00:00:59,760 --> 00:01:07,110
So let's create a serialized field of type wave config S0 and call this the wave config.

15
00:01:08,070 --> 00:01:12,120
And we're also going to want to store the waypoints for our path.

16
00:01:12,120 --> 00:01:17,640
So let's create a list of transforms and we'll call it the waypoints.

17
00:01:18,330 --> 00:01:21,900
And this will be populated from our script object.

18
00:01:22,110 --> 00:01:25,560
Lastly, let's keep track of which waypoint we're currently on.

19
00:01:25,560 --> 00:01:31,110
So let's have an integer called Waypoint Index and we'll default that to zero.

20
00:01:31,110 --> 00:01:33,780
So it starts at the first waypoint in our list.

21
00:01:34,380 --> 00:01:42,080
In START, we want to populate our list of waypoints so we can say waypoint equals the wave config dot

22
00:01:42,120 --> 00:01:47,610
get waypoints and then we want to move our enemy to the first waypoint in the path.

23
00:01:47,940 --> 00:01:56,340
We can do that with transform dot position equals the waypoint at waypoint index dot position.

24
00:01:57,470 --> 00:02:02,570
Then in update every frame, we want to move closer to the next waypoint in the list.

25
00:02:02,870 --> 00:02:09,050
So in update, we're going to use a method that we're going to call follow path and then under update.

26
00:02:09,050 --> 00:02:10,970
Let's actually create this method.

27
00:02:13,140 --> 00:02:20,070
And in here we want to say that if the waypoint index is less than our waypoint account.

28
00:02:20,220 --> 00:02:26,520
So if we're not at the last waypoint just yet, then we want to move towards the next waypoint in the

29
00:02:26,520 --> 00:02:27,120
list.

30
00:02:27,600 --> 00:02:32,790
To do this, we're going to need to know where we are, where we're going, and how fast we're moving.

31
00:02:33,180 --> 00:02:36,450
Well, we know where we are with our transform position.

32
00:02:36,450 --> 00:02:41,340
So let's think about where we're going and create a vector to called target position.

33
00:02:42,660 --> 00:02:48,570
And we're going to set this equal to waypoint at the waypoint index dot position.

34
00:02:49,200 --> 00:02:51,390
Next, we want to know how fast we're going.

35
00:02:51,390 --> 00:02:54,840
So let's create a float called Delta.

36
00:02:55,110 --> 00:02:58,800
Because this is going to be the distance that we're going to be moving each frame.

37
00:02:59,460 --> 00:03:04,440
And we can get that from our wave config dot get move speed.

38
00:03:04,830 --> 00:03:09,420
And to make it frame rate independent, we're going to multiply it by time dot delta time.

39
00:03:10,430 --> 00:03:12,170
To actually move our enemy.

40
00:03:12,170 --> 00:03:18,920
We need to set its transform position for this frame and for this we're going to use a method called

41
00:03:18,920 --> 00:03:20,300
move towards.

42
00:03:21,110 --> 00:03:26,480
We can find this method if we type vector to move towards.

43
00:03:27,680 --> 00:03:31,050
And we can see from the tooltip here that it requires three parameters.

44
00:03:31,070 --> 00:03:36,560
It requires the current position, the target position, and the maximum distance delta.

45
00:03:36,650 --> 00:03:40,040
And it moves the current point towards the target.

46
00:03:40,310 --> 00:03:44,390
So our current position is our transform position.

47
00:03:44,690 --> 00:03:47,810
Our target is going to be our target position.

48
00:03:47,810 --> 00:03:53,660
So the position of our next waypoint and our maximum distance delta is going to be our delta value that

49
00:03:53,660 --> 00:03:54,830
we just calculated.

50
00:03:56,490 --> 00:04:01,290
Finally, once we reached the next waypoint in the path, we're going to need to increment our waypoint

51
00:04:01,290 --> 00:04:01,950
index.

52
00:04:01,950 --> 00:04:10,680
And we can do this by saying that if our transform position is equal to our target position, then we

53
00:04:10,680 --> 00:04:14,880
want to go ahead and take waypoint index and increment it by one.

54
00:04:15,580 --> 00:04:18,160
Now this is giving us a red squiggly error.

55
00:04:18,160 --> 00:04:23,260
And if we hover over it, it's saying that it's having trouble checking a vector three our position

56
00:04:23,260 --> 00:04:25,900
versus a vector to our target position.

57
00:04:25,900 --> 00:04:31,870
So let's fix that very quickly and we can just change our vector two here into a vector three for our

58
00:04:31,870 --> 00:04:32,770
target position.

59
00:04:32,890 --> 00:04:36,160
That removes our red squiggly and our script should now compile.

60
00:04:36,640 --> 00:04:40,510
But before we go ahead and test it, there's one more thing we need to add here.

61
00:04:40,540 --> 00:04:43,780
And that's what happens when we get to the end of our path.

62
00:04:44,110 --> 00:04:49,570
So our if statement will walk us through all of our waypoints, and then if we get to the end, we can

63
00:04:49,570 --> 00:04:54,040
say else destroy the game object.

64
00:04:54,220 --> 00:04:58,420
So that will remove our enemy from the game once it reaches the end of the path.

65
00:04:58,930 --> 00:05:02,290
Now let's go ahead and save our script and jump back into unity.

66
00:05:02,620 --> 00:05:08,380
We've already preformed our path so we can go ahead and delete that from our hierarchy and then on our

67
00:05:08,380 --> 00:05:10,930
enemy we need a wave config file.

68
00:05:11,140 --> 00:05:18,250
So in our waves and paths let's right click go up to create and we should have a wave config file here,

69
00:05:18,250 --> 00:05:19,930
which I've incorrectly spelt.

70
00:05:19,930 --> 00:05:21,670
So we'll fix that in just a moment.

71
00:05:21,940 --> 00:05:28,090
This creates our new wave config, which let's just call it wave zero for now and this needs a path

72
00:05:28,090 --> 00:05:32,770
prefab which we can drag in and an enemy movement speed which we can leave at five four.

73
00:05:32,770 --> 00:05:37,960
Now with our config file setup, let's again go and find out enemy prefab.

74
00:05:39,950 --> 00:05:42,050
And attach our wave config file.

75
00:05:43,010 --> 00:05:48,800
Now, if we go ahead and hit play, we can see that our enemy nicely moves between each waypoint in

76
00:05:48,800 --> 00:05:51,540
our path, and when it gets to the end, it gets destroyed.

77
00:05:51,560 --> 00:05:54,720
So congratulations on making your enemy move.

78
00:05:54,740 --> 00:06:00,320
And now, while I go ahead and fix my spelling mistake for my wave config file, I think it's time for

79
00:06:00,320 --> 00:06:01,280
your challenge.

80
00:06:01,610 --> 00:06:06,950
I want you to go ahead and create at least one new path for the enemies to follow.

81
00:06:07,220 --> 00:06:12,470
And then I want you to go ahead and create a new wave config file that uses your new path.

82
00:06:12,500 --> 00:06:15,080
So pause the video now and give that go.

83
00:06:20,350 --> 00:06:20,830
Okay.

84
00:06:20,830 --> 00:06:21,670
Welcome back.

85
00:06:21,700 --> 00:06:26,620
So while you've been away, I've gone ahead and created a new path, which is just a mirror image of

86
00:06:26,620 --> 00:06:28,390
the path I created earlier.

87
00:06:28,420 --> 00:06:34,840
And to create a new wave config file, let's go up and create a new wave config with the correct spelling

88
00:06:34,840 --> 00:06:35,480
this time.

89
00:06:35,500 --> 00:06:39,580
Let's call this wave one and I'm going to attach my new path.

90
00:06:39,970 --> 00:06:45,190
Now, if we head back into our scene, we can go ahead and duplicate our enemy for now.

91
00:06:46,440 --> 00:06:49,290
Attach the new wave that we've just created.

92
00:06:49,710 --> 00:06:54,180
And if we hit play, we should see two enemies following distinct paths.

93
00:06:54,420 --> 00:06:56,900
So everything seems to be working great.

94
00:06:56,910 --> 00:07:01,560
And in the next lecture we're going to expand on what we've done here and start creating those enemy

95
00:07:01,560 --> 00:07:02,100
waves.

96
00:07:02,100 --> 00:07:03,510
So I'll see you there.

