WEBVTT

00:00.020 --> 00:00.410
Hello guys.

00:00.410 --> 00:01.400
Welcome to the video.

00:01.400 --> 00:06.380
So in this one I want to show you how to upgrade the trapsoul, how to extend the waypoints and how

00:06.380 --> 00:08.060
to make it cycle between waypoints.

00:08.060 --> 00:13.640
But it will go from start to finish then from finish to start, sort of like ping pong, you know?

00:13.640 --> 00:14.150
Yeah.

00:14.480 --> 00:18.710
First let's make the system so it will cycle between multiple waypoints.

00:18.710 --> 00:23.030
Then I'm going to show you how to clean up the hierarchy and how to hide these waypoints.

00:23.030 --> 00:26.330
So they are not visible and they're not making a mess.

00:26.690 --> 00:29.240
Um I want to move.

00:30.070 --> 00:30.730
Trapsoul.

00:30.730 --> 00:31.750
Over here.

00:32.980 --> 00:35.290
I want to take first waypoint over here.

00:35.350 --> 00:37.390
Second waypoint over here.

00:37.390 --> 00:39.910
And then third waypoint over here.

00:40.360 --> 00:43.210
And let's assign third waypoint to the array.

00:43.210 --> 00:46.900
And now our goal is to make it move up right.

00:46.900 --> 00:48.580
And then go back to this point.

00:48.580 --> 00:50.260
And then go back to this point.

00:50.500 --> 00:50.920
Yeah.

00:50.920 --> 00:52.360
And this is how we do it.

00:52.360 --> 00:55.900
We go back to the script and we need to define the movement.

00:55.900 --> 00:59.500
Direction can be simply done by making public int.

00:59.500 --> 01:04.120
Let's do uh move direction equals to one by default.

01:04.120 --> 01:08.230
So one means forward minus one means backwards.

01:08.260 --> 01:11.560
Now we're going to go back to this if statement.

01:11.560 --> 01:15.790
And just to make it simple I'm going to erase everything because we're going to do it a bit differently

01:15.790 --> 01:26.230
when we moving forward I'm going to take waypoint index and do equals waypoint index plus movement direction.

01:26.230 --> 01:32.410
And because it is one at the moment, it's going to do plus one every time we moving forward when I

01:32.410 --> 01:35.290
say forward I mean from the first element to the last one.

01:35.290 --> 01:35.620
Yeah.

01:35.620 --> 01:37.570
Then I want to do something here.

01:37.570 --> 01:43.300
When I'm reaching the last element in the array, I want to change direction to minus one.

01:43.300 --> 01:44.650
This is how we're going to check it.

01:44.650 --> 01:55.390
If waypoint index is equals to waypoint dot length minus one then I'm going to make move direction equals.

01:55.390 --> 01:57.820
To move direction multiply by minus one.

01:57.820 --> 02:02.350
When we multiply in value by minus one, we basically make any negative or positive we setting it to

02:02.350 --> 02:02.980
an opposite.

02:02.980 --> 02:03.280
Yeah.

02:03.280 --> 02:05.200
If it was one it will become minus one.

02:05.200 --> 02:07.570
If it was minus one it will become one.

02:07.570 --> 02:11.080
Now why do I use waypoint dot length minus one?

02:11.080 --> 02:12.940
Its answer is very simple.

02:13.120 --> 02:18.850
To find the last element in the array, we need to check the length of the array and do minus one.

02:18.850 --> 02:24.970
As you can see, length of the array over here is three, but the last element in the array has index

02:24.970 --> 02:25.540
of two.

02:25.570 --> 02:29.020
So to get index of two we just need to get the length minus one.

02:29.020 --> 02:33.400
If it would have five elements, we would need the last element with the index of four.

02:33.400 --> 02:34.840
So we would do five minus one.

02:34.840 --> 02:37.270
So basically you just do length minus one.

02:37.270 --> 02:38.200
Simple as that.

