What is up guys.
Far.
Hear hear from awesome dudes.
Dot com.
We have so far two enemies in our game so we have the beetle and we have the snail we can jump on them
in order to kill them.
Let's add another way to kill our enemies and that is by shooting.
So in order for our player to start shooting we need to create first his bullets so we are gonna go
into sprites and then objects folder and then you have this flame bullet.
It is a sprite sheet as you see.
Let me just click here or set it to sprite mode multiple sprite editor and let us slice our sprite so
click slice and lets see if everything is filed correctly.
This one is this one is and this one is not.
So you see this part right here is sliced in two parts which is not what we on actually three parts.
So lead this part the leads.
Also this part and only leave this one and resize it.
We talked about it sometimes unity does not know exactly how to resize everything so you will need to
do things on your own as we just did.
But in ninety nine percent of the time unit you will know what to do.
So here it is we have 1 2 3 4 or 1 2 3 animations hit apply.
Close the window and we are good to go.
So what do we want to do now is we are going to create the animation from our bullets so take that 1
2 and 3 and drag and drop them right here like this and bam.
Here it is.
We are going to go here in animations create a new folder called Bullet animations and I'm gonna hit
enter what I'm gonna do now is call this animation explode and hit save.
Now before we continue to do anything with the bullet let us create the ideal animation for it so go
into the animation tab and expand this list right here click on Create you clip and this is gonna be
the ideal animation drag and drop this first frame so frame bullet 1 or underscore one drag and drop
in right here and this is gonna be the default state.
Let's go back here and select the animator tab select the flame bullet.
Now you see that the first animation is set to be explode.
So if we run the game known as the bullet is exploding.
This is not what we want.
What we want is to set it to idle so right click on the ideal and click on set as layer deep fault state
and bam.
Here we are so moving forward now we are going to select the frame bullet the flame.
So I'm saying frame but it's flame OK so it's gonna be bullet I'm going to rename it to bullet I'm going
to attach a circle collider why a circle and not a box well because this is like a circle ish game object
it makes more sense to apply a circle and I can resize it so said the radius something like this you
see and it makes more sense that this will be the collider for our flame bullet then to apply a box
collider because this has a circle ish form so we have a circle collider we're also going to attach
a rigid body to D on it.
Now one thing that we're are gonna do with the rigid body is the following We are gonna set it to be
kind nomadic what is going emphatic key nomadic means it will not be affected by gravity.
Now the reason for it is because we are going to move this bullet by using the transform properties
you see here transform which is at the top.
We are going to use the transform property in order to move it.
What am I talking about.
Well for that snail right here we are using the body which is the rigid body and its velocity to move
it forward the player we are doing the same thing.
Let me just find it right here player walk we're using the velocity of the region body in order to move
the player as well but we are gonna use the transform property which is the transform that position
property to move the bullet.
Now when you have a rigid body attached on a game object you want to use the rigid body to move that
game object.
But in our case I'm going to use the transform and these two don't match up.
So if you have a rigid body it is better to use the rigid body to move the game object instead of using
the transform.
But if you do want to use the transform then you said the rigid body you can imagine.
So that way you you can use the transform to move the game object.
Now don't get me wrong this is not like you can't use the transform but when you apply physics to a
game object it is better to use the physics to move it.
But if you still want to use the transform to move that game object then set this game objects rigid
body to keep pneumatic which means physics will not be applied to it.
So gravity will not affect that game object.
Let me show you what I mean.
So when I run the game now known as the bullet fell down bam you see bullet is down.
But when I hit or click on this body type dropdown list which is set to dynamic which means that forces
and gravity will be applied to the game object.
And I set it to keep Matic which means that the gravity will not be applied to this game object and
I hit the play button now.
Bam you see the bullet is standing still.
We are also going to check is trigger check box for the bullet so objective is trigger check box which
will Well then allow us to have on trigger enter in order to detect collision and we are also gonna
go here on top for the tag of the bullet and go ad's tag hit the plus button and I'm gonna name this
two bullet so bullet hit enter.
Select the bullet right here inside of my here Aki and tag it as the bullet.
The last thing before we start shooting is go in player prefabs and drag and drop the bullet here as
a prefab and now we can start coding the first thing that we are going to do is go in the scripts folder
player scripts right click and create a new C sharp script.
This is going to be our player shoot script and attach it on the player of course of drag and drop it
like this.
Attach it on the player select the player where is he and how it applies to that the change applies
to the prefab because we have attached the script double click it to open it in mine and develop you
know me you know what I need to do tag the class given little base Baz I don't even have to repeat that
I'm doing this you already know that.
So what we need here is we need a public game object which is gonna be our fire bullets so fire bullet
and this fire bullet is going to be dragged and dropped now.
So go here in Unity You see we have here an empty slot Come on up here waiting for unity to take mercy
upon me why isn't it appearing so did I see this.
Yes I see it
for some reason it's not appearing.
I can see the circle of that in the bottom right corner.
Well let's continue with the coding and hopefully this will well appear when we end the coding so what
we are gonna do now.
Well we are gonna create a void shoot bullet which is a function that we're gonna call in the update
function.
So void updates like this.
And here we're gonna call our shoot bullet.
Function what we are going to do in our should bullet.
Well we are simply going to check if our input so input get the key down.
What is get key what is get key down what is get key up get key returns true while the user holds down
the key.
So if for example I go here and type get key down key code J like this if I only use get key.
This right here will return true as long as I'm holding this key code the J on my keyboard.
So as long as I'm holding it this will return true.
Get key up will return true when I release that key.
So when I pressed it this code will not be executed.
But when I release it.
So if I hover over it says returns true during the frame to use it releases the key identified by the
key code in parameter which is key code G key code down however if I hover over it it will return true
during the frame the user starts pressing down the key so when I press down the key it will return true.
So essentially when we press down the G key on our keyboard we are going to create here a game object
that I'm going to call bullet and we're going to use instantiate instantiate will simply create a copy
of this game object so you will duplicate it.
Create a copy of it.
So which game object we want to create fire bullet.
Essentially we are telling create a copy from fire bullet at this position which is gonna be transform
position and using this right rotation and quad turn in identity is a shortcut for writing 0 0 0 for
the rotation so we don't need the rotation but it is required because it's a parameter in the instantiate
function.
So we do need to put it right here but we need it to be 0 0 0.
This is a shortcut you see here.
It is a shortcut for that rotation.
Now this is the position where we want to create our bullets so exactly the same position where our
player is now what we need to do here is the following.
So we need to take the bullet and we need to see get component and we're gonna get the fire bullet component
like this.
It's with the capital F and we need to say that's B like this and we're going to multiply.
That equals to by transform Bob a local scale that X and we will talk about this in a second but I'm
going to leave it right here because this will throw an error since we did not create the fire bullet
script.
Let's go in unity and hopefully now we will see here in the inspector panels still nothing appearing.
So let me restarted and go back to you and we are back and you'll see that even though some big companies
and big game engines have their own bugs as you saw.
Finally this fire bullet game object property here has appeared and I can go here inside of the prefabs
player prefabs and drag and drop it off.
Bullet drag and drop it right here.
Finally thank God.
Moving back in the scripts and here I'm gonna put it into player scripts I'm gonna right click and create
a new C sharps crib.
This is gonna be our fire bullet.
Script.
And now we need to go back again in the prefabs into the player prefab so like the bullet.
Click on Add component and search for the fired bullet component.
Attach it on the bullet.
So I am gonna double click it and open it here in Mount or develop and we're going to code the behavior
of the bullet.
So we are going to code the movement of the bullet where we want it to go.
So first of all we are gonna have a private float speed variable which is going to be equal to 10 f.
Now here what did I do.
I use the speed with capital S which is gonna be the local property we are going to create a getter
and a setter right here below the update.
So we are going to create a public float speed like this and we're gonna have a getter C in the get
there.
We are simply going to see return speed which is this right here variable and we're gonna have a center.
So in the center we are simply going to say our speed not here but here speed is going to be equal to
value.
Now these are accessories to this private variable.
We've talked about private variables in the C sharp section and we even mentioned these accessories.
So we are creating a getter and a setter in order to modify the value of our speed get to it will return
the value which is the current value of the speed variable in setter will set its two new value.
So what is that new value that new values this one right here that we are going to set from our player
shoot script.
What we are doing here multiplying this with the transform local scale is the following.
If we shoot and the bullet needs to go to the left side we need to multiply this with our transform
local skill X because it will either be positive or negative and we will see that in a second we will
see that in a second so if we go here we have our public float speed.
The next thing that we need to have here is void move.
This is going to move our game object.
We are going to call this in the update function and here we're going to use the transform property
to move it.
So we're going to use a vector three temporary variable which is going to be equal to transform position.
We are going to use our current position.
This is what I meant when I said that we're going to use the transform to move the game object.
So next we are going to say temp.
That X plus equals to the speed.
Multiplying that with time that delta time and the last thing that we need to do is say transformed
our position is equal to our 10.
What is going on here what is all of this here.
Similarly as we did with the local scale.
Now in order to change this position we cannot change it like this we cannot say transform that position
that x plus equals two speed multiplied with time that delta time.
This does not work.
We will get an error saying as we did with r you see here cannot modify this value right here and need
to store it in a temporary variable.
This vector 3 temp is that temporary variable so we stored the current position in this temporary position.
Then we edit the x from the temporary position so we add to it.
So plus equals to the speed which is this here multiplied with time delta time.
What is time that delta time.
If I hover over it it says the time in seconds it took to complete the last frame.
Essentially Tink of delta time as the time between two frames.
Now if we have 60 frames in a second.
This number is very small and we usually use it to smooth things like movement in the update function.
So when we move something in the update function we will use ninety nine percent of the times delta
time so time that delta time.
This will make the movement of our game object smoother.
So now when we modified the x value we say plus equals speed multiplied with the delta time.
Then we say transformed.
That position is equal to the temporary variable rate assigning back the value to our current position.
Now what we also need to do is have a function that will this evil our game object after some period
of time.
If it does not hit anything.
So we are going to see here.
Void or I enumerate here so I enumerated which is a core routine which I'm going to say this Abel bullet
like this and here is simply going to see yield return.
You wait four seconds five seconds so after five seconds we're simply going to call a game object set
active to Falls which will deactivate the bullet if the bullet does not hit anything in the start function.
We're simply going to say start go routine.
This Abel bullet and walla we are good to go now inside of the void on trigger entered 2D which takes
a collider suffix collider to D which I'm going to call a target.
We are going to this able the bullet now the bullet will be disabled if it hits one of the enemies so
we are gonna see if our target that game object that tag is equal to my tags.
Beetle tag.
Or if the target that game object that tag is equal to my tags.
That snail tag.
But we did not create that snail tag.
We need to go here and right here below.
Public static String snail tag is gonna be equal to snail like this.
So now we can use it here so that come on man.
My tags that nailed tag.
What we are gonna do if this is true we're simply going to call a game object set active to false this
is to deactivate our bullet.
But before that we can actually do this.
We can get the component so we can say something like private animator Adam and in void Awake we can
say Adam is equal to get component animator because we have that animation and we can pass a parameter
in our disable bullet so we can see here float timer pass here our timer.
And also here save five F because here is the timer five F and here when we hit one of the enemies we
are simply going to see Adam played the animation.
This is going to be r explode animation.
And when that animation finishes we are simply going to call this evil bullet.
So start to routine let me just find it right here start our call routine and disable the game object
after let's say point 9 of a second.
So zero point nine of a second.
This is when we hit either the beetle or the snail or some other enemy that we have now before we test
all of this out.
Let's go back here in Unity.
Select the snail and make sure that we tag it.
So in the tag for the snail we need to create a new one.
I'm going to call a snail selected the snail again and tag it right here.
Apply.
Now we are going to deactivate just they'll just so that you see what I was talking about with this
right here.
So notice what is going to happen.
If I go back here if I fire the bullet you see the bullet is going and everything is okay but if I shoot
the bullet notice I am facing the left side.
I'm shooting the bullet but you see the bullet is facing my right side.
What is going on.
Well it is going on that in the fired bullet.
Speed is positive.
You note that the left side is negative and we need to make this speed have a negative value.
So here in the player shoot what we are doing is we are multiplying the speed value which is this one
right here.
So this one right here we are accessing it by using this public access sir.
So we are multiplying that with the local skill X because our players skill is one.
You see it is 1 but when the player is facing the left side it will be negative one which means we will
multiply the speed value either by 1 or negative 1.
If we are facing the left side we will multiplied with negative 1 which will make this value negative.
So current value is 10 when we multiplied with negative 1 it will be negative 10.
And here we are gonna say temp x plus equals minus 10 multiplied with time that Delta time which means
we will add a negative value.
So for example if you see something like this if you say 10 plus negative 3 that is equal to 7.
Even though we are saying here plus because this is a negative value it will subtract from the current
value.
This is while quick maths so temp X minus or plus equals speed multiplied when the Delta time.
If this value is negative it will subtract from it.
That is like in that song two plus two is four minutes.
One is two quick but you see it's quick maths.
So if we go back now and hit the play button.
Notice what's gonna happen.
I'm shooting to the right side but now when I shoot to the left you see the bullet is going to the left
side because my skill is negative one and I have multiplied it with the negative value so it's going
to the left side.
Let us only test the animation on the snails so when we hit the snail it should animates unnoticed now
bam you saw it animated but we need to make it stop.
Which is while also one of the things that we need to do so here inside of our fire bullet we're going
to create a private bool can move and we are going to allow it to move right here in the start.
So can move is true and in the update or actually in the move function if can move then we are going
to move our game object.
So if we can move we will move it but here when we touch one of these game objects we are going to see
can move is now false so that we don't move and that bullet will stay at that place where it hits the
game object.
So let's test it out now.
Bam you see.
But we do need to actually speed up that process.
So let's see after zero point one of a second the bullet will disappear.
Not after zero point nine.
So if we go here again Ron our game bam you saw Bush.
Bush.
And yeah.
This is our bullet.
Of course our snail needs to die after the bullet hits the snail.
We are gonna probe that program that starting from the next video because we already took too long to
explain what or why how we created our bullet how we are moving it.
So on and so forth.
For the movement again we simply use the boolean variable to determine if it can move or not if move
is true which is here that it will move when it's false which is here.
It will not move any longer far here here from awesome dude Zach and I will see you guys in the next
video.