WEBVTT

00:00.000 --> 00:00.390
Hello guys.

00:00.390 --> 00:01.290
Welcome to the video.

00:01.290 --> 00:04.530
In this one we're going to make another cool trap a really nice one.

00:04.530 --> 00:08.310
And we're going to do the soul the spinning soul trap.

00:08.310 --> 00:11.790
Let's take this trap spike and place it into the prefab folder.

00:13.220 --> 00:15.020
So we have it here saved.

00:15.020 --> 00:20.600
And I'm just going to take the trap from here and maybe, you know, place it somewhere here.

00:20.600 --> 00:21.920
I don't need it right now.

00:22.460 --> 00:24.920
Now let's go to graphics.

00:25.620 --> 00:26.460
Traps.

00:26.460 --> 00:31.260
So let's take all of the sprites and make pixel per unit 16.

00:31.440 --> 00:32.580
I'm going to hit apply.

00:32.610 --> 00:34.260
I'm going to take the.

00:35.600 --> 00:36.830
Uh, off sprayed.

00:36.860 --> 00:38.930
Just drag it to the scene, like so.

00:40.230 --> 00:43.170
We're going to change the name to trap.

00:43.470 --> 00:48.900
So let's change layer to ground and let's place it.

00:48.900 --> 00:51.120
No, sorry, not ground traps.

00:51.120 --> 00:53.340
And let's place it halfway into the ground.

00:53.370 --> 00:54.900
I really like how it looks.

00:54.900 --> 00:56.160
Really nice one.

00:56.430 --> 01:01.500
Um, now let's go and make animator for this one.

01:01.500 --> 01:02.790
Animations.

01:03.030 --> 01:07.170
We're going to make new animator controller trap.

01:07.470 --> 01:08.820
So AC.

01:10.930 --> 01:12.820
You're going to place it on the object.

01:13.060 --> 01:20.260
Also, we want to give it a circle collider you're going to make it is trigger and we're going to give

01:20.260 --> 01:22.120
it a damage trigger.

01:22.870 --> 01:24.160
Very cool.

01:24.160 --> 01:28.510
Now let's go to Animation Create new clip.

01:28.840 --> 01:32.860
I'm going to name it as maybe Trap so idle.

01:33.130 --> 01:35.560
And let's do trap so active first.

01:36.230 --> 01:38.030
On the graphics over here.

01:38.030 --> 01:39.110
We need trap.

01:39.350 --> 01:40.250
So.

01:40.250 --> 01:44.090
And let's take on all of the sprites and place them here.

01:44.120 --> 01:45.230
Very cool.

01:45.320 --> 01:47.600
Now we're going to make sample rate 20.

01:48.380 --> 01:52.220
And on the animator it's going to be the animation by default.

01:52.370 --> 01:54.290
Now let's create another clip.

01:55.120 --> 01:57.190
Trap soul idol.

01:57.910 --> 02:00.040
And we're going to make a boolean.

02:00.520 --> 02:02.950
Wait, we just need a sprite over here on the trap.

02:02.950 --> 02:03.760
So idle.

02:05.650 --> 02:13.810
Now we're going to make a boolean active and we're going to transfer to idle if active is false.

02:15.430 --> 02:16.000
Uncheck.

02:16.030 --> 02:16.300
Exit.

02:16.330 --> 02:16.810
Time.

02:16.810 --> 02:18.250
Duration zero.

02:19.140 --> 02:20.670
Then go back.

02:21.780 --> 02:22.800
Exact time unchecked.

02:22.800 --> 02:26.520
Transmission duration zero and condition would be active.

02:26.520 --> 02:27.120
True.

02:27.150 --> 02:27.990
Okay.

02:28.230 --> 02:29.250
Very nice.

02:29.280 --> 02:33.030
Now let's go back and just try to hit the drop.

02:33.030 --> 02:34.560
I want to see if it's working.

02:40.470 --> 02:42.180
Working except the animation.

02:42.180 --> 02:48.240
I didn't see the animation for some reason, or I guess because this boolean here is false by default.

02:48.240 --> 02:49.860
It's okay, we're going to fix it.

02:50.160 --> 02:51.540
Now let's just click it here.

02:51.540 --> 02:52.050
For now.