02:38.200 --> 02:41.860
Now this will turn it back only when it reaches the last point.

02:41.860 --> 02:43.450
But still we want to see it.

02:43.450 --> 02:45.310
So let's leave it like that.

02:45.310 --> 02:46.390
Let's go back.

02:48.850 --> 02:52.030
And let's go and see it in the action.

02:57.230 --> 02:58.640
Goes there, goes there.

02:58.640 --> 03:00.200
Then it turns back.

03:00.200 --> 03:01.040
Very nice.

03:01.040 --> 03:04.130
Now we need to turn it back here from the start.

03:04.130 --> 03:12.110
We can do it simply by going to the script and type in or waypoint index is equals to zero.

03:12.110 --> 03:17.510
So if waypoint index is the first element then again we can change move direction and nothing else we

03:17.510 --> 03:17.750
need.

03:17.750 --> 03:19.610
For now let's save this.

03:19.610 --> 03:21.830
Let's go back and let's try this.

03:31.270 --> 03:34.750
Turn back and then turn it back again.

03:35.170 --> 03:36.310
Very cool.

03:36.310 --> 03:37.480
I love it so much.

03:44.520 --> 03:49.290
Now let's go and deal with the waypoints, because now we need to make a prefab.

03:49.290 --> 03:55.770
But if I take the prefab and if I drag Trapsaw over here, then this prefab will have no waypoints.

03:55.770 --> 03:58.830
And somehow we need to transfer them together with the object.

03:59.520 --> 04:03.300
Uh, one of the ways to do that would be make them as children of a trap.

04:03.300 --> 04:06.150
So let me do prefab unpack completely.

04:06.270 --> 04:08.250
Yeah, but there is an issue.

04:08.250 --> 04:11.790
When you move a parent object, the child object moves as well.

04:11.790 --> 04:18.480
And if we move uh trapsaw to the waypoint and waypoint moves, then we have endless movement, just

04:18.480 --> 04:20.250
like you see now on the screen.

04:20.490 --> 04:21.540
Not cool.

04:21.540 --> 04:29.880
One of the ways to fix it would be go to the script and unparent each element to make it like independent,

04:29.880 --> 04:32.850
because if it has no parent, it's not going to move together with the trap.

04:32.850 --> 04:34.800
So and then it's going to work fine.

04:34.800 --> 04:39.120
But that could clutter up the hierarchy, which I don't really want to have in the game.

04:39.120 --> 04:44.460
I want my hierarchy to be clean so I can see everything and find the objects I need when I'm making

04:44.460 --> 04:44.940
the game.

04:44.940 --> 04:48.360
So because of that, I want to make another array.

04:48.360 --> 04:57.180
Let's do serialize field, private Vector3 array and I'm going to call it um maybe waypoint.

04:58.330 --> 04:59.170
Position.

04:59.620 --> 04:59.980
Yeah.

04:59.980 --> 05:00.730
Just like that.

05:00.730 --> 05:07.240
So now this array has vector three and transform dot position is also vector three.

05:07.240 --> 05:08.620
Let me add three elements.

05:08.620 --> 05:10.270
You can see it has here.

05:10.270 --> 05:15.460
And if you select any of the waypoints this is the transform position over here.

05:15.460 --> 05:20.770
So somehow we need to transfer position of this object into this array.

05:21.040 --> 05:22.990
We can do it with the help of a for loop.

05:24.280 --> 05:28.930
If you never seen for loop before, I'm going to briefly explain how does this work okay.

05:29.170 --> 05:33.880
I'm going to type for click tap twice and it's going to create a for loop.

05:34.390 --> 05:37.120
The for loop does a certain amount of steps.

05:37.120 --> 05:41.170
And you can cycle through something with a for loop because you can define how many steps you need to

05:41.170 --> 05:41.590
do.

05:41.620 --> 05:46.930
Usually you type amount of steps over here and let me make I equals to zero.

