Let's make some collectible items.
So I have some, uh, some fruits.
You can simply say.
So for that, I'm gonna move over to my project tab assets and then pixel, sprite, uh, sprites folder.
Then I'm gonna move over to my Pixel Adventure one folder, then items, and we can see we have checkpoint
and we have some fruits, uh, over here inside this fruits folder.
So we can we have strawberry.
We have pineapple.
All of these.
You can choose any of this.
I will have a, uh I will have them in resources.
You can just download those.
So I'm gonna choose Apple for this.
So I'm going to select this apple.
So let's select that.
Go over to Inspector.
Let's click on this open Sprite editor.
So we need to slice individually.
So we're gonna choose multiple sprite mode to multiple and pixel per unit.
Let's do something like 16 and filter mode to point of filter to get rid of blurry.
And let's click on this apply button in the bottom right.
And instead of slicing automatic we're gonna slice grid by cell count.
And it's gonna be fine.
So that's why.
So just wait.
Now it's done.
So let's go over to Sprite Editor tab.
Let's click on this slice.
Let's select automatic to create by cell count.
And let's count how many columns and rows we have 1369
1215 1715, 17 I guess let's put 17 columns and one row.
Let's click on this slice and then apply.
Now it's fine.
Let's select the game view.
And let's click on this arrow in order to see the individual sprites.
So I'm going to select the first one.
Let's drag it over to the scene view or into this hierarchy.
As soon as we drag it we can see it's pretty much small, so we need to make it a little bit bigger.
So we're going to select the sprite sheet one more time.
And let's make this pixel per unit less number to make it uh the sprite size bigger.
So we're going to do it something like 12.
And let's click on this apply button.
And I guess 12 seems pretty much fine.
But we can go ahead and let's make it ten instead of 12 to make it a little bit bigger and noticeable.
So I'm gonna do ten.
Let's click on this apply.
Now ten is pretty much fine.
And let's select the game object in the hierarchy.
Let's drag it in the top so we can see.
And let's rename that to something like Apple.
Uh Apple.
So let's call it Apple.
Or we can simply call it uh icon something like Apple icon because uh, we just want to, uh, make
uh, two animation clip of this once we're gonna collect this apple.
Uh, we just gonna make an animation like it's gonna go up, and over time, it's gonna fade out.
So we're gonna have to animation.
So that's why we're gonna create an empty parent game object first, and then we're going to make this
apple a child of that game object, or else our animation just gonna be weird.
So that's why.
So we're going to select our Apple in the hierarchy.
Let's call this one Apple icon.
So we're going to call it Apple icon.
And now let's create an Apple Game object.
Uh basically we're gonna make that one parent.
So for it we're going to move over to our hierarchy.
Right click or hit this plus icon.
Then create an empty parent game object sorry empty game object.
And we can call this game object to something like Apple.
And now what we're going to do sorry.
It should be Apple okay.
So it's pretty much fine.
Now we're going to take our Apple icon and let's drag it on top of this Apple GameObject in the hierarchy.
In order to make our Apple icon child of this Apple game object.
So if I select our Apple, uh, make sure you select your apple, then go over to uh sorry Apple icon.
Then go over to Inspector Transform and right click and reset.
So gonna reset the position with our Apple.
So if we select our Apple GameObject in the hierarchy then select this move tool in the scene view.
If we move our Apple game object, we can notice that our Apple icon is also moving with it.
Why?
Because Apple Icon is a child of this Apple game object.
Now we're gonna take our Apple game object.
Let's drag it in the top so we can see all the time.
And now we are ready to make our animation.
But before we make animation, let's add some components to this Apple game object.
So firstly we're going to take the take.
We're going to take it something tag it something like apple uh or collectible items.
So let's add tag with Apple instead.
So we're gonna select that.
Let's go over to Inspector.
Make sure you select the parent one, not the child one.
Inside this child one, we're going to create the animation.
And in inside this parent game object we're going to add those components.
So we're going to select our Apple.
Let's go over to Inspector Tag.
As you can see it says untagged.
So let's click there.
Let's click on Add Tag.
And we're going to create a new tag for this apple.
So let's click on this plus icon.
And we're going to name the tag something like Apple Apple.
And let's click on save.
And we're going to choose our apple sorry Apple.
And then go over to Inspector and make sure you just tag it as Apple.
Now as you can notice we just assign it this Apple game object as a Apple tag.
And now we're going to add a component.
So first thing I'm going to add a rigid body 2D.
And we're going to set it to kinematic.
So it's not going to fall down at all.
So let's click on this add component and make sure you do it in inside.
Basically do it inside an object, not the child one inside our child one.
We're just going to make the animation.
So that's why.
So we're going to add a circle collider 2D.
So let's add that up.
You can add any collider but make sure it just fits perfectly perfectly.
So that's why.
And we're gonna are we gonna make the circle a little bit bigger.
So let's move over to our circle Collider 2D.
Sorry.
So what we're gonna do now?
Basically let's go over to Circle Collider 2D, and we're going to check this trigger a trigger to true.
Basically we're gonna check that.
So it's gonna check with trigger.
And it's not gonna register our player to pass through it.
So that's why we just check this trigger to true basically checked.
And now we're gonna add a rigid body to the component.
And we'll and we will set that to kinematic.
So it's not gonna fall down due to our gravity.
So let's click on this add component and let's find Rigidbody 2D by typing.
And let's attach that.
And let's set the body type to kinematic.
So we're going to move over to rigidbody 2D component.
And let's select this body type to kinematic.
So now it's fine.
And collision detection we're going to choose continuous and interpolate to interpolate to making out
smooth.
And now that's fine.
And as you can notice.
And now let's make the animation.
And we're going to make the animation inside this style game object.
So that's why we just created our parent game object.
So the animation not going to be weird.
So that's why.
And inside this Apple icon we just want to make the animation.
To do that we will head over to our project app assets and then sprites folder then fruits folder.
As you can see we have all of this.
So we're going to select from start to the end all of these individual sprite and then drag it over
to top of this Apple icon GameObject in the hierarchy.
Make sure you do it inside this now child, not the parent.
Uh, width.
So we need to place the animation.
So we will go to assets first.
So we will.
Sorry.
Uh, so firstly we will move over to our just really a bit sorry.
So we will go to assets first and then animation and then, uh, other animation.
Let's create a new sub folder for our, uh, this collectible items so we can call that items folder.
So let's right click then new folder.
And let's call this folder to something like item item item underscore animation animation something
like that.
And let's open up this folder by double clicking.
And we have to name the animation.
And we're gonna to name this animation to something like fruit, uh, fruit idol or Apple Idol.
Let's call it apple underscore idol.
And let's click on that save.
And now if we go ahead and select this animator tab and select this Apple icon game object underneath
our Apple GameObject in the hierarchy, and move over to animation tab and then click on this play button.
As you can see, it's playing the animation clip.
But the problem is that animation seems a lot, uh, too much.
You can simply say, uh, it's kind of, uh, seems like slow.
So we're gonna increase that up with our script.
So we're going to create a script, a Apple script for it.
Uh, so what we will do, uh, we're going to move over to our project app, and then we're gonna do
that.
But before we do, let's create our collectible animation clip because we're gonna have two animation,
which, as you've seen, this is our, uh, Apple idol and another animation clip just gonna be our
apple collected basically means our apple just gonna go up in the right direction, and over time,
it's gonna fade out.
To do that, we're gonna move over to our animation tab and let's click on this, uh, click over here
and then click on this Create New clip.
And we have to name the clip what we want to name it.
So I'm going to call it something like Apple underscore collected like that uh collected.
And let's click on the save button in order to create the animation.
And we're gonna move over to something like, uh, two seconds.
Uh, something over here, um, uh, 120 frames, something like that.
And, uh, make sure you click on this red button to capture the animation, else it's not going to
work.
And also make sure you select the child one, not the parent one.
So now what we're gonna do, we're gonna drag it up and we will go to a sprite renderer component in
the inspector, then color click there and in the bottom you will see a and just set it to zero.
So what time it's going to fade out as you can notice.
So if we just go ahead and then click on this play button.
As you can see indeed our apple is just going up.
And over time it's just spinning out.
And once you satisfy make sure you just click on this red button in order to capture the animation.
And if you just leave it like that, it's automatically just going to capture the animation clip.
And now we're going to create a script for our idle.
So if we choose, we just want to make our animation a little bit fast.
So let's create a script for it.
So we will go over to our project tab assets and then script folder.
Let's right click then create and then Monobehaviour script.
And we can name that script to something like Apple.
So Apple or collectible let's call it instead Apple I guess I am spelling it correct or I don't know.
So let's let's do that.
And we hit enter.
And we're going to open up this script by double clicking on it.
And we're going to attach this script to our Apple game object and and make and our animation is sitting
on basically the animator component is sitting on uh, into this uh, Apple icon tile game object.
So what we need, we need a reference to that animator component of this Apple icon, because we created
the clips inside this Apple icon.
So that's why.
So we need a reference of this animator component inside this Apple icon.
And after that we can just do the animation.
So just wait a little moment.
And once it's done we can just go ahead and we can do that.
So just wait a moment.
And if I select my apple icon And the inspector in the hierarchy.
Then go over to Inspector.
As you can notice, the animator component is sitting on inside this Apple icon child game object.
So we need a reference of this animator component to do that.
Let's go to top up.
So let's move over to script and let's go to the top of our class.
And let's make a serialized film.
Serialized field sorry serialized field.
Private private.
And the component we're looking for is animator.
So we're going to type this animator.
And we can call this one something like animator.
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.
And underneath our Apple script we will able to notice a animator component just going to pop up a underneath
our Apple script.
So what we need to do, we need to drag this Apple icon GameObject into that slot because the animator
component is sitting on inside this Apple icon game object.
So if I select my Apple GameObject in the hierarchy.
So we need to attach the script.
So let's go over to script folder in our project app.
And let's select this Apple Script and drag it over to Add Component section in the Inspector.
And make sure you attach that in into your parent game object of Apple, not the child one.
And now we can see underneath our Apple script, we can see an animator component.
And we'll just pop up and it says none animator.
So what we're going to do, we do know that animator component is sitting on this Apple icon game object.
So we're going to select that and drag it over to that.
And now as you can notice we have the Apple Icon Animator.
And now we're just going to speed up.
To do that let's open up our Apple Script by double clicking on it.
And we're gonna move over to this start function.
And we do know that a start function get call in the start of the game.
So in this start of the game, what we want, we just want to speed up our animation of that Apple Apple.
And to speed that up we basically need to move over to animator component.
And then we're going to access dot speed.
And we can set the speed that we like.
So let's make a variable for our speed.
And we're gonna adjust underneath our inspector inspector underneath our Apple script.
So let's go to top of our class and let's declare another variable serialize fail private.
And this is going to be float.
And we can simply call this one speed animation speed.
So let's call it animation speed animation speed.
And by default I'm going to set something around 1.5 F.
And let's close that up.
Semicolon.
And now let's move over to this animator dot speed basically accessing the speed.
And we're going to set our animation speed.
And let's close that up with semicolon.
So in this start of the game it's gonna be animation speed, which is by default we set 1.5.
So our animation speed just gonna be a little bit high.
So basically what we're doing firstly we're going over to this Apple icon GameObject and going over
to this animator component of this Apple icon.
And then we just accessing the speed parameter of this animator component.
And we just set it equal to our animation speed.
So basically that is the thing we're doing.
So now let's move over to our scape.
And we're going to press down Ctrl s in order to save our scape.
But before I save I'm going to remove this update function since we don't need and also this comment
right over here to just make our, uh, you can simply say our code a little bit clean and make sure
you do this practice because it's, um, just going to make your, uh, code clean.
And we're going to press Ctrl control S to save our skip.
And now let's move over to unity.
So we will able to notice underneath our apple strip another field just gonna pop up.
And we just name that as animation speed.
And by default we have given 1.5.
But you can just go with any number that you like.
And now if I select my Apple GameObject in the hierarchy then go over to Inspector, then Apple Script.
We can notice we have the animator component as well as animation speed.
So if I just go ahead and then click on this play button in the top, you will be able to notice the
animation speed of this apple.
Just going to be a little bit faster.
As as we saw earlier, it's going to be a little bit faster that of you can simply say of that.
So if I get out of play mode and if I set this animation speed to something like two, and then click
on this play button in the top.
In order to play our game, you're gonna notice that our animation is gonna be a little bit faster than
previous speed because we just set the animation speed to two.
So just wait a moment.
And indeed, animation is just doing fine.
And if I select my animator tab and then select this Apple icon, we cannot see.
So let's get out of play mode.
And if you just get this type of weird behavior, basically means if you select the game object and
you not able to notice your animation clip animator there, then what you can do is you can simply right
click on this animator tab, then click on this close tab and make sure you just go to window in the
top bar.
Then in the bottom you can see animation and the animation is control six as well.
We have the animator so we can simply click on it and we can just toggle it right over here.
And now if I select my Apple icon and by now we can see our Apple idol as well as Apple collected.
So we need to make transition between this animation clip.
So let's select the parameter.
Let's click on this plus icon.
And we're going to choose a trigger.
And we're going to call this one something like collected.
So let's call it collected collected.
And we're going to choose that animation.
So let's make transition from this idle to our Apple collected.
And make sure you select your Apple Game object.
Basically the child one underneath your Apple Game object in the hierarchy.
And then go over to this animator tab.
So let's right click on this Apple idle and make transition to Apple collected.
And and we will select our arrow.
Basically the transition.
Let's go over to Inspector.
And we're going to uncheck the has exit time and transition duration.
We're going to set it to zero because as soon as we collect this apple we just want to play the Apple
collected animation clip right away.
So that's why I just set the transition duration to zero as well as I just uncheck the has exit time.
And now if we scroll down we need to choose the condition.
So we're going to choose this collected trigger condition.
So let's click on this plus icon.
And we're going to choose this collected.
And now also we don't want to play that Apple collected animation clip over again and again.
So we're gonna uncheck the loop.
Time for it.
What we need to do we need to move over to our project tab assets and then animation folder.
And inside our animation folder we created this items animation sub folder.
As you can notice.
And we can see both of these animation clip.
We have the apple idle as well as Apple collected.
So we're gonna choose the apple collected.
Then go over to Inspector and we're gonna uncheck the loop time.
And now if I select my animator tab and then click on this play button in the top and try to trigger
this collected parameter manually, you will able to notice that Apple collected animation.
Just gonna play right away as soon as we're gonna trigger this collected parameter, because we have
chosen that condition, so that's why.
So now if I manually try to trigger this apple collected, as you can see, it's played the, uh, Apple
collected animation clip right away.
And that's pretty much cool.
So I hope you get the idea that how you can do it and we're gonna get out of play mode and make sure
you, Apple, make sure you select your apple and turn it into prefab.
So you're gonna have the copy and you can later on use the copy and just place it around your level.
So I'm gonna move over to my uh, console tab first.
And I'm going to create this messages.
And I'm going to select my project tab, then assets and then prefabs folder.
And let's select the Apple GameObject in the hierarchy.
And let's drag it over to inside this prefabs folder in order to turn it into a prefab.
Or you can simply say copy of it.