Let's code our game.
Basically, once this player game object collides with this heart, it need to be collected.
Also, this heart need to play the heart collected animation clip.
So we're going to do that.
So before we do, uh, if we select our heart sprite and then move over to animator tab, we can see
we have two animation heart idle and Heart Collected.
And we need to trigger this collide parameter.
And we do know once we trigger it's going to play the heart collected animation.
And we're going to trigger this collide parameter through our code.
Once our player GameObject collect this heart game object.
So we're going to do that.
So let's select our player GameObject in the hierarchy.
And let's open up the player script by double clicking on it.
And we're going to use Ontriggerenter 2D function basically this one.
So this function get called when we trigger with something.
And you can just ignore these things over here and make sure you just include this function in your
class which is On trigger.
Enter 2D function and this function get called when we trigger with something.
So if I select my heart gameobject in the hierarchy, then move over to Inspector Polygon Collider 2d.
We can see we just take this trigger box.
So that's why we're going to use that on trigger enter 2D function.
So that function get called when we collide with something on when we trigger with something.
So let's suppose this player game object trigger with this heart game object.
Then this function over here just gonna get trigger.
And we can extract extract information about what we hit with.
So by moving over to this collision parameter we can extract info something like a name of the game
object.
We trigger it with the tag of the game object or the component that we're looking for.
So we can just extract info about that.
So what we're going to do now, uh, basically we're gonna check if we trigger with this heart and if
this game object has a tag called heart.
And if we select our heart GameObject in the hierarchy.
We can see in the Inspector tag, we assign it as a heart attack.
So we're going to trigger with this trigger with this.
So in that situation what we need to do we need to increase our health amount of this player game object
by one unit.
And after that we need to play the heart animation heart collected animation clip.
And also we need to destroy that heart heart game object once we collect that.
So let's do that.
So we're going to move over to script.
And over here we're going to check.
So this function get called when we trigger with something.
And what we're going to do over here we're going to check if we make some first bracket and some curly
brackets and what we want to check over here.
We just want to check the game object we triggered it with.
We can we're going to extract the tag of the game object.
So we're going to use this collision parameter in order to do that.
So let's go to collision parameter.
So I'm going to use this collision dot sorry.
We're going to use this collision dot game object.
Sorry I'm really apologize for it.
Game object dot tag.
And we're going to check if it's equals equals to heart.
If that is the case means we're just colliding with the heart.
Uh, means you need to put inside this quotation.
So make sure you just do that.
So collision dot game object basically means the game object we created with.
So if this player game object trigger with this heart game object or collide with this heart game object.
So the collision game object right there, this heart game object.
And what are we doing after that.
We just extracting the tag of the game object.
So we basically moving over to this heart game object and then accessing the tag of it.
So we can see we assign it with heart tag, as you can see in the inspector.
And we're checking if the tag is equals equals to heart.
And definitely we're going to trigger with that heart game object.
And we assign it that heart as a heart tag.
As you can see in the inspector.
So we're definitely gonna trigger with this heart game object.
So in this situation, what we need to do, we need to increase our player max health by one unit.
And after that this heart game object need to play the heart animation clip.
Sorry not heart.
The collected animation clip of this heart game object.
So let's do that.
So we're gonna are just collecting over here as you can see.
So we're checking the tag of the game object.
If it's equals equals to heart means we basically trigger it with heart game object.
So now we're going to increase our max health.
So the max health we set by default.
Uh you can see we have it over here as you can see.
So we're gonna increase this max health by one unit.
So let's do that.
So we're going to do it over here.
So the max cell so max cell.
And top of that I'm going to add.
So we're going to use this plus equal in order to add.
And we're going to add one unit of health.
So I'm going to put one.
And after that, what we need to do.
We also need to play the hurt animation clip.
The collected animation clip of this hurt game object.
So we we do know that if we select our hardest sprite in sprite, we can see a animator component is
attached to this sprite game object.
So basically what we have to do, we need to move over to this hurt game object first.
And by this we're going to move over to this transform of this hard game object.
And by this transform reference we're going to access the first child so hard sprite right over here.
It's going to be the first child or the first.
So the index is just gonna be zero.
And uh, and we do know that, uh, animator component is attached to this sprite game object, not
this hard game object.
So that's why we need to move over to this sprite game object or the or the child of this hard game
object.
Then we're gonna move over to this animator component of this hard sprite.
And by getting this animator component, we're going to trigger this collider parameter.
And we do know once we collide it's going to play the heart collected animation clip right away.
So let's do that.
So over here what we're going to do we're going to use this collision.
So we will go to collision dot GameObject means we basically move over to that basically this heart
game object.
And now what we're going to do we're going to access the child of this heart game object because the
animator component is attached to this heart sprite, not this heart game object.
So you can see in the inspector as well.
So that's why we're going to access the first child.
So we will move over to transform.
And then we're going to use this dot get child function.
And over here get child function in brackets.
It's going to take the index of the child.
So the first child so hard sprite we can see we have only one child of this heart game object.
And the first child is just going to be index zero.
So if you just pass right over here zero in this gate cell.
So it's going to reference the first cell.
And if you let's suppose pass one what basically means the second child or the second child index is
going to be one.
But we don't have any second child as you can see.
And the animator component is attached to the first first child.
So that's why in this brackets I'm going to pass zero zero basically referencing the first child.
And after that what we're going to do, we're going to go to that sprite game object.
So we're going to type this game object.
And now we're gonna move over to transform sorry animator component.
So we're going to use this dot get component function.
Not sorry this is not the function.
Or you can simply say uh sorry.
It's uh basically use method group.
So you can see uh what what basically is this.
So over here what we need to do, we need to pass the component that we're looking for.
So the component we're looking for is a animator component.
So firstly, what are we doing.
We just moving over to this hard GameObject first.
So collision dot GameObject basically means the hard GameObject.
And after that we just moving over to transform.
So firstly we just moving over to this hard GameObject, then this transform component of this hard
GameObject.
And by this transform we just accessing the child of this hard GameObject, which is hard sprite as
you can see.
And after that what we're doing, we're just going over to this sprite and then we just moving uh,
we basically just getting this animator component, as you can see in the inspector.
So in this arrow we need to pass the component that we're looking for.
So we're going to pass animator component animator component.
And we're going to make first bracket.
And now we're going to use dot set trigger function in order to trigger that collide parameter.
So we're going to use this set trigger function.
And this sorry and uh dot set trigger function.
And this trigger function in the first bracket in basically the first bracket is in quotation.
It's going to take the parameter name or the parameter that you want to track a trigger.
So we using this trigger function basically.
Why.
Because this parameter in animator tab we can see this is a trigger parameter.
So that's why we need to use this trigger function.
And this bracket.
It's going to take the parameter name.
So make sure you type quotation and spell it correct.
Basically you need to pass the parameter name.
So in my case collide.
And make sure you spelling it correct.
And you can check that by moving over to unity.
And in animator tab you can check.
So I can see the C is capitalized and all other letter is small letters.
So make sure you just type spell it correct.
And after that we're going to close that up with semicolon.
So firstly we're going to collision dot GameObject basically means heart.
And then going over to transform.
And by this transform which is going over to the first tile and then we're just getting the animator
component.
Then we're just triggering the collide parameter.
So basically we're moving over to this hard GameObject first and this hard, uh, GameObject transform,
which is accessing the first tile or which is moving over to this sprite game object, or the first
child of this hard GameObject.
And after that, we're just getting this animator component, as you can see in the code over here.
Basically we're just going over to this animator component.
And by this animator component we're just triggering this collide parameter.
You can see in the animator tab.
And we can also notice in our code over here which is triggering the collide parameter.
So I'm going to press down Ctrl S to save my script.
And let's move to unity by now.
And let's have a look on this if it's perfectly working or not.
So we're going to take a look on it.
So just wait for to compile and once it's done, we are ready to play our game and we will see our max
health just going to increase by one unit.
And I'm going to drag away my enemy a little bit further away for testing purposes now.
So I'm going to select my enemy two GameObject in the hierarchy.
And I'm going to drag it a little bit far away from my player GameObject something uh, somewhere over
here.
And this seems pretty much fine.
And if I select my player game object in the inspect hierarchy in the Inspector, we can see underneath
our player skip max health is five.
So once we collect this one or collide with this hurt, we're going to see this max health just going
to increase by one unit.
So let's click on this play button in the top.
And we're using plus equal sign in order to uh add.
So you you you just do that uh plus equal one which just gonna increase.
So let's collect this heart game object And as soon as we collect it, as you can see, it just splits
the heart.
Animation clip.
And after that we can see the max health of our player.
So I'm going to click on this pause button in the top in order to pause my game.
And we can see our player Max health is now six.
So basically that is working fine.
But the problem is that if we move over to hierarchy and if we just take a look, we can still see the
hard game object is laying around in the scene view as well as in the hierarchy.
So after collecting this, what we basically need to do, we need to destroy this hard game object right
away because we collect it.
But uh, instead of destroying this hard game object right away, what are we going to do?
We're gonna destroy it after some period of time.
Basically, uh, once we played the collected animation clip 100%.
After that amount of time, we're gonna destroy our hard game object.
We're not gonna destroy it right away.
So firstly, we need to play the hard collected animation clip as basically it's just playing.
It's going up and over time it's just fading out.
So we're going to play that animation clip.
And after completing that animation clip of that hard game object, we're going to destroy that.
So let's get out of play mode.
And if I select my hard GameObject in the hierarchy, then move over to animation tab.
Uh, sorry, we need to select the child because the we created the animation clip a child of this hard
game object.
So we're going to select our hard sprite or the child game object.
And if we move over to animation tab and click over here and I'm going to choose the hard collected.
And we can see this animation we completing 60 frame.
Or you can simply say one second which is completing the whole uh hard game object.
So sorry, not whole uh, in one second we just completing the whole animation clip.
Sorry, not game object.
So we just completing the whole animation clip after one second.
So after this amount of time, we're going to destroy our hard game object.
So it's not going to lay around in the hierarchy as well as in the scene view.
So let's do that.
So we're going to move over to script.
And after playing the animation clip we're going to destroy that.
So we're going to use this destroy function.
And we do know it's going to take as a first argument what game object you want to destroy.
So I'm going to pass this dot game object.
Sorry.
Not going to pass this dot game object.
This dot game object.
Basically it's just referencing itself.
So it's going to be player.
But we don't want to destroy our player.
Instead we're gonna destroy that hard game object.
So we're going to pass this collision dot game object and collision dot game object basically means
the game object we trigger it with.
So we trigger it with this hard game object.
So collision dot GameObject right there gonna be this hard game object.
And you can just close that up with semicolon.
But it will destroy right away.
So that's why as a second argument we're going to specify our time.
And if you just leave like this it's going to destroy the game object right away.
And we don't want to do that.
So that's why as a second argument we're going to pass the time or we're going to specify the time.
So I'm going to put one if why I just put one because uh, in one second I'm just completing the hurt
collected animation clip, which is going up and over time just fading out.
So once again, it just completing the whole animation clip.
So that's why as a second argument I just pass one f, which basically means one second.
Now I'm going to press down Ctrl s to save my script and let's move to unity.
So after one second you will see that hurt game object will also be destroyed.
After collecting that firstly we collecting and we increasing our Maxwell.
And after that once again we just destroying it right away.
So let's click on this play button in the top in order to take a look on this and let's, uh, see what's
really happening.
So just wait for to compile.
So if we get closer to this hard game object, we can see we increase our health.
And the hard game object also get destroyed.
So I'm going to click on this pause button in the top to pause my game.
And if we move over to hierarchy, I'm not able to see any hard game object laying around in this hierarchy
as well as in the scene view.
Uh, as you can see.
So basically means after one second we're just destroying, uh, right away after, uh, after one second,
basically.
And that is pretty cool.
I hope you get the idea that how you can do it.
So I'm going to click on this play button in the top in order to get out of play mode.
Uh, get out of play mode.
And I can just remove this hard game object from, uh, for now in the you can see in the hierarchy,
but I'm going to leave that.
So let's leave.
Leave it like that.