05:46.960 --> 05:47.500
Okay.

05:47.650 --> 05:49.150
So this is a common for loop.

05:49.150 --> 05:51.670
It's going to do five steps to understand how it works.

05:51.670 --> 05:57.910
Let's do debug log I we're going to debug this I over here let's go to play mode.

05:57.910 --> 06:00.160
And let's just see the console.

06:05.150 --> 06:09.020
And over here we have 01234.

06:09.020 --> 06:14.330
So basically you see how I element was increased with each step of a for loop.

06:14.330 --> 06:19.610
And once I was not less than five for loop stopped.

06:19.610 --> 06:20.030
Right.

06:20.030 --> 06:21.080
This is how it is.

06:21.080 --> 06:23.870
You have I equals to zero I less than five.

06:23.870 --> 06:24.290
Yes.

06:24.290 --> 06:28.730
Then do something and do I plus plus then I equals to one.

06:28.730 --> 06:30.170
Is it still less than five.

06:30.170 --> 06:30.470
Yes.

06:30.470 --> 06:32.270
Then do something and do I plus plus.

06:32.270 --> 06:36.530
And when I becomes bigger or equals to five it is not working anymore.

06:36.800 --> 06:43.700
So we can use it in our advantage because instead of five we can set it to waypoint dot length.

06:43.700 --> 06:51.500
And now instead of debugging and I we can do way point position I which is going to be zero, 1234 and

06:51.500 --> 06:56.300
so on equals to waypoint I dot position.

06:56.300 --> 07:05.150
And we just need to go here and do uh waypoint position equals to new vector three.

07:05.150 --> 07:08.780
And in a square brackets we're going to do waypoint dot length.

07:09.320 --> 07:12.290
So basically this one creates a new array.

07:12.290 --> 07:17.090
Just add there with the length of waypoint dot length.

07:17.090 --> 07:24.980
So no matter how you change this waypoint dot length the waypoint position array will always uh have

07:24.980 --> 07:26.540
the same amount of elements.

07:26.780 --> 07:31.250
Now let's go to play mode and see the result in the inspector.

07:33.330 --> 07:35.730
And boom, we've got the positions.

07:35.730 --> 07:36.690
Very cool.

07:36.690 --> 07:37.200
Right?

07:37.230 --> 07:38.190
I love it so much.

07:38.190 --> 07:39.120
Very nice.

07:39.150 --> 07:43.920
Now that we have it working, let's hide this array because we don't need it anymore.

07:43.920 --> 07:45.300
We know how does it work.

07:45.690 --> 07:49.770
So I'm going to just make it private without serialize field.

07:50.130 --> 07:54.750
And then I want to take these lines of code and wrap it into a method.

07:54.750 --> 08:00.660
Let's do extract method update away points info.

08:00.690 --> 08:02.310
Yes, this is better.

08:02.400 --> 08:04.380
Now this will update waypoint info.

08:04.410 --> 08:10.200
The only thing we want to do here is to place this before we change transform position.

08:10.200 --> 08:17.610
It is important because if we change transform position before we update waypoints, then waypoints

08:17.610 --> 08:18.600
are going to be moved.

08:18.600 --> 08:21.930
You know, um, maybe let me show it to you.

08:21.930 --> 08:22.650
Why not?

08:24.200 --> 08:27.020
I'm going to save it without the updating waypoints.

08:27.050 --> 08:28.400
I'm going to go to play mode.

08:29.980 --> 08:36.280
And let's say my trapsoul is over here, but my first waypoint is over here.

08:36.310 --> 08:41.830
Okay, look, when I move this object to the first waypoint with the.

08:42.720 --> 08:45.060
With this line of code over here.

08:45.990 --> 08:51.150
It will snap there and all waypoints are going to be moved together with it.

08:51.780 --> 08:52.290
Okay.

08:52.290 --> 08:53.400
Here is the waypoint.

