Hello guys.
Welcome to the video.
In this one I want to make another flying enemy and this video will be a good opportunity to show you
another trick to learn.
Let's go to Prefabs enemies and let's make prefab out of the enemy ghost like that.
Then I'm going to do prefab unpack completely and we need to change name to Enemy Be.
Let's go to asset animations.
Enemies.
Uh, we need to make a folder.
Be.
Then inside we need to make animator.
This is going to be enemy b a c.
And then we need to go to animations.
I'm going to select B and drag the animator controller.
And then let's create a couple of clips.
This one going to be enemy B idle.
Nice.
Now we need to go to Graphics enemies B let's take these and make pixel per unit 16.
And then I want to take the idle sprites and drag them here.
Sample rate 20.
Also, let's take the sprite renderer over here and let's turn it into a b.
Okay, this is good I like it.
And now we need one more for the hit create new clip enemy B hit.
Can I take the heat sprites?
Drag them here.
Sample rate 20.
And then we need just one more for the attack.
Enemy the attack.
Let's save this and let's take a tax rates.
Drag them here.
Sample rate 20.
Okay.
This is good, I like it.
Now let's go to animator.
Over here.
We're going to be in the idle, then we're going to trigger attack.
So let's do a trigger attack.
And then we need transfer to be attack from an idol.
Has exit time.
Uncheck.
Transition duration zero.
Then transfer back.
With the exit time of one, transition duration zero, and we can enter enemy be hit from any state
with a trigger hit.
Let's do condition hit exit time.
I mean transition duration zero can transit or self uncheck.
That's all what we need for this setup of animator.
Now my windows are a bit off so I'm going to do windows 2D.
Sorry general where is it.
Layouts two by three okay this is a bit better.
Yes.
Let me open animation.
And window animator.
All right, this should be okay.
Also, we need a console.
Add a tab.
It's not here.
Window.
General console.
Like that.
Nice.
Now we need a script.
So I'm going to go to scripts folder.
We're going to make a script in the enemies folder.
It's going to be enemy B.
Okay, uh, let's go on enemy B.
Let's remove enemy ghost script.
And let's add enemy B.
Okay.
Let's check colliders.
I think it's pretty much okay.
We don't need to change anything I like it.
Maybe damage trigger.
I should be good.
It's enough.
Now let's go to the script.
And here is the script.
And we want to inherit from the enemy.
Enemy.
Then over here we need to take a functionality from a plant, I guess, because it's going to be very
similar to the plant, just slightly upgraded.
So let me do close all by this.
Then I'm going to go to enemy plant and we can just take.
Uh, like entire script from here.
Let's copy this.
Go to NB and paste this in.
And now we need to slightly change it.
So first of all, we need a bullet for the B.
Let's do here B details.
And let's go and quickly create the.
Be bullet script because it's going to behave in a special way.
So we're going to go here and we're going to do C sharp script.
Enemy bullet.
B?
Yeah.
Like that.
Now, inside of this script, we can use enemy bullet B.
And let's go and adjust this script over here.
So instead of enemy bullet we want to use enemy bullet be.
By the way, I just realized we have enemy bullet script.
So we can have a similar name.
Let's press Ctrl RR and let's do enemy underscore bullet underscore B like that.
Very nice.
Now this bullet is not going to have velocity just like we had unplanned, because this bullet is going
to target the player and it's going to follow the player like a auto targeting missile or something
like that.
You'll see it as of now, let's just remove these two lines of code and let's leave it as it is.
Okay?
Now.
Let's go.
Up to the header.
We want to have bullet prefab.
We need gunpoint.
We need bullet speed.
We need attack cooldown.
Also, we need a serialized field.
Private float bullet lifetime.
Because this bullet is going to follow the player, we don't want it to follow the player.
Like endlessly.
We want to have some limitations on this.
That's why we're going to have this variable bullet lifetime.
We're going to just destroy the bullet after lifetime is over.
Something like that.
Now let's talk about targeting the player, how it's going to work.
First of all, we want to have a private transform target.
And we're going to fill up this target with, uh, any player that walked below the be this, what are
we going to do?
So let's go to update method and let's do this.
If target is equals to null means we need to find the target.
And we're going to look for a target with a raycast I'm going to do raycast hit 2D hit equals to physics
2D raycast from.
Uh transform.position.
To vector two dot down direction.
Because we want to aim down for the distance, we can really have a big distance because we want to
detect player.
In any case, if if that's the case then I'm going to do float max value.
And what is player player like that.
Then over here we're going to do something simple.
If hit transform not equals to null then target equals to hit transform.
And now we're going to assign target.
If we find any.
Then when we did perform the attack, when we did create a bullet we're going to do target equals to
null.
So we can look for a new target again.
All right this is good.
Now let's go back to update method.
And over here boolean can attack will be true if we don't have cooldown.
And if we have target.
Just like that.
And then over here we can remove this is player detected because we're doing similar check over here
right.
So we can remove it.
And this should be good enough.
This should work.
And now, instead of creating a bullet, I'll just debug a message debug log.
I created a bullet.
We just want to be sure it is working.
And we have a method same as it was on the plant.
All right, let's go back to unity.
We need to find the B and let me place it somewhere.
Well, here is fine.
Now, what is ground?
Ground?
What is player?
Player?
Ground.
Check over here.
Bullet.
Prefab.
We need to make it.
We're not going to use it now.
Let's make empty gunpoint.
We're going to assign it over here.
Let's focus on the gun point and move it over here to the bottom of the B.
Like that.
All right.
This looks good to me.
We also need to set up animation event.
So we're going to go to animations find enemy, be attack.
Let's find a frame where bullet supposed to fly out.
So it is here we're going to add event function enemy be methods create bullet.
All right.
This is supposed to be ready to check.
Let's go and try going to go to play mode.
And it shoots and it creates a bullet.
We can see a debug message.
Nice.
Now we need to set up bullet and that's what we're going to do.
We're going to go to prefab, uh, enemies.
There is this enemy bullet from plant.
You can just drag it here and we want to do prefab unpack completely.
Then we want to remove this script over here and we want to add enemy bullet be.
Also I want to add damage trigger like that.
It should be good enough.
Now let's go to enemy bullet B and do a bit of a setup.
This bullet is supposed to follow the player.
So first of all, we need to have reference to the player.
I'm going to do Private Transform target.
Then this bullet is supposed to have a waypoint that it uses to follow the player.
We can make a list.
Private.
List of Vector3.
Waypoints.
And let's make it as a new list.
And we're going to fill up waypoints with the current position of the player.
And we're going to do that with a bit of a cooldown.
And because of that, we're going to add each new position of the player to this list.
And then bullet will follow the trail.
It will follow positions that player had.
This is how it's going to be.
Also going to need private int we index.
And we're going to need let's do serialized field private float way point update.
Pull down something like that.
And we also want to have a speed.
So let's do private float speed.
Now we need a method that we're going to use to set up all of these variables from enemy B.
So let's make public void set up bullet.
We're going to pass transform new target.
Then we're going to need float speed.
Let's do new speed.
And we need float.
Um, life duration.
Let's open this.
Let's do, uh, speed equals to new speed.
Target equals to new target.
And then we want to do destroy game object in a live duration just like that.
Well, this is good.
Now let's make a coroutine that would add waypoints to the list.
We're going to do a private.
Void add waypoint.
In which we're going to do something simple.
If target is null, then we're going to return.
Then we want to do waypoint add target dot position.
And just in case I want to return from a method, if we try to add position that we already have.
So I'm going to do for each vector three uh waypoint in waypoints.
If waypoint is equals to a target dot, position means some of them is equals to the current position.
And we then need to add same position.
And if that's the case then we're going to return from a method.
Okay.
Very nice.
Now we want to add waypoints with some sort of cooldown.
And I think we can do that with a coroutine.
I'm going to do uh, I enumerator.
Let's do it as private.
And the way point coroutine, in which we're going to do the same thing as we did on a blinking text.
Over here is going to be very similar.
So we're going to do while true and this will run endless loop for us.
So while true and waypoint.
And then wait.
Yield return.
New weight.
Four seconds of waypoint update cooldown.
And I think it's a good idea to start this coroutine when we set up the bullet.
So I'm going to do start coroutine of a.
And waypoint.
Coroutine.
Okay.
And now this will add waypoints endlessly.
And also we want to move the bullet.
So I'm going to make an update.
In the update we're going to do transform position equals to.
Vector two move towards from a transform position to a waypoints with away index.
With the speed multiplied by time, the delta time.
And just to avoid any errors, we want to return if waypoints dot count is less or equals to zero.
Okay, this is good.
Then if we are close enough to the destination.
If vector two, the distance between Transform.position and waypoints with a way index.
Is a less than 0.1 F, then we are close enough and we can move to the next point.
So I'm going to do way index plus plus.
And also I want bullet to be facing the direction that is going to.
So for that I'm going to do transform up which is a green axis equals to.
And we're going to get direction from a current position to the waypoint.
So equals to transform position minus waypoint waypoint index.
And also would be nice to make it face the player once it flew out of the enemy.
So I guess I'm going to copy this line of code.
Go over here and over here we're going to do target instead of.
Uh, the waypoint.
Okay.
This is supposed to be good.
For now.
Let's save everything.
Let's go to enemy B.
Over here.
Since we have live duration on a bullet itself, we do need this line of code.
But we need this line of code to make a bullet.
And we're going to do new bullet setup.
Bullet.
We're going to pass target.
We're going to pass, uh, bullet speed and bullet lifetime.
Like that.
Very nice.
That's basically everything that we need for now.
Let's go ahead and try this.
Mm.
Let's do waypoint cool down maybe 0.1.
I'm going to see how it works.
If I like it I'll just hide the variable.
Okay.
Let's go and make a prefab out of this one.
We need to go to prefabs enemies.
We can just rename it to Enemy Bullet Be.
Also before I forgot, let's go to rigidbody and let's make it as kinematic.
Okay.
Now let's drag it over here.
Then we're going to go to enemy B, take it from here and make it as a prefab over here.
Very nice now.
Let's see.
This is the bullet here.
We need to update the sprite something I forgot.
Let's go to Graphics enemies B and there is this bullet.
You're going to take the sprite renderer drag it like so.
And this should be good.
Let me see it.
Let's apply overrides and let's go to play mode.
I'm gonna run below the bee and bullet is flying out and it is chasing the player.
Whoa!
Yeah, yeah, that is quite fast.
Let me try again.
Cool, right?
I think the lifetime is too long.
So let's take the nib and let's do lifetime 1.5 and cool down maybe 0.75.
Just for fun.
Okay.
That was quite fast with the disappearing.
Why there are two bullets flew out.
Now it's only one.
Uh, maybe I was under the bed long enough.
That's why I guess.
Okay.
It's fine.
It's just so cool down and let's make it longer.
Then let's do a cool down one, okay?
Now a couple of other things.
I would like to give this bullet a trail.
And also I would like it to disappear in a pretty way.
Now let's just.
Go ahead and create effect trail.
On this trail.
We want to make it really slim.
So like if you move it around now, you'll see how big is the trail.
But if you change the width to something really small and also we can make a dot over here and make
it thinner with the time.
And also let's go to color and let's make it transparent with a time like that.
This is good.
Now it's going to look something like that.
We only want to reduce the time over here to maybe, uh, point 75.
Okay.
So now we're going to have a trail.
We only need to make it visible.
So we're going to go to certain layer and do enemies minus one.
All right, uh, let's try this.
Let's apply overrides and let's go to play mode.
And now, uh, something I did wrong.
We need to go to trail and do transform zero zero.
Okay.
Let's apply overrides.
And yeah, this should be good.
Also, let's reduce speed so we can actually see what is happening.
I'm going to do 4.5.
Let's see the trail again.
So there is the trail makes it pretty and nice.
I'm going to increase a lifetime to three.
Okay.
There was a second bullet, and it's not supposed to be there.
I guess we have animation on the loop, and that's why it is happening.
I'm going to go to Asset animations.
Enemies be.
Let's take the be attack and let's uncheck loop time and let's do it for hit as well.
Okay.
This is good, I like it.
Now what else?
I want bullet to disappear in a pretty way.
So I'm going to open the script.
And I'm going to go to the root.
Over here.
Let me take the method over here.
Actually, just this line of code.
Copy this.
Go to enemy bullet.
Be.
We're going to make Ondestroy method Ondestroy.
We're going to create visual effect.
And we just need this variable.
So I'm going to go to fruit again.
And I'm going to copy this.
Go back to bullet B.
Paste this in here.
And is it destroyed on its own?
I'm not sure.
How do we destroy the visuals of this?
Let me check.
Asset prefab we fix.
Pick up a fix and destroy me event.
Okay.
It is destroyed in the end of the animation, so it's fine.
All right.
So we're going to save this.
And now it's going to create a visual effect when it is destroyed I kind of like it I only want to make
it smaller.
So I'm going to do new effects.
Transform local scale.
Equals to new vector 3.6 f 0.6 f 0.6 f.
Okay, good.
So we're done with the bullet.
I think we can.
Let me just show it to you once more.
And I think we can close the script.
We don't need it anymore.
I'm going to close it, and we're going to work.
And let's close the fruit.
And we're going to work on the bee itself.
Uh, what I want to do now is to improve movement of the bee.
Let's assign pick up wave fix over here, and let's do apply overrides.
Okay.
Now I'm going to delete it.
And let's talk about movement of the bee so I can place it over here for example.
And it looks nice, but I want to make it more like a bee.
I want to make it sort of buzzing around, uh, with a.
Limb movement.
Okay.
I wanted to fly from left to right.
That's what I want to do.
So to do that.
Woo woo.
I like how bullets are flying.
It looks pretty.
Although just now I felt like it does not.
Uh, let me try to increase lifetime.
It's good.
Just the lifetime was short.
I kinda like it this way.
Yeah.
I'm sorry.
So what I was saying, I want to make this be, like, just like that a little bit here and there.
Right?
That's what I want to do.
And that's what we're going to do.
It's a very simple thing to add.
Let's go and open nib script.
I'm going to make a list.
Private list of vector three.
We points, let's do equals to a new list of waypoints.
Sorry.
New list of vector three.
Then we're going to need a private int way index.
Similar to what we had on the B let's do using system collection generics.
And we're going to open start method start or we have start method right.
We can override it override start.
And inside I want to do some sort of uh maybe we need some kind of private float.
Offset.
I'm going to make it equals to point 25 F.
Maybe that's too big.
I'll try and I'll make it visible so I can see it.
Serialize field.
Now, what are we going to do here on the waypoints?
We're going to add our waypoint according to the position of the b.
So if B is in the center first point will be here.
Second point will be here.
Third point will be here.
Fourth point will be here.
And because of that B is going to fly like that.
And it's going to look like it's buzzing.
That's what I want to do.
Let's do it in some sort of simple way without overthinking it.
I'm going to do waypoints.
Add transform position plus new vector three.
And for the x we want to add 0.25 f.
So offset for the y we also want to add offset.
And for the z we do need to add anything.
Then I want to add another point.
But this time we're going to have a same on the x and minus on the y.
So first one will add point over here.
Second will add point over here.
If we pass both values as negative it's going to pass value over here I mean add position over here.
And fourth one going to add value over here.
Also if you want you can manually do it with the transforms just like we did with the Trapsoul okay.
So I'm going to duplicate this.
Do minus offset on the x.
Actually, let's do both with the minus and then duplicate it and do positive on the y but minus on
the x.
Okay, that's what I want to do.
Now we want to go to the update and make a method.
Let's do a handle movement.
Method.
Inside of this method we're going to circulate between these positions.
This is how it's going to be.
Transform position equals to vector two.
Move towards from a transform position to a waypoint.
With our way index.
And we're going to use speed.
More speed.
Multiplied by time.
Delta time.
Okay.
And then if we are close enough again, we're going to just increase the index effector to the distance
from transform.position to waypoint with an index.
Is less than 0.1 F, and then.
Then we're going to do way index plus plus.
Also, we want to check if way index is bigger or equals than waypoints count.
Uh, minus one.
Then we index is equals to zero, right.
Or we don't need minus one here I think we didn't need it.
Okay.
Let's save this and we're good to go.
Can we wrap this into a method?
Let's do method.
Find target.
If empty, something like that.
Yeah, it should be good.
Now, let me just go ahead and try this.
You're going to go back to unity.
We have offset of 25.
And this might be too big.
But I want to see it first.
And let's give it more speed of six.
Now let's go to play mode.
And now we can see here more speed is too big, but other than that, it looks good.
Now let's try move speed two and let's try less of the offset.
Maybe, uh, point two.
Oh, yeah.
This looks a bit better, right?
I think I like it.
If you want, you can try to choose a random index instead of making it fly in circles, but I think
I'm going to accept it as it is right now.
I kind of like it.
Yeah.
Uh, and now it's gonna shoot bullets if you walk below it and you need to dodge them.
Just increase the lifetime.
Yeah, I think it's a fun mechanic.
And then you need to kill it if you want.
Boom.
We need to stop movement if it died.
Now let's go back to the script.
Over here.
If is dead, then return.
Okay.
Nice.
Now let's close this.
Let's go to Asset prefab.
Let's go to enemies and let's make prefab out of this.
So you can place multiple bees on the level.
And we're good to go to the next video.
Let me just try it.
I'll wait.
When?
I want to give them a lifetime of.
For.
Oh, no.
They fly in Synchronically.
We need to fix it.
As it looks silly.
Oh, no.
UN peu.
You.
Berlin.
Okay.
How can we do that?
And let's go to the script.
Let's make a method.
Maybe we have it on the enemy.
Allow movement.
Something like that.
Now I don't see it.
Okay.
I'm private void.
Allow movement.
It's going to do can move equals to true.
Then on the movement method over here, if can move walls, then return and then go to the start method.
Let's do invoke.
Name of.
Uh, allow movement.
And I'm going to be a random value.
So let's do float random value equals to random range.
From zero to something like.
I don't know.
Point six.
And this should be enough.
Wait a second.
Random value.
Why does not allow me to use random?
Okay.
Now should be fine, and we can remove the system from here and remove this from here.
And it should be good.
Okay, now they're gonna fly with a random delay and I hope it's going to help.
Also, let's take this and make a method.
Create waypoints.
Yes, a bit better.
Now let's go to unity.
Let's see them.
And I do apply over rates.
Didn't help.
Because can move Boolean is true by default.
I believe that's why.
Let's do can move false.
Now.
It should work, I hope.
Please work.
Oh, yes.
Now they flying randomly.
Okay.
Makes me dizzy a little bit, but it's fine, I guess.
Oh.
So you need to think how to bypass them.
All right.
This is cool.
Also.
Uh, the finish point is not working, but it's only because I'm in two players mode.
It would be fine if you start game from the main menu.
All right, I'll see you in the next video.
Bye.