Let's make our player shooting.
So we're going to select our player GameObject in the hierarchy.
And let's open up the player script by double clicking on it.
So what we basically want to do, we just want to shoot basically means if we press star, if we click
left mouse button, then we want to shoot.
Uh.
So we're going to do that.
So let's open up our player script quickly and let's move over to this update function.
Because we need to check every single frame.
If we just uh, click left mouse button or not.
So I'm going to do it in the bottom somewhere over here in update function.
You can do it anywhere you like.
It doesn't need to be in order.
And we're going to check for some inputs.
We're going to use this if condition.
Then make first packet and some curly brackets.
And what we want to check over here.
We want to check for uh input.
And we're going to check for left mouse button.
So we're going to use this input uh dot get key function.
Sorry dot get mouse button function, mouse button function and make this first brackets.
So if you pass in this get mouse button mouse button down function one it's going to check for the right.
Are you right click on mouse button.
But we want to check for left mouse button.
If you want to check for right mouse button then you can pass one.
It's basically means that we're just clicking the right mouse button.
Input dot get mouse button down.
One basically means we just clicking the right mouse button, but I want to check for left mouse button.
So that's why instead of one I'm going to pass zero.
It's basically means we're just clicking the left mouse button.
So what we want to do we just want to shoot.
So we're going to make a another function for our shooting.
So we're going to make it somewhere over here.
And for function we're going to type void.
And we have to name the function what we want to call it.
So I'm going to call it something like shoot and then make first bracket and some curly brackets.
And what we want to do inside this function.
For now, let's print out a message in the console, say something like, uh, shoot.
So we're going to type debug dot log for printing out message and then make first bracket.
And then close that up with semicolon.
And make sure you in make sure you just pass the string in this debug dot log function.
So make first make quotation for string we need to type quotation.
And we have to put a message that we really want to see underneath our console tab.
So I'm going to say something like player shoot, player shoot.
And what I'm going to do I'm gonna call this function over here.
So if input dot get mouse button down zero basically means if we're clicking the left mouse button then
what we want to do, we're going to call the shoot function.
And what should function is really doing is just printing out a message in the console says player shoot
as you can see.
So let's call the function over here, which is shoot and make first bracket, then close that up with
some my colon.
So whenever we click the left mouse button this should function will call out.
And this should function basically doing this thing which is printing out a message in the console says
player should as you can see.
And now we're going to press down Ctrl S to save our script.
And let's move to unity and have a look on this.
So whenever we're going to click the left mouse button that message should pop up right away.
So just wait for a little moment and we're gonna have a look on this.
And we're going to select our console tab in order to see our message.
So I'm going to select my console tab.
And let's click on this play button in the top to play our game.
And whenever we're going to click the left mouse button that message just going to pop up right away
says Player Shoot.
So let's have a look on this.
So if I click left mouse button, as you can see, as soon as we're clicking the left mouse button,
the message in the console just getting pop up saying a player should, as you can see in the inspector.
So I'm going to get out of play mode instead of printing out message what we really want to do, we
just want to shoot.
So let's double click on this message in order to open up in Visual Studio quickly.
And instead of printing out message, we just want to shoot.
So what we're gonna do, we're gonna shoot out a raycast from this player game object, top of this
gun from this top of this gun of this player game object to the right direction.
So whenever we click left mouse button, then we're gonna shoot.
Basically we just want to shoot a raycast or a ray to the right side.
So we're going to do that.
So let's uh move to script.
And over here instead of printing out message we're going to shoot out a ray.
So we're going to use this function.
It's called physics 2D, so use this physics 2D raycast.
So this physics 2D dot raycast.
It's going to shoot our ray.
And as a first argument it's going to take the position from where you want to shoot out.
So we're going to shoot out from the the top of this gun of this player game object to the right direction.
And the second thing it's going to take the direction.
And the third thing it's going to take the length of the ray.
So we're going to also define the length of the array.
We can simply say.
And the fourth argument it's going to take the layer mask basically which game object we want to target.
So we're going to target enemy game object for now.
As you can see we don't have enemies at all.
So for now we're gonna uh basically check with something else.
Uh, you can simply say, so what we need, we need those variable.
So let's make this, uh, for now, comment by double slash, and let's go to top of our class, because
we need to gather those variables.
We do know first thing we need a position where we want to create our.
You can simply say the array or gonna shoot out our array.
So let's make a public a public that will be transform.
And we do know that this transform component is responsible for position rotation and scale.
So that's why we because we need the position.
And this transform gonna also have the position.
So that is the position that is the thing.
And we have to name and name it.
So I'm going to call it something like shoot point or shoot position.
We can simply call it.
So I'm going to call it something like shoot point.
And the second thing we need our direction in which direction.
So we're going to shoot out the array to the right direction.
So we don't need to put a variable for that.
We're going to use vector two dot right.
It's basically means we're going to shoot to the right direction.
So we can just ignore that parameter.
But we need as a third argument the length of the array.
So we're going to define a length as well.
So let's go to script and we're going to make another public.
And that will be float.
And we can call this one something like length.
Or we can simply call it distance.
And by default I'm going to give it a default parameter, something like uh something like five f or
five unit distance.
And the third argument we need layer mask basically which game object we want to target.
So we're going to target our enemy game object.
So we need to assign our enemy as a enemy layer.
But for now let's uh type the variable.
And we're going to take a look on that later.
So we're gonna type public layer mask.
And we can call this one something like attack layer or target Layer.
Or we can simply call it what is enemy.
What is enemy?
Something like that.
Or you can simply call it something like attack layer or target layer as well.
It's basically totally up to you.
And we're going to close that off up with semicolon.
Let's press down Ctrl S to save our script and move over to unity.
And we need to assign those variables one by one.
So firstly we need to create a point from where we want to shoot out the array.
So I'll create an empty game object to this player game object.
And we're going to move it top of this gun of this player game object.
So as soon as our player gets compiled we can see underneath our player script in the inspector should
point distance.
And this word is enemy.
It just gets gets pop up.
This this variable this variable just gets pop up.
So we need to assign one by one.
So let's create an empty game object.
And we're going to move it top of this gun.
So we're going to shoot out the array from that point or from that position.
So let's select our player game object.
Let's right click on it.
Then create an empty game object.
And you can call this game object to something like shoot point or fire point.
But I'm going to call it something like shoot point.
Shoot point.
Then go over to Inspector.
As Inspector, if you want to choose an icon, then click over here and I'm going to choose this gizmos
and select the move tool from this scene view.
And we're going to move it top of this gun.
Something over here I guess this is going to be perfect.
So perfect.
And this is awesome.
And this is okay.
But you can just play around with this.
And it's basically totally up to you.
And now what we need to do now we need to select our player game object.
And let's select this shoot point and drag it over to this shoot point field underneath your player
skip and the distance.
By default we set five basically means the length of the ray we can increase as well.
So I'm going to set it to something like ten.
And what is enemy.
And it says nothing.
For now let's create a test game object.
And we're going to assign that game object as a enemy layer.
So let's do that.
So move over to hierarchy.
Right click or hit this plus icon.
Go to 2D object as sprite.
And then we're going to choose this square.
And I'm going to call this square to something like enemy.
So I'm going to call it enemy.
And make sure you attach colliders to this square or to this enemy game object as well.
So it's going to detect our collision to do to do so let's click on this add component.
And I'm going to add a component called Box Collider 2D for collision detection.
And we added Box Collider 2D.
And I'm going to move this one something over here.
And in the Y I'm going to stretch a little bit.
So let's go to Transform Scale in the Y.
I'm going to set it to something like five.
And this seems perfect as you can see.
And we're going to assign this enemy game object as a enemy layer.
So how you can do that.
You need to select the game object first in which game object you want to do.
So in our case, this anime is a game object.
For now, we just, uh, use this square and make sure you just go to Inspector in the top right you
can see layer and it says default.
So just click there and then click on this add layer.
And we need to create a new layer.
So I'm gonna uh create a anime.
So I'm going to type anime anime and hit enter to save it.
And go back to that game object one more time.
Basically select the game object in the hierarchy one more time, then go to Inspector and Layer.
As you can see, it says default.
So when I click there and we're going to choose this anime, now we assign it this anime as a anime
layer, as you can see in the inspector.
So make sure you just do that one more time.
Basically, you need to select the game object and you need to assign with the layer which you recently
name it.
So make sure you just do that.
And now we're going to select our player game object.
And let's go to our player script.
We can see what is anime.
And it says nothing.
So the array should be only detect this anime game object, or we only just want to damage this anime
game object.
So that's why.
What is enemy or as a layer mask?
We're going to choose our enemy.
You can choose multiple at once as well by clicking.
But, uh, I'm gonna choose this enemy.
Or I just want to damage this enemy game object only.
So that's why.
What is enemy as a.
I'm gonna choose enemy only, so it should be nothing.
So you need to, uh, select the or select the layer in, uh, in which layer you just want to damage,
or which game object you want to damage.
But before it, make sure you assign that game object with some certain layer.
And now we're going to select our player game object.
Let's go to overrides and then click on Apply All.
So the changes we made over here it's going to be saved into prefab of this player game object as well.
So now let's open up our player script so that we only going to check for this enemy or this square
game object.
And we do know we assigned this square or this anime game object as a anime layer, and we have chosen
underneath our player skip as well.
So let's go to Skip now.
And what we're going to do, we're going to move over to this physics 2D dot raycast.
And let's remove those, uh, forward slashes.
So the first thing it's going to take the suit position or from where we want to shoot out the ray.
So I'm going to type this shoot point.
Shoot point sorry shoot point dot position.
Uh so which basically just moving over to this shoot point and then we just accessing the position.
As you can see, we can see in the inspector.
And the second thing it's going to take the direction.
So we just want to shoot out in the right direction.
Uh right direction.
Something like that.
So that's why we're going to pass transform Dot.
Right.
Or we can simply pass a vector to as well.
Uh, you can think of it like this.
So we are going to gonna use this new vector tool and make this first bracket.
And we do know that this vector two is gonna have x axis and the y axis.
So we don't want to shoot in the y axis at all.
We want to only want to shoot to the right side or to the right.
Or you can simply say in the X position we don't want to shoot it in the Y.
So that's why in the Y we're going to put zero f.
And we do want to shoot out in the right direction or in the x axis.
So that's why we're going to put one f in the x axis.
And if you put minus one then it's gonna move.
Uh that should uh or the raycast will uh, are just gonna be in the left direction.
But we don't want to do that.
So that's why we just pass this positive one f or plus one f, you can simply say, or you can simply
use this vector to vector to vector two dot.
Right.
Means, uh, the same thing x in the one and y in the zero.
You can simply use that or you can use this one as well.
Transform dot right.
Basically means, uh, the direction the player is facing.
So as you can see, player is facing the right direction.
So transform dot right basically means the direction the player is facing.
So it's going to be somewhere like this to the right direction.
So basically that is the thing.
So I'm going to pass transform dot.
Right.
Or you can simply pass Vector2 dot right as well.
And the third argument comma, it's going to take the distance or the length of the array.
So I'm going to pass my distance and distance I set in Inspector.
Uh as you can see ten in the inspector.
So the length of the array and the fourth argument, it's going to take the layer mask or which game
object we really want to target.
So comma, as a fourth argument I'm going to pass what is enemy.
And we do know what is enemy.
We have chosen enemy game object or that enemy layer that we want to damage.
So I'm going to pass My word is what is enemy and make sure you just close that up with semicolon.
So this function physics 2D dot recap is gonna create the ray from this root point dot position in the
right direction.
With this much of length.
And that ray is only gonna damage our enemy.
As you can see.
And we just pass what is enemy as a what is enemy?
We only have chosen that enemy game object or that square.
I as you seen.
So this ray can be true or false, depending upon the ray is colliding with this enemy game object or
not.
So let's suppose that we just created the ray from this position.
So if the ray collide with this enemy game object, then this function over here, it's gonna give us
a true value.
Or, or else it's going to give us a false value.
So what we're going to do, we're going to store the result into a another raycast hit 2D parameter
or a container or a variable you can call.
So what are we going to do?
We're going to type this raycast.
Raycast hit 2D variable that basically type of the variable.
So we do know this is a ray.
So that's why we need to type this raycast hit 2D.
And you have to name the variable or the parameter that you want to set with.
So I'm going to call it something like hit info.
Or you can simply call it call info or the hit information.
And I'm going to set equal to this.
So this hit info can be true or false depending upon if the ray is really colliding with this enemy
GameObject or not.
If it's then this, uh, just gonna give us a true value.
So that's why this hit info is just gonna be a, uh, basically true because we directly sending it,
as you can see, raycast hit 2D info, uh, sorry, raycast hit 2D, hit info equal to this.
So if the ray is really gonna collide with that, uh, enemy game object, so this hit info gonna be
true, or else it's gonna be false.
So what we're going to do in the next, uh, in the next, we want to check if this heat info is true.
Basically means if the ray is really, uh, colliding with this anime game object.
So what we have to do, we need to damage this anime game object because the ray is just gonna collide
with this anime game object.
So we're going to check over here.
Uh, so we're going to use this, uh, if condition, make first bracket and some curly brackets and
what we want to check here, we want to check if hit info is true.
Basically means the ray is really colliding with this enemy game object.
So we're going to check if hit info is equals.
Equals to true means the ray is really colliding with that, uh, anime game object.
Or you can pass simply like this.
If hit info, it basically means true and means our ray is colliding with that enemy game object.
So what we want to do, we want to damage that enemy game object for now.
Uh, later on we're going to give some help to our enemy.
Game object.
And we're going to subtract.
For now.
We're going to print out a message for printing out.
We're going to type this debug dot log function.
And this over here it's going to take the string or the message you want to see.
So what we're going to do we're gonna go over to this hit info parameter.
Basically we're gonna take this parameter.
And by this parameter we're going to access the name of the game object the ray hit width.
So if hit info is true basically means the ray really colliding with this enemy game object.
So what we're gonna do, we're going to go we're gonna move over to this enemy game object, and we're
going to access the name of this enemy game object.
So we're going to move over to this hit info, as we do know that we're storing all of the information
basically this ray into this info.
So that's why we can extract any information about the ray with which we hit with the ray.
So we can extract info like name, component, even the tag of the game object.
So we're going to use this parameter to do that.
So we're going to use this hit info transform dot name.
So basically hit info dot transform basically means the array.
Let's suppose the array collide with this enemy game object.
So this enemy game object.
So hit info dot transform basically means we're just going over to this uh game object which is enemy.
So we just moving over to this enemy game object and then we accessing the transform of it, as you
can see in the inspector.
And by this transform we just accessing the name of the game object.
So in our case we can see we just name it this square as enemy game object.
So basically this hit info transform dot means the name of the enemy game object.
And top of that what we're going to do.
So make sure you just add this plus icon if you just want uh want to add any custom messages then make
this a quotation and you can put whatever message just you want to say.
So I'm going to pass octopus takes damage or something like that, and make sure you just close that
up with semicolon.
So hit info dot transform dot means name basically means we're just going over to this anime game object
and we accessing the name so we can see we assign it this anime anime game object as an anime name.
You can see in the inspector and top of that what we added a message, we added text image.
So this is gonna be enemy.
So enemy takes damage message.
Just going to be pop up in the console right away once we hit.
As you can see when we click the left mouse button, then we're calling the shoot function and shoot
function.
Firstly creating the array.
And it's checking if the array is really colliding with this enemy game object or not.
If it's then what we're doing, we're just printing out the message in the console.
So this hit info dot transform dot main.
Basically name is just gonna be enemy.
So this is gonna be enemy.
And on top of that we just add a custom message text damage.
So enemy takes damage message.
is just going to pop up right away, and we're going to press down control S in order to save our skip.
And let's move to unity.
So right after once we click our left mouse button, the rage is going to create from that short point,
and it's going to shoot out to the right direction.
And if the ray is really colliding with this enemy game object then in the console enemy takes damage
message just going to pop up right away.
So now if we click on this play button in the top and we can shoot.
So I'm going to get out of play mode.
So I'm going to click on this play mode in the top to get out of it.
And I just want to make my player game object a little bit slow.
Uh for testing purposes you can say.
So I'm going to select my player game object.
Let's go to Inspector then player skip.
And I'm going to make the speed something like 2.5 to make it a little bit slower in order to checking
purposes.
So let's click on this play button now in the top.
And let's have a look on this.
So if I just click left mouse button as you can see the message.
So I'm going to click on this pause button in the top to pause my game.
And we can if we move over to console tab we can see anime takes damage.
Just gets pop up y.
Basically we're just going over to the game object the Ray hit with.
So in our case we definitely gonna hit with that anime.
So we just accessing the name.
So the name is anime.
And top of that we added a custom message says takes damage.
So this just going to be anime enemy takes damage.
So that's why the message in the console just gets pop up.
Says anime takes damage.
I hope you get the idea that how you can do it and I'm gonna get out of play mode.
And also I'm gonna clear my console tab.
So let's go to console tab and then click in this clear in the top left in console tab.
You can see in order to clear all of the messages.