08:53.400 --> 08:55.320
Here is the waypoint and here is waypoint.

08:55.320 --> 08:56.640
So this is not what we need.

08:56.640 --> 08:59.340
So first we want to update the waypoints.

08:59.340 --> 09:01.500
Save them into vector three.

09:02.140 --> 09:03.010
Just like that.

09:03.010 --> 09:09.850
And then we can take transform position and make it equals to waypoint position zero.

09:10.150 --> 09:10.900
Nice.

09:10.900 --> 09:12.670
Now let's go to the update.

09:13.970 --> 09:15.500
And use it here as well.

09:15.500 --> 09:22.910
Instead of waypoint, I'm going to use waypoint position waypoint index and over here as well.

09:28.010 --> 09:28.760
I'm sorry.

09:29.290 --> 09:34.510
Waypoint position with a waypoint index less than 0.1 F.

09:34.540 --> 09:36.640
Okay okay okay okay.

09:36.640 --> 09:42.460
Now the only goal we have is to stop movement every time we reach in the final point or starting point.

09:42.460 --> 09:48.340
You can do it simply here by starting a coroutine of a stop movement with a cooldown.

09:48.340 --> 09:49.180
Nice.

09:49.180 --> 09:50.530
I'm going to save this.

09:51.320 --> 09:52.910
Going to go back to unity.

09:55.750 --> 09:57.880
Now this can be used as a prefab.

09:57.880 --> 09:59.980
We can go to prefab folder.

10:01.400 --> 10:02.600
Drag it here.

10:02.600 --> 10:04.100
And if you need a new trap.

10:04.100 --> 10:04.670
So.

10:06.440 --> 10:11.630
You can just drag it into the scene, take the waypoints, place them where you want them to be.

10:11.630 --> 10:13.100
For example here.

10:14.120 --> 10:16.790
Here and over here.

10:17.180 --> 10:19.970
And for this one, let's say I want.

10:21.240 --> 10:22.920
It to be here.

10:23.130 --> 10:23.880
We just.

10:25.490 --> 10:26.570
With just two of them.

10:29.410 --> 10:30.040
Okay.

10:31.040 --> 10:31.940
Should I?

10:31.940 --> 10:32.720
Yeah, actually.

10:32.720 --> 10:34.880
Let's do one more over here.

10:36.670 --> 10:37.720
Yeah, I think it's nice.

10:37.720 --> 10:41.320
Now let's go to play mode and see if something broke or not.

10:41.320 --> 10:42.700
I think it's going to be fine.

10:44.990 --> 10:47.660
And boom, we have two traps moving.

10:48.440 --> 10:49.640
With the waypoints.

10:52.820 --> 10:55.970
This one is not moving because I forgot to add it into the list.

11:00.600 --> 11:02.370
So don't forget to add it.

11:03.600 --> 11:05.340
Over here, it should be added.

11:14.920 --> 11:17.200
It just added here, like so.

11:17.470 --> 11:19.360
And that's good.

11:20.020 --> 11:20.800
Yeah.

11:20.800 --> 11:21.820
Nice.

11:22.800 --> 11:25.680
I think I just forgot something over here.

11:25.680 --> 11:33.030
Yeah, instead of waypoint lengths, let's check waypoint position dot length, because that's the waypoint

11:33.030 --> 11:33.750
we're using.

11:34.140 --> 11:34.800
All right.

11:34.800 --> 11:35.460
This is good.

11:35.460 --> 11:37.920
This could be improved to make it a bit more convenient.

11:37.920 --> 11:43.530
For example we could collect children of the trapsoul automatically so we don't have to reassign them

11:43.530 --> 11:44.430
in the inspector.

11:44.430 --> 11:45.930
But we don't really need it right now.

11:45.930 --> 11:47.160
So I guess that's it.

11:47.160 --> 11:49.080
And I'll see you in the next video.

11:49.080 --> 11:49.800
Bye.
