Hello guys.
Welcome to the video.
In this one we're going to make another cool trap a really nice one.
And we're going to do the soul the spinning soul trap.
Let's take this trap spike and place it into the prefab folder.
So we have it here saved.
And I'm just going to take the trap from here and maybe, you know, place it somewhere here.
I don't need it right now.
Now let's go to graphics.
Traps.
So let's take all of the sprites and make pixel per unit 16.
I'm going to hit apply.
I'm going to take the.
Uh, off sprayed.
Just drag it to the scene, like so.
We're going to change the name to trap.
So let's change layer to ground and let's place it.
No, sorry, not ground traps.
And let's place it halfway into the ground.
I really like how it looks.
Really nice one.
Um, now let's go and make animator for this one.
Animations.
We're going to make new animator controller trap.
So AC.
You're going to place it on the object.
Also, we want to give it a circle collider you're going to make it is trigger and we're going to give
it a damage trigger.
Very cool.
Now let's go to Animation Create new clip.
I'm going to name it as maybe Trap so idle.
And let's do trap so active first.
On the graphics over here.
We need trap.
So.
And let's take on all of the sprites and place them here.
Very cool.
Now we're going to make sample rate 20.
And on the animator it's going to be the animation by default.
Now let's create another clip.
Trap soul idol.
And we're going to make a boolean.
Wait, we just need a sprite over here on the trap.
So idle.
Now we're going to make a boolean active and we're going to transfer to idle if active is false.
Uncheck.
Exit.
Time.
Duration zero.
Then go back.
Exact time unchecked.
Transmission duration zero and condition would be active.
True.
Okay.
Very nice.
Now let's go back and just try to hit the drop.
I want to see if it's working.
Working except the animation.
I didn't see the animation for some reason, or I guess because this boolean here is false by default.
It's okay, we're going to fix it.
Now let's just click it here.
For now.
It's going to be true and it's going to work.
Very well.
We have a trap that is spinning and it is very dangerous.
Now let's make it move.
Uh, how are we going to do that?
We're going to need a script.
C sharp script trap.
So.
I'm going to open the script.
And inside of this script, we need a couple of things.
First of all, we want to have a movement speed.
So I'm going to do private plot move speed.
Then we need to know where this object is going to move.
So basically we need a waypoint.
And not just one way point, but a bunch of them.
Because of that, I'm going to make private transform array, and I've shown the arrays before, basically
can store a bunch of variables under the one, and then you can get access to the variable with an index,
uh, array of way point.
Okay.
And we need the index.
I'm going to make it public for now so we can see how does it work.
But later on I'm just going to hide it.
So let's do public int.
Waypoint index and I'm going to make it equals to one by default.
Also, I guess speed could be three by default.
Very cool.
Now let's go and open start method.
And in the start I want to set transform position of the object of the trap.
So equals to waypoint.
With an index of zero dot position.
Because of that, the object will snap to the first position in the beginning of the game.
Now we need an update method.
Update and in the update method, we want to move objects from one point to another.
Before we did that with the rigid bodies velocity.
But now we can use transform position and we can change transform position with a time.
Let's do transform position equals to vector two.
Move towards from current position to the target position, and target position is waypoint with the
index we have over here.
So waypoint index and then dot position.
And then over here.
Basically we need to set the speed of the movement.
But we cannot just use move speed because it will move super fast.
We don't need that.
We need frame independent movement to have frame rate independent movement.
We can do move speed multiplied by time dot delta time.
If you see time dot delta time for the first time, it basically returns interval and seconds from the
last frame to the current one.
If that sounds confusing, I'm just going to say that if you need some constant smooth movement and
it should be frame rate independent, you just multiply value by time dot delta time and time.
The delta time always change in itself.
If you have low pH this while you're going to be bigger.
If you have high pH this value, you're going to be smaller and it's always adjusting itself.
So you basically have like sort of time in seconds okay.
If all of that is confusing, just know that we're using time delta time to make it smooth and frame
rate independent.
Now that we move in object, actually, let's go and see how do we move the object.
We want to know if it's working.
On the trapsoul.
We need to put the script and over here we have waypoints which are null at the moment.
And waypoint index is equals to one.
Now let's make a couple of objects.
I'm going to make way point.
I'm going to give it a gizmo so I can see it.
And I'm going to duplicate it.
So the first one going to be here, the second one going to be here.
And we're going to open the Trapsoul and drag them into this array.
What something important here is that we do not count, uh, elements from one.
We count them from zero.
So if you want to get first waypoint, you would have to type waypoint with the index zero.
If you want to get second one you would have to type waypoint with the index one.
So now we have waypoint zero and waypoint one.
And according to the script we have it's going to place itself on the waypoint zero.
And then it's going to move to the waypoint one because waypoint index is equals to one okay.
Let's go.
And we can see it is moving.
Very cool.
Now we have a goal to make it move back.
That is very simple to do.
In the update over here.
I want to check if vector2 the distance between Transform.position and the waypoint.
We currently have.
Waypoint with a waypoint index.
Dot position is less than 0.1 F means we are close enough.
We can change waypoint index.
So we're going to move to another waypoint.
We can change it simply by simply by increasing the value.
Let's just type waypoint index plus plus.
And this will do it plus one every time we reach a new waypoint.
And to secure ourselves, we want to check if waypoint index is not bigger than the length of the waypoints
we have.
Simply type here waypoint index bigger or equals to waypoint dot length.
And if it is then we're going to do waypoint index equals to zero.
So we're going to increase value by one every time.
And if at some point it's going to be bigger or equals to waypoint dot length, then we're going to
make it zero.
So it's going to go back.
Let's go to unity now and see how does this work.
And we can see this cycle in between waypoints if you extend waypoints.
Like that.
It's going to fly between waypoints like that.
Oh, I forgot to add it to the elements.
Let's select the trapsoul and drag it here.
And it's going to cycle between three points.
There is something I want to do to improve it.
As of now, you can see it moves in circle like that.
What I want to do is to make it move, uh, sort of backwards when we need it to move back.
So it's going to move here, then here, then back here, and again here, here, then back here, back
here and like that.
But we're going to do that in the next video.
In this one.
Let's just slightly improve it and move on okay.
So I'm going to delete the waypoint over here.
I'm going to have only two, and I want to add a bit of a delay to the movement of the trap.
Let's go to the trap.
So let's make Privatebool can move, and we're going to make it equal to true by default, because we
want it to move from the start.
Then I want to make a coroutine private I enumerator stop movement.
You're going to pass float delay.
And over here we're going to do can move equals to false.
Then we're going to do yield return new wait for a seconds of a delay and then can move equals to true.
And I think.
You can add variables for the delay.
Let's do serialized field private float.
Cool down.
I'm going to make it equals to one by default.
Very nice.
Now we need to start this coroutine every time we set in index to zero I guess, because for now we
have only two waypoints.
And please pay attention.
This system is developed for the trap that works with two way points.
If you want to have more waypoints, we're going to do that in the next video.
So over here I'm going to do start coroutine stop movement with a cooldown.
Okay.
And in the update over here if you can move.
Equals to false, then return.
And we want to update boolean in the animator.
So I'm going to make private Animator.
Anim.
Then we're going to get it in the awake.
Awake.
Awake enum equals to get component of animator.
And then over here enum set bool.
Of active two can move boolean.
Very nice.
And there is something else I want to do.
I want to check.
The damage direction.
This is what we're going to do in this video, and then we're going to jump to the next one real quick.
So as of now.
Uh, active does not exist.
What do you mean?
Animator.
Oh, I misspelled it.
Okay, it should be active.
Let's go.
Moves there.
Stops.
Moves back.
We also need to flip the sprite.
We can do it right now, I think.
Then let's work on a damage direction.
Then we're going to jump to the next video.
So to flip the sprite we can get access to the component of a sprite renderer.
And we can check this flip x true false true false every time we need to do it.
Simple as that.
Let's go to the trap.
So let's do private.
Uh, sprite renderer SR.
Then in that week we're going to do SR equals to get component.
And every time we.
Return movement to the object.
We're going to do s r flip x.
Equals to naught as our flip X.
Because it's a boolean, it can be switched to an opposite value.
The very same thing we did on the player when we changed facing the right boolean.
Okay.
Now let's go.
And I'm going to show you something.
At the moment, the knockback of a player works accordingly to the facing direction we have.
So even if I stand over here and look to the left, I'm going to be knocked to the right.
Same goes here.
And I want to change knockback direction according to the source of the damage we have.
How are we going to do that?
When we call a knockback we're going to pass position.
Of the source damage to the player.
When we call a knockback, then we're going to see if trap.
So X position is bigger than player's X position means the source damage is from the right and we need
to be knocked to the left.
We need to make knockback direction minus one.
If an opposite thing is happening, an x position of the player is bigger than x position of the.
The source of the damage, then we need to be knocked to the right and knockback direction should be
equals to one.
So we're going to go to the player.
Over here.
We're gonna pass.
Float.
Source damage X position.
Then over here we're going to make float.
A knock back direction equals to one.
So it's going to be one every time we call in a method.
And then we're going to check if.
Transform position.
Dot x of the player is less.
Then source damage exposition.
Then it means we are on the left side from the source of the damage, and we need to be knocked to the
left.
And for that we're going to do knockback direction equals to minus one okay.
And now we can just use knockback direction over here.
Very cool.
Okay, now let's save what we have here and let's go to the damage trigger over here.
And over here we need to pass X position and we're just going to pass transform position dot x.
Simple as that.
Now we can go back to unity and see the difference.
I can move into the trap.
I'm going to be knocked same as before.
If I go to the trap and I faced another direction, still, I'm going to be knocked to the left and
to the right.
Which is very good, right?
It's more how it's supposed to be, I think.
Yeah.
Very well.
Now, there is something I want to tell you about these waypoints.
You're going to make it better, but as of now, it should be just like so in the hierarchy.
And in the next video, I'm going to show you what to do with it and how to make it cleaner, how to
not clutter up the hierarchy we have at the moment.
But that's going to be in the next video.
I'll see you there.