Hello guys.
Welcome to the video.
So in this one I want to show you how to upgrade the trapsoul, how to extend the waypoints and how
to make it cycle between waypoints.
But it will go from start to finish then from finish to start, sort of like ping pong, you know?
Yeah.
First let's make the system so it will cycle between multiple waypoints.
Then I'm going to show you how to clean up the hierarchy and how to hide these waypoints.
So they are not visible and they're not making a mess.
Um I want to move.
Trapsoul.
Over here.
I want to take first waypoint over here.
Second waypoint over here.
And then third waypoint over here.
And let's assign third waypoint to the array.
And now our goal is to make it move up right.
And then go back to this point.
Yeah.
And this is how we do it.
We go back to the script and we need to define the movement.
Direction can be simply done by making public int.
Let's do uh move direction equals to one by default.
So one means forward minus one means backwards.
Now we're going to go back to this if statement.
And just to make it simple I'm going to erase everything because we're going to do it a bit differently
when we moving forward I'm going to take waypoint index and do equals waypoint index plus movement direction.
And because it is one at the moment, it's going to do plus one every time we moving forward when I
say forward I mean from the first element to the last one.
Yeah.
Then I want to do something here.
When I'm reaching the last element in the array, I want to change direction to minus one.
This is how we're going to check it.
If waypoint index is equals to waypoint dot length minus one then I'm going to make move direction equals.
To move direction multiply by minus one.
When we multiply in value by minus one, we basically make any negative or positive we setting it to
an opposite.
Yeah.
If it was one it will become minus one.
If it was minus one it will become one.
Now why do I use waypoint dot length minus one?
Its answer is very simple.
To find the last element in the array, we need to check the length of the array and do minus one.
As you can see, length of the array over here is three, but the last element in the array has index
of two.
So to get index of two we just need to get the length minus one.
If it would have five elements, we would need the last element with the index of four.
So we would do five minus one.
So basically you just do length minus one.
Simple as that.
Now this will turn it back only when it reaches the last point.
But still we want to see it.
So let's leave it like that.
Let's go back.
And let's go and see it in the action.
Goes there, goes there.
Then it turns back.
Very nice.
Now we need to turn it back here from the start.
We can do it simply by going to the script and type in or waypoint index is equals to zero.
So if waypoint index is the first element then again we can change move direction and nothing else we
need.
For now let's save this.
Let's go back and let's try this.
Turn back and then turn it back again.
Very cool.
I love it so much.
Now let's go and deal with the waypoints, because now we need to make a prefab.
But if I take the prefab and if I drag Trapsaw over here, then this prefab will have no waypoints.
And somehow we need to transfer them together with the object.
Uh, one of the ways to do that would be make them as children of a trap.
So let me do prefab unpack completely.
Yeah, but there is an issue.
When you move a parent object, the child object moves as well.
And if we move uh trapsaw to the waypoint and waypoint moves, then we have endless movement, just
like you see now on the screen.
Not cool.
One of the ways to fix it would be go to the script and unparent each element to make it like independent,
because if it has no parent, it's not going to move together with the trap.
So and then it's going to work fine.
But that could clutter up the hierarchy, which I don't really want to have in the game.
I want my hierarchy to be clean so I can see everything and find the objects I need when I'm making
the game.
So because of that, I want to make another array.
Let's do serialize field, private Vector3 array and I'm going to call it um maybe waypoint.
Position.
Yeah.
Just like that.
So now this array has vector three and transform dot position is also vector three.
Let me add three elements.
You can see it has here.
And if you select any of the waypoints this is the transform position over here.
So somehow we need to transfer position of this object into this array.
We can do it with the help of a for loop.
If you never seen for loop before, I'm going to briefly explain how does this work okay.
I'm going to type for click tap twice and it's going to create a for loop.
The for loop does a certain amount of steps.
And you can cycle through something with a for loop because you can define how many steps you need to
do.
Usually you type amount of steps over here and let me make I equals to zero.
Okay.
So this is a common for loop.
It's going to do five steps to understand how it works.
Let's do debug log I we're going to debug this I over here let's go to play mode.
And let's just see the console.
And over here we have 01234.
So basically you see how I element was increased with each step of a for loop.
And once I was not less than five for loop stopped.
Right.
This is how it is.
You have I equals to zero I less than five.
Yes.
Then do something and do I plus plus then I equals to one.
Is it still less than five.
Yes.
Then do something and do I plus plus.
And when I becomes bigger or equals to five it is not working anymore.
So we can use it in our advantage because instead of five we can set it to waypoint dot length.
And now instead of debugging and I we can do way point position I which is going to be zero, 1234 and
so on equals to waypoint I dot position.
And we just need to go here and do uh waypoint position equals to new vector three.
And in a square brackets we're going to do waypoint dot length.
So basically this one creates a new array.
Just add there with the length of waypoint dot length.
So no matter how you change this waypoint dot length the waypoint position array will always uh have
the same amount of elements.
Now let's go to play mode and see the result in the inspector.
And boom, we've got the positions.
Very cool.
Right?
I love it so much.
Very nice.
Now that we have it working, let's hide this array because we don't need it anymore.
We know how does it work.
So I'm going to just make it private without serialize field.
And then I want to take these lines of code and wrap it into a method.
Let's do extract method update away points info.
Yes, this is better.
Now this will update waypoint info.
The only thing we want to do here is to place this before we change transform position.
It is important because if we change transform position before we update waypoints, then waypoints
are going to be moved.
You know, um, maybe let me show it to you.
Why not?
I'm going to save it without the updating waypoints.
I'm going to go to play mode.
And let's say my trapsoul is over here, but my first waypoint is over here.
Okay, look, when I move this object to the first waypoint with the.
With this line of code over here.
It will snap there and all waypoints are going to be moved together with it.
Okay.
Here is the waypoint.
Here is the waypoint and here is waypoint.
So this is not what we need.
So first we want to update the waypoints.
Save them into vector three.
Just like that.
And then we can take transform position and make it equals to waypoint position zero.
Nice.
Now let's go to the update.
And use it here as well.
Instead of waypoint, I'm going to use waypoint position waypoint index and over here as well.
I'm sorry.
Waypoint position with a waypoint index less than 0.1 F.
Okay okay okay okay.
Now the only goal we have is to stop movement every time we reach in the final point or starting point.
You can do it simply here by starting a coroutine of a stop movement with a cooldown.
Nice.
I'm going to save this.
Going to go back to unity.
Now this can be used as a prefab.
We can go to prefab folder.
Drag it here.
And if you need a new trap.
So.
You can just drag it into the scene, take the waypoints, place them where you want them to be.
For example here.
Here and over here.
And for this one, let's say I want.
It to be here.
We just.
With just two of them.
Okay.
Should I?
Yeah, actually.
Let's do one more over here.
Yeah, I think it's nice.
Now let's go to play mode and see if something broke or not.
I think it's going to be fine.
And boom, we have two traps moving.
With the waypoints.
This one is not moving because I forgot to add it into the list.
So don't forget to add it.
Over here, it should be added.
It just added here, like so.
And that's good.
Yeah.
Nice.
I think I just forgot something over here.
Yeah, instead of waypoint lengths, let's check waypoint position dot length, because that's the waypoint
we're using.
All right.
This is good.
This could be improved to make it a bit more convenient.
For example we could collect children of the trapsoul automatically so we don't have to reassign them
in the inspector.
But we don't really need it right now.
So I guess that's it.
And I'll see you in the next video.
Bye.