Welcome back my fellow game developers to a brand new video.
And in this one we've actually made our enemies lightly stupid.
So even though he can shoot,
but only the slight problem is that he shoots himself,
which is sometimes good,
especially if a very hard enemy.
But in our case, we will be fixing it later on.
But for now, all you need to know is that we can
change the time between shots to, for example, three.
And you can see it takes three seconds to shoot,
but we don't do that using a counter.
We do that using something called a co-routine,
which is a very hard scaring but in credibly powerful tool to learn in Unity.
So I need your concentration and your full attention.
Let's not waste any more time and let's get started.
Okay, so time to give our enemy the ability to shoot the player,
or at least attack the player.
We do have two different types of attacks that we are going to implement in our game.
One of them will be the Malay,
one of them will be simply shooting from afar or
throwing the sword and the skeletons example.
But we are going to focus more for now on the shooting from
a distance and do them melee attacks later on.
Maybe in a different section,
may be in the boss section where we have the giant skeleton.
Okay, with that out of the way,
let's go ahead and get started.
The first thing we're going to do is we're going to
create a couple of variables and the enemy controller 0 case.
So we are going to go into the enemy controller.
And in here we are going to create a couple of variables.
So before the death splatter right here,
we're going to add attack,
just a common so we know what everything is and it's going to be a serialized field
of type Boolean and it's going to be a melee attack.
So if it is a melee attacker,
we are going to check it in the inspector and it will attack using melee.
Then we are going to create a serialized fields,
which is going to be a game object and it's going to be the enemy projectile.
So this is what the enemy will be throwing out of his hand if he does shoot.
Finally, we are going to create a transform dot.
Oh, sorry, fire position.
Okay, so now we have the projectile.
We will create a file position.
And we also want to have a time in-between the shots that the enemy does.
So it's similar to the way we handled our gun.
So float time between shots.
And finally we are going to create a private bool ready to shoot.
So what happens here?
When we are ready to shoot, the enemy shoots,
and every time he shoots and between time shots is going to calculate.
So we did this using,
I believe the counter.
We created a counter where we count down the time until
we finish a certain amount of time between shots when it's finished,
then we can show it again.
That was an old way.
Now we are going to use something called the co-routine.
So before I do that and introduce you to coroutines,
let me first of all set the ready to shoot in here to true.
So whenever the enemy starts,
he is ready to shoot and an update.
We are going to go ahead and create something in here which is a condition.
And if The Malay attacker,
or if we're not a merely attacker, we are not.
When we put the exclamation mark again and we are ready to shoot.
Then what we're going to do is we're going to go ahead
and say that ready to shoot as false.
And then in here we are going to start a coating.
So what is a co-routine?
Why do we need it in here?
Let's have a bit of an explanation.
So what is a coltan?
A co-routine is a method that can suspend the execution of
instructions until a certain condition or certain conditions are met.
These suspensions are know as yield.
And in our example,
will be suspending the time until our enemy is ready to shoot again.
So let's go back to our code and see what all of that means.
So currently what we have,
we check in the update which is called every single frame.
We check if we are not aiming attacker and we are ready to shoot,
then ready to shoot, set to false.
And for example, here we are going to shoot the bullet.
But what we want, just like we had in the counter,
we want after a certain amount of time to be able to shoot.
Again, we have this set to false because we don't want to shoot
every single frame a bullet and have gazillions of bullets.
So the way we fix this is by using a co-routine.
So let me just go ahead and create a coroutine right
here is going to be of type eye enumerator.
You don't need to worry a lot about what our enumerator is,
just know that it's the type of coatings.
We're going to create the coroutine name as fire enemy, projectile.
I'm going to open up the brackets and we are going to suspend the time.
So I'm going to say that yield.
Okay, Focus, yield.
That means we are suspending.
Return. New.
Wait four seconds.
I'm going to open up the brackets.
And how many seconds do we want to weight as the time between shots?
And after that, we can go ahead and instantiate the bullet or instantiate the projectile,
which is the projectile,
enemy projectile where do instantiated at the five position,
that position and the position but rho station.
And then what we can do is set the ready to shoot back to true.
And now we can show it again.
Complicated, isn't it?
I know less explain.
Oh, and before I forget,
and here after we have the shoot,
we start the co-routine,
which is the far enemy projectile.
Okay, now you're even more confused.
Let's try this again and update.
What do we have every single frame update as being cold,
we have all these statements right here which we
will be converting into methods in just a bit.
But what we focus on is here,
if we are not a melee attacker and we are ready to shoot,
then ready to shoot,
set to false, and we start a Colton.
So what happens in here,
ready to shoot a set to false?
That means that this method or this condition right here is false.
So we don't shoot again every single frame.
That's why we set it to false.
But how are we going to set it back to true?
We used to do that using the counter.
Now we are using something a coating.
And just like I said,
co-routine is called Using start coroutine.
You put the name in here and you call it I enumerator.
No worries. Don't worry about that. For now.
You want to delve deeper into that? Be my guest.
You don't have to know exactly what that means.
We have the far enemy projectile,
then we have this expansion, which is yield.
Then we have the return new, wait four seconds.
So because we have an ion numerator,
we have to return a new weight four seconds. What does this do?
Just like it says, it waits for a number of seconds,
which is the time between shots.
And after it waits for this,
then we instantiate the enemy projectile.
Enemy shoots, and he is ready to shoot again, again and again.
So what happens in here?
First of all, we start,
we have the ready to shoot to true.
We should.
It's false, we no longer shoot.
And when the time between shots ends,
we throw the projectile and then we are ready to shoot again.
Let's test this out and see how it works.
I'm going to go back into Unity.
I'm going to see where do we actually have our knife 0.
So we don't have a knife.
So what we'll do is we'll simply use the bullet that we already have.
So in the prefabs,
we have the player booklets.
Just use that for now, the fire position,
let me add right-click,
create an empty game object.
And I'm going to put it right here,
are run almost right here.
So this will be the fire position on the enemy.
We provide him with the FAR position
defined between shots would be 1 second. We will save that.
And now we are going to make sure that he is not a, made an attacker.
Let's run the game and see what happens.
So now, there you go.
You can see that he is instantiating the bullet,
but He's shooting himself and the face.
So if I pause this for a second,
you can see that the five position,
that's how it's being directed.
And we know that the players bullet only traveled in the right direction.
I know it's kind of funny.
Let's try this a different way.
But in any case,
let's not worry about that too much for now because in the next video we'll be
creating proper enemy bullets that will actually far towards the player or not.
Even bullets will create the projectile,
which is the ninth of the enemy.
He'll be throwing it as the player and not shooting himself.
But you can see right now that this is working using the Colton.
If I click back on the enemy right here,
you can see that I can increase the time between shots to 2.
So now every two seconds we have this attack.
So I hope you enjoyed the video.
I know the shooting mechanic isn't perfect,
but as I said in the next video,
we'll be creating better bullets.
I hope you are committing all of your changes.
And in the next video we'll be making a proper enemy bullets,
Gentiles, or whatever you wanna call it.
So I'll see you then.