Welcome back, my fellow game developers.
In this video, we are going to add bullets.
We call them bullets, but they are swords to our skeleton.
So when we run our game, look what happens.
You can see that the skeleton is actually now throwing sorts.
And not only that, if we walk around,
the swords start following us.
And if you notice,
if we change the source that are already flying,
keep flying in the direction because there is something very recent that happened.
They discovered that sorts don't change directions in mid air.
Amazing discovery. Let's not waste any more time with our,
let's not call them stupid jokes.
And let's get started.
Oh, okay, so time to create some proper enemy bullets.
So the first thing that we're going to do is we are going to go ahead
and get the sprites of our sword or the sort of the enemy.
So if I go into the sprites,
if I go into the enemy's,
the Muslim enemies, the skeleton.
And in here I have the sprite for where is it?
Where is the flying thoughts?
So there you go. This is the flying sorted.
We create anything for it.
I think we haven't.
So I'm going to create an empty game object, reset its position.
And I'm going to give it one of the,
where is it, the sorts?
So let's just give him this one.
I will add four.
I can simply add the sword right here.
Just remove this game object and I'll call this the flying skeleton sword. There you go.
And I'm going to create an animation.
So I'm going to go to the Assets and animations,
and I'm going to skeleton animation.
And let's just call it the flying saucer.
Click on Save.
There we go. So now we have the animator and let me just choose all of the sort sprites.
And let's go ahead and drag them in here.
So now if we run the animation, there we go,
you can see we have an extremely fast sword That's move it around.
Okay, so that's almost okay. So there you go.
You can see that this is the way our sort works.
And it explodes before it will get to the enemy.
So it has about 40 frames of flying time.
And that's good. We do want to have the sword at least can destroy at some range.
We don't want the salt to be flying all the time now,
if you do want to have your salt flying all the time,
you will need to add a couple of things.
So for example, you can just put him on a loop and the salt will keep flying
and flying without getting ever destroyed until he reaches the player.
And finally, fair can get destroyed with some conditions.
But let's not worry too much about that right now.
What we will do is we are going to open up the scripts in here.
We are going to go into the bullets and
we'll right-click and create a new C-sharp script,
which is going to be the enemy bullet Controller.
And let's just call it bullet or projectile,
but for now we will call it bullet.
So I'm going to save that.
And I'm going to double-click and open this up.
So the first thing that we are going to need is a serialized field,
which is a float for the bullet speed.
And i'm, I'm going to use bullet and sort of projected as we go on.
And we are going to need a reference to the players direction.
So I'm going to create a private vector 3 and it's going to be the player direction.
So how is the enemy bullet actually going to work?
What we will do is then start,
we are going to determine the players direction.
So it's going to be equal to find objects of type.
And it's going to be the layer controller.
And from here we are going to go ahead and use the transform dot position of
the player that we find minus the transform.py position dot o.
That's it. No, not normalized,
No, not right now.
So why are we doing this installed and not an update?
Because we don't want the bullet to keep following the player.
We only want to have the player direction as soon as the bullet is fired.
So when the bullet is fired,
we are instantiating that bullet.
And that bullet. As soon as it wakes up. Start.
We'll start. It will find the player object,
get the direction of the player.
And then we are going to make sure that the player direction normalize.
There we go.
So that way it's not faster if it's moving in a diagonal.
Okay, great. Now the next thing we want to do is we want to make sure that
that bullet gets destroyed whenever it hits an obstacle.
But not only that,
we want to also reduce the health of the player later on when it hits him.
So one of the things that we will need to do.
Is issue you a challenge?
How about that transition?
The straw, the bullet, and check the player.
So you'll need to create,
this should be M on Trigger Enter to D for the bullet.
You need to check if what we hit was the player by his tag,
will need to destroy the bullet when it hits something.
And I'll give you a small hint,
you'll need to use the straw.
Well, this isn't actually,
it was pretty easy,
but I thought that maybe because it's the first time you use destroyed,
maybe you'll need that.
So remember that you'll also need to add a trigger to this.
Pause the video right now and go do the Shalon k.
So there is something that I left From the challenge.
I did mention it a bit and I hope that shoe that this on your own,
I'm going to have to add a collider.
So a circular collider 2D, It's pretty big.
I'm going to put the radius above, take big.
I'm going to, okay,
So I think this radius is good enough.
Let's see how this will be flying.
So this is the complete circle.
So I'm going to move it a little bit down,
a little bit like that or okay, great.
So now we have the collider,
we make sure to make it a trigger.
I'm going to save this and I'm going to go ahead into the o and add
also the scripts of the enemy bullet controller.
There we go.
The boat speed will be, let's make it 34.
Now, double-click on this, open this up.
And now what we're going to do is create an on sugar enters,
so on Trigger Enter to D.
And here we are going to check if the collision.com tag.
And if it's the player,
obviously we'll need to create the stack in a bit.
If it's the player will simply going to use the debug dot log.
The layer was hit.
And then we are simply going to straw this,
whatever the target was,
soul destroying game object.
Save that.
Now the final step is we want to move our bullet.
And how are we going to move our bullet?
Well, obviously we can use rigid body again,
just like we did with the player bullet.
But I think I'm this case,
it's much better to use the old-fashioned way which is moved the transform of the book.
First of all, there are many reasons.
First of all, we can do this pretty easily and it is very effective.
And the second reason is we don't want to have a lot of rigid bodies and our game,
because whenever you have a rigid body,
that means the physics engine is working.
And when there is a physics engine is working,
that means that there are more computational power.
So the last we use the butter our game runs.
So an update, I'm going to access the transform.
I'm going to, I mean the position on the transform.
And I'm going to keep increasing it.
Going into the player's direction,
time the bullet speed.
So this speed, and I'm of course going to use time, delta time.
So as soon as the bullet is instantiated,
the direction of the player is found.
And each and every single frame the transform is moved towards that player.
And as soon as it hits the player,
then the game object is destroyed.
And if we had deep layer or any other object that gets destroyed,
but if we had the player specifically,
then the player was hit is going to be put or printed on the console.
So I'm going to save this.
I'm going to go back into my again.
And let's see, we are going to make this a prefab.
Prefab. I'm going to right-click and here and create a folder, 40 bullets.
And the player bullet in here.
Double-click in here, set the flying skeleton sort in here.
I'm going to delete it.
Let's save this.
And let's go ahead and run the game and see what happens.
So, okay, we didn't change the heap.
We haven't changed the actual player bullets.
So there we go.
Let's add this flying skeleton,
sort astron the game.
And as you can see,
nothing is happening because the bullet is on the wrong sorting layers.
So at the enemy sorting layer,
now we should save this.
Now when we run, we should.
Yep, there we go. You can see that we have these sorts flying in towards the player.
And when we had the player,
we don't have anything printed out to the console.
Why is that? Well, let's check, will save this.
So I forgot the most important step and what you should have done in the challenge,
I assume that you would know what to do and the challenge,
because we are checking for the player tag,
but apparently by memory, isn't that great?
So the problem here is that we did not tag the player as well, the player.
As you can see, we already have the tag right here.
So big mistake, no worries about that.
We live, we learned,
we apply our changes.
We save all of this, we run the game.
And now what we should see is that there you go.
As soon as the sorted the player, we have,
the player was hit and you can see it multiple times and here 4, 5, etc.
Now, what I wanted to show you is if I move the player,
the swords that are already going in the direction of the player won't change.
So there you go. You can see that now sort
follow the player as he moves and can we destroy them?
Yep, we can.
If you want, you can add another layer,
which is the enemy bullet layer,
and make sure that the bullets of the player don't interact with the bullet enemy.
That's your choice.
What game mechanics do you have in your game?
Do that, apply it?
I would love to see it if you show me.
And was that none, I hope you enjoy it.
I hope you are committing your changes every single video.
And in the next one we are going to add a range to the enemy
shooting so we don't want to let our ME sures from wherever on the map we want to have a,
some kind of range in which the enemy can shoot us.
So I'll see you then.