Let's create some traps.
Game object basically traps.
To do that, I'm going to head over to my project app.
Then assets.
I will head over to my sprites folder, then Pixel Adventure one folder.
So just wait a minute.
I'm going to make this like this.
And let's move over to sprites folder.
Then Pixel Adventure one folder and I have three folder inside there sub folder.
And let's go to traps and let's select the sofas.
So we're going to deal with the sofa first.
So let's select the sprite sheet.
Let's go over to Inspector.
Let's click on this open Sprite editor editor.
So let's slice this individually.
So pixel per unit we're going to choose 16 filter mode to point no filter.
And then click on this apply button and make sure sprite mode to multi.
Because we're going to slice individually.
Instead of slicing automatic we're going to move over to our Sprite Editor tab.
Let's click on this slice and we're going to select automatic to read by cell count.
And let's count how many columns and rows we have.
We can see one row and four columns.
So we're going to put eight columns and one row.
And after doing that let's click on this slice button and then click on this apply button.
And I'm going to choose my game view once again.
And let's go over to that spreadsheet.
Click on this arrow in order to see the individual sprite.
And in the bottom right you can see in your project tab you can just scale this up like this.
It's up to you basically.
So I'm going to select the first sprite.
Let's drag it over to the scene view or into this hierarchy.
So I'm going to select that.
Let's drag it over to the scene view or into this hierarchy.
As you can see we have our shop.
So let's call it something like shop.
So let's go over to Inspector and let's call this one store.
And we're going to add some some components to this shop game object.
First thing we're going to add a rigid body 2D and we will set the gravity scale to zero, so it's not
gonna fall down at all.
Second, uh, second component we're gonna add a circle collider 2D for collision detection.
So let's click on this add component.
But before you do, make sure you select your GameObject in the hierarchy.
Then go over to Inspector and then click on this add component.
And we're gonna add a rigid body 2D.
So let's type that and it's going to pop up.
And as soon as it's gonna pop up we just gonna click on it in order to attach that.
And now we're going to set the gravity scale to zero.
So let's set this to zero and collision detection.
We're gonna choose continuous and interpolate to interpolate.
And we can freeze the rotation about z axis.
Or we can just skip it that.
So now let's click on this rigid body one more time just to collapse at once.
And let's click on this add component.
And now we're going to add a circle Collider 2D.
So let's type that circle Collider 2D.
Or we can just go over to, uh, Collider 2D and we can just add that up.
So let's attach this circle, Collider 2D, and we need to adjust the collider as the size of our sprite.
So we're going to move over to Inspector and then click on this Edit Collider.
I just want to make the radius a little bit smaller.
So I'm going to move over to my radius uh inside this circle Collider 2D component.
And let's make it something like 11.1.
Let's make it 1.1.
And then Simms.
Perfect.
And we're going to assign this circle Collider.
Sorry this game object as a trap game object since this is going to be a trap.
So that's why we're going to assign this so game object as a trap tag.
To do that we will go over to Inspector.
Let's click on this tag and then click on this add tag.
And let's create a new tag for our show.
So let's click on this plus icon.
And we're going to no sorry we're not gonna name it to something like.
So.
Instead, we're gonna call it trap because we're gonna have multiple traps, so that's why.
So let's call this one trap, and let's click on the save button and then select your game object.
Then go over to Inspector and Assign with the tag which you recently created.
So I assigned this game object as a trap.
As you can notice in Inspector.
So I assigned this game object as a trap tag.
And now we can just go ahead.
We can and then we can and we can just make the animation.
So let's make the animation.
So to make the animation we will move over to our project app assets and then sprites folder.
And we will head over to that folder.
And then we will head over to Source folder.
There we can see we have the sprite sheet.
So just click in this arrow to see the individual and select all of them by holding down shift.
And then and then drag it over to this game object on top of this object in the hierarchy.
In order to make the animation right away.
So just wait a little moment.
It's lagging a little bit.
So we need to place the animation where we want to place it.
So for that I'm going to just click on this double arrow in the top left side we can see and let's click
on this assets in assets.
We already created a animation folder as you can notice.
So we're going to open that up by double clicking on it.
And let's uh let's create a sub folder for our traps.
So let's right click and then new and folder.
And we're going to call this folder to something like traps folder.
So let's call it traps underscore underscore animation animation.
And let's open up this sub folder by double clicking on it.
So just bit wait.
So I really apologize.
It's just lagging a little bit.
So let's open that up by double clicking on it.
And we have to name the animation.
So this will be show, uh, and it's gonna spin.
So I'm going to call it simply something like so.
So let's call it so.
And after doing that let's click on the save button.
And now if we click on this save button and then select our show GameObject in the hierarchy.
Then move over to the animation tab.
You will notice that this GameObject just gonna spin around itself.
So just wait a moment and you will be able to notice that.
So just wait a moment and once it's done, we can just go ahead and check that out.
As you can notice we just created the animation.
So I'm going to select my shock GameObject in the hierarchy.
And let's move over to this animation tab.
In order to check the animation, let's click on this play button in animation tab.
And indeed we can notice that this shock GameObject just rotating itself in the z axis as you can notice.
So we just created and also let's create a script in order to move this game object between 2 or 3 points.
Even more than that, points even more than 5 or 10 points.
It's basically up to you how many points you just want to move your traps or this game object.
For that, we need to create a script.
To do that, we will head over to our project tab.
Then we will head over to assets folder and then Script Folder.
Let's right click there, then create, then Monobehaviour script.
And you can name that script to something like Shaw.
It's up to you, whatever you want to call it.
I already created a script called so and I have it right over here as you can notice.
So I'm going to select my game object in the hierarchy.
And let's select this script and drag it over to this add component section and Inspector.
Or you can simply click on this add component.
And you can find the script by typing the name of the script.
And then click on it in order to attach that.
So we want to move Uh, our shock ammo check between two points.
So let's create those two points.
To do that, I'm going to move over to my hierarchy.
Let's right click.
Let's firstly create an empty parent game object.
And inside that parent game object we're going to create two child game object to just keep our hierarchy
a little bit clean.
So we're going to create an empty parent game object first.
And let's call this GameObject to something like points.
So let's call it points and then go over to Inspector then transform.
Then right click transform and then reset.
And if you want to choose an uh gizmos or icon then go over to Inspector.
Click here in this box.
And then we're going to choose this yellow or anything like that.
Let's click on this.
I guess we are able to see.
I don't know.
Okay.
That's fine.
We can just okay.
We have it over here.
So now as you can see we created this point.
Points game object.
Now we're gonna create two tile game object or two points.
In order to move this swap between those two points or more than two points.
So let's right click on this point game object.
Let's create an empty game object.
And we can call this one first point or let's simply call it one.
And now we're going to select the move tool in the scene view.
And let's select that game object.
And let's move it somewhere around here.
And if you want to choose any gizmos for it then go over to Inspector.
Click here.
And we're gonna choose this yellow one.
As you can notice right over here we have and let's duplicate this one in order to create the second
point.
To do that I'm going to select my one game object underneath my points GameObject in the hierarchy.
Let's select that and we're going to press down Ctrl D to duplicate.
And let's move it down somewhere over here.
So between these two points we just want to move our at this raw game object and let's call the duplicate
point, uh, point to something like two, uh, like that.
So that's why we created an empty parent game object, as you can see.
And inside that parent game object, we just created these two child game object or these two points.
And we can just click over here to collapse.
As you can see now our hierarchy seems a lot more clean.
So that's why we just created an empty parent game object first.
And inside that parent game object we just created two child game object.
Or you can simply say these two points or position.
And I'm going to select Mysore and let's move it somewhere over here like that.
And we can just go ahead and we can open up our script.
So we're going to open up our source script.
Uh, so let's select our source GameObject in the hierarchy.
Let's go over to Inspector and let's open up the source script by double clicking on it.
And what we need, we basically need a reference both of those point.
So we can just move between those two points.
So what we will do, we will go to top of our class, uh, class.
And over here we're going to make a serialized field and that will be transformed.
So let's make it over here.
So let's make a serialized field.
Sorry.
Serialized field.
Sorry.
I really apologize for that.
Okay.
Serialized field.
And this will be private.
We're going to mark private instead.
Even we made it private.
We are gonna able to notice, since we just mark mark it as a serialized field.
As you can notice, and we need a reference.
Both of these points.
We need both of these points, this first point and the second point.
Or you can simply say position.
So we need to type transform because transform we don't know it's responsible for position.
So we're going to type transform.
And we can call points, but we have more than one point, as you can notice.
Uh, as you can notice, we have this first point and the second point.
So we basically need to create an array.
So basically right after this transform you basically need to make this brackets.
And now this is an array.
An array basically you can uh you can put multiple things.
It can be anything like game object numbers even position.
So that's why we just made this array basically square brackets.
And we have to name the array what we want to name it.
So we're going to call it something like points.
And let's close that up with semicolon.
And now we just created our points array basically a variable.
And we're going to put our first point.
And the second point inside this array that we created which is our points.
So now I'm going to press down Ctrl s to save my script.
And now let's move over to unity in order to save our script.
And we need to assign those points.
So we just created an array.
And we do know that array is basically a parameter type.
But it can hold up multiple values.
It can be numbers.
It can be names.
It can be position.
It can be, uh, anything like that.
So you need to make square brackets right after your transform you making.
So make sure you just do that.
And that's basically reference our array.
And we need to take one by one point.
And we can simply just drag it into this array.
And in the start of the end basically once our source script just gonna compile completely, uh, our
array is just gonna be empty.
So we need to put, uh, both of these point the first point and the second point into that array,
and you will see that.
So just wait a moment and once our compilation is done, we can just go ahead and we can just assign
those points, or we can just store the first point and the second point into that points array that
we created inside our C script.
So just wait a moment to compile our script, and once it's done, we can just go ahead and we can assign
those, uh, assign those, or we can simply drag one by one, uh, into that points array.
So as soon as our, uh, script compiles, as you can notice points and it says zero.
So what we can do, we can just move over to hierarchy than points game object.
And we can select the first point and drag it over to this points uh array.
And then we're going to select the second one and let's drag it over to this points array.
Now we can notice we have the both of these points.
Uh so this first point basically the top one we can see it's assigned with a zero index as you can see.
And the bottom position or the second position we can see index is one.
So both of these position just get assigned with an index.
So the top position or the first uh first position index or the point uh point index.
We can notice it's zero and the second one is one.
So let's suppose we want to move to the first point.
So what we need to do we basically need to pass our zero.
So in order to move our show game object we basically need to open up our script.
So let's double click on it in order to open that up.
And we will go to top of our class.
And we're going to declare a private variable to just keep track of which point we are in.
And we do know that if the index is zero, it's going to be the first point.
And if the index is one basically means the second point or the bottom point.
So we're going to make a variable to just keep track of which point we are in.
So we're going to make a private.
And that will be in like 0123.
And we can call this one index point point index.
Let's call it simply point index point index.
And let's close that up with semicolon.
And now what I'm going to do I'm going to move over to my start function.
So this is a start function.
Get call in the start of the game and the start of the game.
I'm gonna set this point index to zero.
So I'm going to set point index sorry point index equal to zero.
And let's close that up with semicolon.
And now what we're going to do we're going to move over to this update function.
And we do know that this update function runs every single frame.
So every single frame we need to move over this short game object between these two points.
So we want to move to the first point.
So as index we're going to pass zero.
So in order to move that what we need to do we need to use a function.
And it's called it's called vector2 dot move towards.
But before we do we need to set the current position.
So we will go to transform dot position and transform dot position.
Right over here basically means the current position of that.
So game object basically this game object current position as you can notice in the inspector.
And so what we're gonna do we're gonna set the current position equal to vector two.
a vector2 dot move towards function vector two dot.
Sorry, you need to use this function and this vector to dot move towards function as a first argument.
It's going to take the take the current position where you are in.
So we're going to pass our transform dot position which basically means our current position.
And the second it's going to take the target position or via equipment to go.
So we're going to pass our points array.
So we basically need to type our points array.
Or basically we have to type the point points array.
Or basically we need to pass our array.
And then we need to make these brackets.
And inside this bracket you have to put the index a put.
You can put the index that you just want to move over to that point.
So I'm going to pass my point index I just created.
And make sure right after this square brackets you need to type the dot position.
Because this this basically this array is a Transform array.
Or you can simply say position array.
And the third argument is going to take the speed with how much speed you just want to move over to
that point.
So we're going to declare a variable for our speed.
So let's go to the top of our class and let's make another serialized field and private.
And this will be float.
And we can call this one something like move speed.
So let's call it move speed.
And let's uh by default I'm going to set something like to f something like that.
And third argument we're going to pass our move speed and make sure you multiply this with time dot
delta time to make frame rate independence.
So I'm going to pass our time delta time.
And let's close that up with semicolon.
So what we simply doing inside this update function.
So transform.position basically means the current position of that.
So game object and we're setting equal to Vector2 dot move towards function.
And this vector2 dot move towards function.
Basically as a first argument it's going to take the current position where you are currently right
now.
So we just pass Transform.position, which basically referencing the position we are currently in or
current position, current position of that GameObject.
And the second thing, it's going to take the target position or where you want to move your game object.
So we pass our points.
And inside this square brackets we basically need to pass the index.
And we pass point point index.
And as you can notice in the start of the game we just set our point index to zero.
So this one right over here it's going to be zero.
And we do know that zero basically means the top position or the first point.
So this game object is just going to move over to that point.
So if I move over to my script one more time so just wait you will notice.
So we're gonna move towards to the first position as soon as we're gonna start our game.
As you can notice right over here.
So this point index is zero and zero.
So we're just going over to that position.
Uh, right after, uh, we just accessing the, uh, position of that array.
As you can notice.
And the third argument is just taking the speed with, uh, how much speed we just moving over to that
point.
And now what we're going to do, we're going to press down Ctrl s to save our script.
And now let's move over to unity.
And you will see, as soon as we're gonna start our game, we will able to notice our game object,
or this trap is going to move over to the first point or the top position because the top position index
is zero.
So that's why.
So just wait a moment and you will see, uh, you will see that, uh, we basically just going to move
over to the first point as soon as we're gonna start our game, because the first point index is zero.
And we can notice underneath our show script and the first point index, we can notice it's zero.
And the second point index is one.
So let's suppose you just want to move to the second point.
So as a as a index you need to pass this one.
So it's you you're just going to move over to that point or the bottom position or simply the second
position.
Now our script gets compiled and I'm going to drag my stroke game object a little bit down somewhere
over here.
And if I just go ahead and then click on this play button, you will see as soon as we're gonna, uh,
start our game, these are just going to move over to the top position or the first position because
as in point index, in the start of the game, we just set it to zero.
And zero basically means the first point.
And you're gonna notice that.
And indeed we can notice that it's, uh, going to the first position and it's just sticking right over
here.
So we also need to uh, basically we also need to move over to this second position or this bottom position.
So basically what we have to do, we need to increase our point index by one.
And we do know that if we put a one inside that array point.
Index array point index one.
So it's going to move over to second position or the bottom position.
Because the bottom position we can notice underneath our show script is index is one.
So let's get out of play mode.
So what we're gonna do before we just move over to the first position, we basically want to check the
position between our first point and this.
So game object if the position between our show and the first point or this point with this point is
too much close, then we're just going to increase our point index by one.
And we do know if we increase.
So it's going to move over to the bottom position or the second position.
So let's go over to script.
And we're going to do it over here.
So before we move position we just want to check.
So let's make if condition make first bracket and some curly brackets.
And what we want to check over here we basically want to check.
So we need to use this vector to dot distance function.
And this function it's going to take two position and it's going to take two position.
And it will compare between them.
So we want to check the position with our saw.
So we're going to pass our transform dot position.
It's basically means current position of that object.
And the second thing it's going to take the basically as a second argument it's going to take another
position that we want to compare with.
So we're going to pass our points array.
So we need to pass points array a points array.
And we need to make this square brackets.
And inside this square brackets we basically have to pass our index.
So we're going to pass our point index and point index.
In the start of the game we set zero.
So it's our game object.
Just gonna check the position between our this row and this first position and make sure you access
the position.
So right typing square brackets you need to type this dot position because we need to access the position.
And now if the position between them, if it's lesser than some small number, some smaller number,
we're going to check with.
So we're going to put zero point 1FA pretty smaller number something like that.
So if that is the case basically means our game object.
So we using this Vector2 dot distance function and this Vector2 dot distance function, it's going to
take two position and it will compare between them.
So we just pass as a first argument current position which is transform.position basically means current
position of that.
So game object.
And the second thing we just pass our point index or the top position or where we just moving.
And in this start of the game we do know this point index that's going to be zero.
So in the start of the game this if condition is just going to check the between our this game object
and this first point.
So if it's lesser than some smaller number as you can see we're checking then what we need to do basically
means we just reaches right over here.
So we basically need to increase our point index by one.
And if we increase point index by one then it's going to move over to this point index.
Sorry.
It's going to move over to bottom position or second position.
You can simply say so now let's move over to unit is script.
And now what we have to do we reaches our first point.
You can simply say the the distance between our show and the first point is too much close now.
And now what we need to do we need to increase our point index.
So what we will do, we will use this point index plus plus.
It basically means that we just adding one.
So in this start of the game we set zero.
So plus plus basically means we adding one.
So zero plus one.
It's going to be one.
So this point index it's becoming one.
So this over here right over here it's going to be one.
So so our now the game object just going to move over to our second point.
because this point index is just going to be one.
As soon as our game object reaches top point or the first position, then we increasing our point index
by one.
So the point index right over here becomes one.
Means our game object just going to move over to our bottom position.
Or you can simply say the second position.
And now we're going to press down Ctrl S to save our script.
And let's move over to unity.
So as soon as our game object just just going to reach our top position, it's going to move over to
second position.
Because the we checking the distance between our shore and the first point, if it's lesser than with
some smaller number, then we just increasing our point index by one.
And we do know if we increase by one that means point index becoming one and one.
We do know that is the second position.
Or you can simply say this bottom position.
So just wait a little moment and it's done now.
So I'm going to move it somewhere over here.
And let's click on this play button.
And you will see as soon as this game object and the first point, uh, first point position, if the
distance between is too much close, then we increasing.
Then we increasing our point index means it's gonna move over to the second position.
Or you can simply say to the bottom position.
So just wait a moment.
And indeed we can see it just reaching the second position.
And now it's stuck at second position or this bottom position because we just keep increasing our point
index.
So we're gonna get out of play mode.
So now what we're gonna do if this short game object is, uh, something like over here with the second
position, uh, or the bottom position, you can simply say if the distance between is now too much
close with the second position.
Then again, we're gonna set back our point index to zero.
So this, uh, so just gonna move over to top position if we set back, uh, point index to zero, because
zero basically means the top position.
Or you can simply say the first position.
So what we will do for that, we're going to move over to our script.
And over here we're going to check.
So let's go to top of our class and we will check.
So we're going to use this if condition.
Let's make first bracket and some curly brackets.
And what we want to check over here.
We basically want to check uh sorry.
We basically want to check if our point index if it's equals equals to points uh points dot length.
Points dot length basically means how many points we have.
So point if the point index is equals equals to point dot length basically means how many points we
have.
So we have we can notice we have one and two.
So this point index points dot length right over here it's going to be two.
And if the point index is one.
So as soon as it reaches our point basically means we just uh over here somewhere over here.
So we're checking as you can see we're checking if our point index is equals.
Equals to points dot length.
Points dot length basically means how many points we have.
Means our game object is right over here.
So now what we need to do.
We need to set back our point index to zero.
Because we are increasing.
And we are checking if the point index is equals equals to our points dot length.
So if that is the case then we're going to set back this point index to zero once again.
And we do know if we set back to zero it's going to move over to our top position or the first position.
So we're going to set our point index back to.
So let's set our point index equal to zero.
And let's close that up with semicolon.
And we're going to press down Ctrl s to save our script.
And now let's move over to unity.
So as soon as this game object just going to check with second position or the bottom position.
And we do know once it's just going to reach bottom position.
So the index point index is going to be one.
And we're checking if the point index is equals Equals.
To.
Oh.
Or that.
How many points we have.
Then we again setting back our point index to zero.
And we do know that the point index zero basically means the top position.
And point index one basically means the bottom position.
So we will able to notice that this game object just going to move between these two points, once it's
going to reach the bottom position, then it's again going to move over to top position because we changing
the point index.
So let's click on this play button in the top in order to play our game.
And we're gonna take a look on that on this.
So as soon as this point sorry this game object just going to check with the second position or the
bottom one is going to move over to the first point once again.
And we can notice that it's moving to the first point and second point.
And as soon as it's reaching or sorry reaching to the second point, we can see it just moving over
to the first point.
But we getting an array?
Sorry, we're getting an error in our console tab that says.
Index was outside the bounds of the array.
So we basically need to get out of play mode.
And we need to move over to our script.
And we need to put this line of code right over here underneath, uh, underneath everything.
Uh, so we can just move, we can select like this and we can hold down alt or alt key on our keyboard,
and we can use our up or down arrow key in order to move this line of code, as you can notice.
So I'm gonna move it somewhere over here.
And now that's fine.
And make sure you press down Ctrl s on your keyboard in order to save your script one more time.
And it's gonna save.
So I hope you get the idea.
Now I'm gonna press down one more time to control S, and let's move over to unity.
So it's gonna be fine by now.
We're not gonna see any error in our console tab.
I hope you get the idea that how you can just move a game object between two points and you can create
as many points you want.
So you can just duplicate this second point.
And you you need to make sure that you also, uh, also select the third point and then drag it over
to this points array, or else you're gonna get some error or you not gonna move over to the third position.
So that's why.
And I'm gonna show you that.
So let it first compile it.
And let's click on this play button in the top in the in order to play our game.
And let's take a look on this.
So just wait wait a moment.
And by now we should not see any error messages.
Just going to pop up underneath our console tab.
So I'm going to clear it first.
As you can notice in our console tab we not getting any error messages.
Let's suppose we want to move this game object.
Uh three points.
Something over here as well.
So we're gonna get out of play mode.
For that.
We need to create one more point.
Or you can simply say position.
So we will select the second or the first position.
And let's press down Ctrl D to duplicate.
Uh sorry.
And we're going to move it somewhere over here.
And let's call this point to something like three.
So let's call it three.
And now what we have to do we need to put this three point, uh, basically the third point into our
array or else we're not gonna move at all.
So to the third point.
So I'm going to select my sock object in the hierarchy.
Let's go over to our script in the Inspector.
Uh, so we can see we only have the first point and the second point and we don't have the third point
yet.
So we will select the third point and let's drag it over to this point array.
Now we can see uh, the third point index is now two, index is two, second point index is one.
And the first point index is zero basically means this.
Uh short game object just going to move around between these points, because we're just checking the
distance between our points.
And if that gets closer, then we just are increasing our point index.
And once it reaches the final position, then again we're setting back point index to zero.
So that's why this game object is just going to move over to first point one more time.
So let's click on this play button in the top in order to take a look on this.
And you can increase the speed as well by just going over to your your source script.
And you can just play around with the move speed.
So just wait a moment and we will able to notice that.
So this game object just gonna move between these three points because we just increase and we will
able to notice.
So it's move it over here.
Then here as you can notice indeed it's moving to the third point.
And we basically need to move our show a little bit something over here.
And we can notice that.
So I'm going to get out of play mode.
And what I'm going to do I'm going to move the third point somewhere over here to just to just make
sure.
So I'm going to select my third point and let's move it somewhere around here.
And let's press down control S in order to save our project.
And I'm going to move this here.
So now let's click on this play button in the top in order to play our game.
And we will be able to notice our game game object.
Just going to move between these three points and one.
Once it's gonna reach the final position or the third point, you can simply say then again we're setting
back point index to zero basically means our show game object.
Just gonna move over to our first point right away.
As you can see, indeed the show game object just moving between these three points and you can create
as many points you want and make sure you track those points into your array.
So you're gonna have the position, and after that you can just move your game object.
So I hope you get the idea.
So I'm gonna get out of play mode.
And also make sure you make this game object a prefab so you will have the copy of it.
So I'm going to head over to my project tab then assets folder.
So just wait a little bit assets folder and then prefabs folder.
And let's select the GameObject in the hierarchy.
And let's drag it inside this prefabs folder in order to turn it into a prefab.
And now as you can notice, we just turn it into a prefab.
Basically, it's turned out blue means we just made it prefab and we can just click on it at this point
scheme object in order to collapse all of our points.
So that's why we just created an empty parent game object first and into that game object, we just
created those tile game object in order to make our hierarchy a little bit clean or the project, you
can simply say, and we're going to press down one more time, Ctrl s on our keyboard in order to save
our project.