02:52.050 --> 02:54.780
It's going to be true and it's going to work.

02:56.720 --> 02:57.590
Very well.

02:57.590 --> 03:00.950
We have a trap that is spinning and it is very dangerous.

03:00.980 --> 03:03.620
Now let's make it move.

03:06.330 --> 03:07.350
Uh, how are we going to do that?

03:07.350 --> 03:08.670
We're going to need a script.

03:10.570 --> 03:12.880
C sharp script trap.

03:12.880 --> 03:13.750
So.

03:16.250 --> 03:18.110
I'm going to open the script.

03:19.580 --> 03:22.490
And inside of this script, we need a couple of things.

03:22.490 --> 03:25.130
First of all, we want to have a movement speed.

03:25.130 --> 03:27.950
So I'm going to do private plot move speed.

03:28.800 --> 03:32.430
Then we need to know where this object is going to move.

03:32.430 --> 03:35.100
So basically we need a waypoint.

03:37.340 --> 03:39.980
And not just one way point, but a bunch of them.

03:39.980 --> 03:45.800
Because of that, I'm going to make private transform array, and I've shown the arrays before, basically

03:45.800 --> 03:51.320
can store a bunch of variables under the one, and then you can get access to the variable with an index,

03:52.160 --> 03:54.980
uh, array of way point.

03:54.980 --> 03:55.730
Okay.

03:55.730 --> 03:57.590
And we need the index.

03:57.590 --> 04:00.560
I'm going to make it public for now so we can see how does it work.

04:00.560 --> 04:02.480
But later on I'm just going to hide it.

04:02.480 --> 04:04.160
So let's do public int.

04:04.970 --> 04:08.720
Waypoint index and I'm going to make it equals to one by default.

04:08.930 --> 04:11.750
Also, I guess speed could be three by default.

04:11.750 --> 04:12.950
Very cool.

04:13.010 --> 04:17.510
Now let's go and open start method.

04:17.510 --> 04:22.850
And in the start I want to set transform position of the object of the trap.

04:22.850 --> 04:25.250
So equals to waypoint.

04:26.310 --> 04:29.850
With an index of zero dot position.

04:29.850 --> 04:34.620
Because of that, the object will snap to the first position in the beginning of the game.

04:34.620 --> 04:36.270
Now we need an update method.

04:36.690 --> 04:42.450
Update and in the update method, we want to move objects from one point to another.

04:42.450 --> 04:45.690
Before we did that with the rigid bodies velocity.

04:45.690 --> 04:50.520
But now we can use transform position and we can change transform position with a time.

04:50.520 --> 04:54.450
Let's do transform position equals to vector two.

04:54.480 --> 05:02.130
Move towards from current position to the target position, and target position is waypoint with the

05:02.130 --> 05:03.570
index we have over here.

05:03.570 --> 05:08.820
So waypoint index and then dot position.

05:08.820 --> 05:10.140
And then over here.

05:10.140 --> 05:13.320
Basically we need to set the speed of the movement.

05:13.980 --> 05:18.270
But we cannot just use move speed because it will move super fast.

05:18.270 --> 05:18.960
We don't need that.

05:18.960 --> 05:23.670
We need frame independent movement to have frame rate independent movement.

05:23.670 --> 05:27.840
We can do move speed multiplied by time dot delta time.

05:27.840 --> 05:33.330
If you see time dot delta time for the first time, it basically returns interval and seconds from the

05:33.330 --> 05:35.010
last frame to the current one.

05:35.010 --> 05:41.700
If that sounds confusing, I'm just going to say that if you need some constant smooth movement and

05:41.700 --> 05:46.980
it should be frame rate independent, you just multiply value by time dot delta time and time.

05:46.980 --> 05:49.530
The delta time always change in itself.

05:49.530 --> 05:52.140
If you have low pH this while you're going to be bigger.

05:52.140 --> 05:56.520
If you have high pH this value, you're going to be smaller and it's always adjusting itself.

05:56.520 --> 06:00.480
So you basically have like sort of time in seconds okay.

06:01.750 --> 06:07.570
If all of that is confusing, just know that we're using time delta time to make it smooth and frame

06:07.570 --> 06:08.380
rate independent.

06:08.410 --> 06:13.510
Now that we move in object, actually, let's go and see how do we move the object.

