We also need to destroy our arrow.
Once it hits our player game object.
So let's open up our arrow script.
So for to do that I will head over to my project tab assets and then prefabs folder.
And let's select this arrow.
Let's go over to Inspector.
And let's double click on this arrow script to open it up in Visual Studio.
For that we're gonna use Ontriggerenter 2D function.
Because if we move over to unity and select this arrow, we can see and then move over to this box Collider
2D.
We can see that uh, is triggered.
We just set it to checked.
So that's why we're going to use Ontriggerenter 2D function in order to check if that arrow just triggered
with this player game object or not.
And also make sure you assign your player game object as a player tag.
If you haven't, then select your player game object in the hierarchy.
Then go over to Inspector any tag.
As you can see it says untagged, so just click there and then click on this player.
Now we assign it this player as a player tag.
As you can notice in the inspector.
And make sure you also move over to overrides and then apply all.
And now we're going to open up our arrow script.
So let's head over to Arrow Script and let's use this on trigger.
On trigger enter enter 2D function.
Sorry we need to use this on trigger on trigger trigger enter 2D function.
So this function get called when we trigger with something.
And we can extract information.
What we trigger it with.
By going over to this collision a parameter we can extract information like name of the game object.
We trigger it with the tag of the game object, anything we like.
So over here what we're gonna do we're going to check.
So let's make if condition some first bracket and some curly brackets.
And what we want to check.
We basically want to check if we trigger with that player game object or not.
So we're going to use this collision dot game object and collision dot object over here basically means
the game object we trigger it Wait, so the arrow.
Definitely.
Definitely gonna trigger with that player game object.
So collision dot GameObject over here basically means the player game object.
And now what we're gonna do, we're gonna access the tag of it.
And we will check if the tag is equals equals to player.
If that is the case.
And we do know that we assign it this player as a player tag as you can notice in the inspector.
So we definitely gonna trigger with that.
So what we need to do now we need to destroy our arrow as soon as it's trigger with this player game
object.
But before we do also we need to destroy.
We basically need to damage our player.
For that, we're gonna print out a message something like debug dot log.
And we say, we're gonna say in quotation something like damage, uh, damage and the name of the player.
So I'm going to use this plus icon.
And top of that I'm going to use this collision dot GameObject dot name.
So top of our this message damage, we just adding the name of the game object we trigger it with.
Wait.
So the message should be pop up.
So this will be damaged.
Collision GameObject basically means player and we accessing the name.
So the name is player.
So this over here becomes player.
So the message should pop up player damage.
Player takes damage.
So let's simply say something like player uh sorry uh player uh let's say takes damage.
Uh, or we can simply say, let's put this damage, uh, put this damage over here.
So let's use this plus icon in order to add the message.
And we're going to say takes damage, takes damage.
And let's close that up with semicolon.
So this will be player.
So the message should be pop up in our console tab should be something like this player takes damage.
And after that we want to destroy our arrow.
So destroy we're going to use this destroy function and destroy function as a first argument.
It's going to take the game object we want to destroy.
So this arrow script is attached to our arrow game object.
So we want to destroy that arrow game object basically means the game objects are a game object itself.
So that's why we're going to pass this this dot game object.
And then we're going to close that up with semicolon.
So the game object will be destroyed right away because we didn't given any second argument.
As you can see we didn't as specified.
So we just destroying our arrow game object right away.
Or you can simply say immediately after printing out this message says player takes damage.
So now press down Ctrl S to save our script and let's head over to unity.
So as soon as our arrow is just going to trigger with this player game object, the message just going
to pop up in our console tab, say something like player takes damage and after that that arrow game
object just going to destroy itself right after triggering with this player game object.
So let's have a look on this and you will see that.
So just wait a little moment and once it's done we can see that.
So just wait a little moment.
It's gonna take a few seconds to compile our script.
Once it's complete, we can just go ahead and play our game in order to check that out.
So just wait a little moment.
It's, uh.
So basically, I have low system, so that's why it's taking a little bit longer time.
So now, as you can see, the compilation is done.
I'm going to select my console tab.
And let's click on this play button in the top in order to play our game.
So as soon as we're gonna get attack range of this enemy game object, you will see this enemy just
gonna shoot out ray array on top of this player game object.
So let's have a look.
So if I get closer to this, uh, as you can see, as soon as our arrow just hitting our player game
object, it just destroying itself, as well as the message in our console tab.
Just getting pop up says player takes damage, since the name of the game object is player.
As you can notice in the hierarchy as well as in the inspector.
So I'm going to get out of play mode, so I hope you get the idea.
So we're going to leave the message for now, because we're going to use that later in order to damage
our player.
So we also need to give some health amount to this.
You can simply say this enemy game object in order to take damage.
So I hope you get the idea.
Now I'm going to press Ctrl s on my keyboard in order to save my project.
And if you change anything inside this player, game object, or enemy one, make sure you just go over
to Inspector Overrides and then apply all.
And also you make sure inside this player as well.
And after that you can simply press down Ctrl S in order to save your project.
Or you can simply move over to top left file and then click on the save button in order to save your
project.
So I hope you get the idea that how you can do it.