06:13.540 --> 06:15.340
We want to know if it's working.

06:17.400 --> 06:18.360
On the trapsoul.

06:18.660 --> 06:24.390
We need to put the script and over here we have waypoints which are null at the moment.

06:24.390 --> 06:26.670
And waypoint index is equals to one.

06:26.670 --> 06:28.620
Now let's make a couple of objects.

06:28.620 --> 06:31.470
I'm going to make way point.

06:31.860 --> 06:34.740
I'm going to give it a gizmo so I can see it.

06:34.740 --> 06:36.420
And I'm going to duplicate it.

06:36.570 --> 06:41.070
So the first one going to be here, the second one going to be here.

06:41.100 --> 06:45.540
And we're going to open the Trapsoul and drag them into this array.

06:45.660 --> 06:50.040
What something important here is that we do not count, uh, elements from one.

06:50.040 --> 06:51.360
We count them from zero.

06:51.360 --> 06:57.240
So if you want to get first waypoint, you would have to type waypoint with the index zero.

06:57.240 --> 07:01.380
If you want to get second one you would have to type waypoint with the index one.

07:01.380 --> 07:03.960
So now we have waypoint zero and waypoint one.

07:03.960 --> 07:08.940
And according to the script we have it's going to place itself on the waypoint zero.

07:08.940 --> 07:15.090
And then it's going to move to the waypoint one because waypoint index is equals to one okay.

07:15.090 --> 07:16.110
Let's go.

07:18.780 --> 07:21.180
And we can see it is moving.

07:21.180 --> 07:22.050
Very cool.

07:22.050 --> 07:24.840
Now we have a goal to make it move back.

07:25.110 --> 07:26.610
That is very simple to do.

07:29.210 --> 07:30.770
In the update over here.

07:30.770 --> 07:36.950
I want to check if vector2 the distance between Transform.position and the waypoint.

07:36.950 --> 07:38.090
We currently have.

07:38.090 --> 07:40.730
Waypoint with a waypoint index.

07:42.190 --> 07:46.480
Dot position is less than 0.1 F means we are close enough.

07:46.480 --> 07:48.310
We can change waypoint index.

07:48.310 --> 07:50.140
So we're going to move to another waypoint.

07:50.140 --> 07:54.670
We can change it simply by simply by increasing the value.

07:54.670 --> 07:57.190
Let's just type waypoint index plus plus.

07:57.190 --> 08:00.460
And this will do it plus one every time we reach a new waypoint.

08:00.460 --> 08:07.030
And to secure ourselves, we want to check if waypoint index is not bigger than the length of the waypoints

08:07.030 --> 08:07.660
we have.

08:07.660 --> 08:13.030
Simply type here waypoint index bigger or equals to waypoint dot length.

08:13.030 --> 08:18.220
And if it is then we're going to do waypoint index equals to zero.

08:18.220 --> 08:20.950
So we're going to increase value by one every time.

08:20.950 --> 08:25.330
And if at some point it's going to be bigger or equals to waypoint dot length, then we're going to

08:25.330 --> 08:26.650
make it zero.

08:26.650 --> 08:28.060
So it's going to go back.

08:28.060 --> 08:31.540
Let's go to unity now and see how does this work.

08:37.170 --> 08:41.460
And we can see this cycle in between waypoints if you extend waypoints.

08:42.690 --> 08:43.830
Like that.

08:44.160 --> 08:46.800
It's going to fly between waypoints like that.

08:50.130 --> 08:52.410
Oh, I forgot to add it to the elements.

08:52.650 --> 08:55.410
Let's select the trapsoul and drag it here.

08:58.450 --> 09:00.760
And it's going to cycle between three points.

09:01.980 --> 09:04.020
There is something I want to do to improve it.

09:04.020 --> 09:07.140
As of now, you can see it moves in circle like that.

09:07.140 --> 09:12.930
What I want to do is to make it move, uh, sort of backwards when we need it to move back.

09:12.930 --> 09:18.960
So it's going to move here, then here, then back here, and again here, here, then back here, back

09:18.960 --> 09:20.160
here and like that.

09:20.160 --> 09:22.200
But we're going to do that in the next video.

09:22.200 --> 09:22.920
In this one.

09:22.920 --> 09:25.590
Let's just slightly improve it and move on okay.

09:25.590 --> 09:28.860
So I'm going to delete the waypoint over here.

09:30.000 --> 09:36.420
I'm going to have only two, and I want to add a bit of a delay to the movement of the trap.

09:36.720 --> 09:38.220
Let's go to the trap.

09:38.220 --> 09:45.120
So let's make Privatebool can move, and we're going to make it equal to true by default, because we

09:45.120 --> 09:46.650
want it to move from the start.

09:46.890 --> 09:54.060
Then I want to make a coroutine private I enumerator stop movement.

09:55.130 --> 09:57.230
You're going to pass float delay.

09:58.130 --> 10:01.670
And over here we're going to do can move equals to false.

10:02.060 --> 10:09.140
Then we're going to do yield return new wait for a seconds of a delay and then can move equals to true.

10:09.410 --> 10:11.540
And I think.

10:12.120 --> 10:14.640
You can add variables for the delay.

10:14.640 --> 10:16.770
Let's do serialized field private float.

10:16.770 --> 10:17.580
Cool down.

10:17.580 --> 10:19.770
I'm going to make it equals to one by default.

10:19.800 --> 10:20.850
Very nice.

10:20.850 --> 10:28.200
Now we need to start this coroutine every time we set in index to zero I guess, because for now we

10:28.200 --> 10:29.550
have only two waypoints.

10:29.550 --> 10:30.780
And please pay attention.

10:30.780 --> 10:35.460
This system is developed for the trap that works with two way points.

10:35.460 --> 10:38.850
If you want to have more waypoints, we're going to do that in the next video.

10:40.640 --> 10:46.640
So over here I'm going to do start coroutine stop movement with a cooldown.

10:46.940 --> 10:47.690
Okay.

10:48.690 --> 10:51.390
And in the update over here if you can move.

10:52.640 --> 10:56.270
Equals to false, then return.

10:56.360 --> 11:00.530
And we want to update boolean in the animator.

11:00.530 --> 11:04.910
So I'm going to make private Animator.

11:06.490 --> 11:07.240
Anim.

11:08.020 --> 11:10.270
Then we're going to get it in the awake.

11:10.300 --> 11:11.110
Awake.

11:11.950 --> 11:15.490
Awake enum equals to get component of animator.

11:15.490 --> 11:18.790
And then over here enum set bool.

11:19.820 --> 11:23.600
Of active two can move boolean.

11:23.600 --> 11:24.830
Very nice.

11:25.310 --> 11:27.320
And there is something else I want to do.

11:27.320 --> 11:28.580
I want to check.

11:29.770 --> 11:31.180
The damage direction.

11:31.180 --> 11:34.840
This is what we're going to do in this video, and then we're going to jump to the next one real quick.

11:35.460 --> 11:37.050
So as of now.

11:40.830 --> 11:42.720
Uh, active does not exist.

11:42.720 --> 11:43.740
What do you mean?

11:44.540 --> 11:45.860
Animator.

11:46.100 --> 11:47.810
Oh, I misspelled it.

11:47.810 --> 11:49.460
Okay, it should be active.

11:50.590 --> 11:51.640
Let's go.

11:54.530 --> 11:55.550
Moves there.

11:55.700 --> 11:56.540
Stops.

11:56.540 --> 11:57.740
Moves back.

11:57.830 --> 11:59.870
We also need to flip the sprite.

12:00.410 --> 12:02.660
We can do it right now, I think.

12:02.990 --> 12:05.000
Then let's work on a damage direction.

12:05.000 --> 12:06.860
Then we're going to jump to the next video.

12:07.460 --> 12:11.240
So to flip the sprite we can get access to the component of a sprite renderer.

12:11.240 --> 12:17.060
And we can check this flip x true false true false every time we need to do it.

12:17.240 --> 12:18.590
Simple as that.

12:18.620 --> 12:20.240
Let's go to the trap.

12:20.240 --> 12:23.240
So let's do private.

12:24.270 --> 12:27.390
Uh, sprite renderer SR.

12:27.920 --> 12:32.180
Then in that week we're going to do SR equals to get component.

12:32.180 --> 12:34.160
And every time we.

12:35.430 --> 12:37.200
Return movement to the object.

12:37.200 --> 12:39.930
We're going to do s r flip x.

12:40.970 --> 12:43.400
Equals to naught as our flip X.

12:43.400 --> 12:46.850
Because it's a boolean, it can be switched to an opposite value.

12:46.970 --> 12:51.770
The very same thing we did on the player when we changed facing the right boolean.

12:52.420 --> 12:53.230
Okay.

12:54.480 --> 12:55.680
Now let's go.

12:55.680 --> 12:57.300
And I'm going to show you something.

12:57.300 --> 13:02.880
At the moment, the knockback of a player works accordingly to the facing direction we have.

13:02.880 --> 13:09.180
So even if I stand over here and look to the left, I'm going to be knocked to the right.

13:09.810 --> 13:11.160
Same goes here.

13:11.160 --> 13:16.200
And I want to change knockback direction according to the source of the damage we have.

13:16.200 --> 13:17.340
How are we going to do that?

13:17.340 --> 13:21.570
When we call a knockback we're going to pass position.

13:22.390 --> 13:24.760
Of the source damage to the player.

13:24.760 --> 13:29.050
When we call a knockback, then we're going to see if trap.

13:29.050 --> 13:37.090
So X position is bigger than player's X position means the source damage is from the right and we need

13:37.090 --> 13:38.530
to be knocked to the left.

13:38.530 --> 13:41.110
We need to make knockback direction minus one.

13:41.110 --> 13:48.580
If an opposite thing is happening, an x position of the player is bigger than x position of the.

13:49.770 --> 13:55.200
The source of the damage, then we need to be knocked to the right and knockback direction should be

13:55.200 --> 13:56.460
equals to one.

13:57.240 --> 14:00.840
So we're going to go to the player.

14:01.700 --> 14:02.720
Over here.

14:05.750 --> 14:07.070
We're gonna pass.

14:07.070 --> 14:08.030
Float.

14:08.910 --> 14:12.450
Source damage X position.

14:12.900 --> 14:15.660
Then over here we're going to make float.

14:16.520 --> 14:19.670
A knock back direction equals to one.

14:19.670 --> 14:22.730
So it's going to be one every time we call in a method.

14:22.820 --> 14:25.670
And then we're going to check if.

14:26.600 --> 14:28.400
Transform position.

14:28.990 --> 14:32.380
Dot x of the player is less.

14:33.590 --> 14:35.870
Then source damage exposition.

14:35.870 --> 14:41.870
Then it means we are on the left side from the source of the damage, and we need to be knocked to the

14:41.870 --> 14:42.500
left.

14:42.500 --> 14:47.510
And for that we're going to do knockback direction equals to minus one okay.

14:49.090 --> 14:52.780
And now we can just use knockback direction over here.

14:54.160 --> 14:55.240
Very cool.

14:55.360 --> 15:01.300
Okay, now let's save what we have here and let's go to the damage trigger over here.

15:02.050 --> 15:07.960
And over here we need to pass X position and we're just going to pass transform position dot x.

15:08.170 --> 15:09.220
Simple as that.

15:09.250 --> 15:12.610
Now we can go back to unity and see the difference.

15:13.270 --> 15:14.500
I can move into the trap.

15:14.500 --> 15:16.450
I'm going to be knocked same as before.

15:16.960 --> 15:22.840
If I go to the trap and I faced another direction, still, I'm going to be knocked to the left and

15:22.840 --> 15:23.590
to the right.

15:24.310 --> 15:25.450
Which is very good, right?

15:25.450 --> 15:27.580
It's more how it's supposed to be, I think.

15:27.580 --> 15:28.120
Yeah.

15:28.300 --> 15:29.140
Very well.

15:29.140 --> 15:31.960
Now, there is something I want to tell you about these waypoints.

15:31.960 --> 15:36.070
You're going to make it better, but as of now, it should be just like so in the hierarchy.

15:36.070 --> 15:40.300
And in the next video, I'm going to show you what to do with it and how to make it cleaner, how to

15:40.300 --> 15:42.820
not clutter up the hierarchy we have at the moment.

15:42.820 --> 15:44.470
But that's going to be in the next video.

15:44.470 --> 15:45.160
I'll see you there.
