Hello, guys.
Welcome to the video.
Uh, before we proceed, I want to fix a bug that we have with the rhino.
And also we have this bug with, uh, other enemies.
But I plan to pay full attention to this on the cleanup video in the end of this section.
As of now, I just want to show you a quick fix to a very small bug that we have.
So as of now, if I jump on the head of the rhino, it may happen that I'll get damaged instead of killing
him, just like you've seen here.
So what happens is damage trigger may be bigger than a body collider you have on the enemy.
And when we try to kill the enemy, we looking for this collider for the body Collider, the main collider
that the enemy uses.
And when we damage in the player, we use damage trigger.
And this trigger should be smaller on the Y than the body collider we have on the enemy.
And now you'll not have any bug like that.
Okay.
Very cool.
Uh, also, I want to do a bit of cleanup before we start.
This is a real fast one.
So one day I woke up and I realized that damage Trigger is also a collider.
And if that's the case, then we can just.
Get the collider and disable it together with the main collider instead of make an entire variable on
the script.
So let's go to enemy rhino.
Then I'm going to need enemy script.
And over here we can turn this collider into an array.
And then when we get in component of the collider, we're going to do.
Collider.
Get components in children.
This will collect colliders in children, but the main transform will be taken into consideration as
well because when it starts to count the children and getting components in children, it also checks
the main object itself.
So it kind of does get component and then get components in children all together.
Uh, if we make this variable public.
Or visible?
Just visible.
What's that?
Uh.
I'm going to comment this out for now.
So look we have colliders array and we can go to play mode.
And now we have main Collider and Damage Trigger Collider included into this array.
Thanks to that, we can now just disable both colliders on death and not to worry about assigning another
variable into enemy script.
So on the enemy.
I'm going to remove this variable damage trigger.
I don't need it.
Also, we know how assigning of a player works, so I'm going to just hide it from the inspector.
I'm going to hide this as well.
I don't need it anymore.
Maybe like this.
And then we're going to go down to the death method, remove this line of code.
And then I'm going to do.
For each var.
Collider in call array.
Let's rename it to colliders.
So for each of that we're going to do.
Let me remove this and do collider.
Enable false.
Just like that.
Very good.
Now we're going to go back to unity.
We can see Inspector is a little bit cleaner.
We don't have an extra variable there.
And same works for mushroom as well.
This is clean and nice.
And now if we go to play mode, you'll see that both enemies can die from the jump on the head and work.
Same as before, but everything is cleaner now.
Oh, wait a second.
I did not kill him for some reason.
Let me check.
So damage trigger is a bit lower than the body collider.
So why did I.
Let me make it lower even more like so, because it's not important.
If we jump on the head of the enemy, he's supposed to die anyway.
Player should be damaged only from the left or right side.
Okay.
Very cool.
There is another thing I want to clean up and we can jump to the next video.
I guess we're going to make enemy there.
Um.
Uh, let's go to assets prefab over here.
I'm going to drag enemy rhino.
So now we have prefab of the rhino as well.
And as of now we have two enemies that using player detection chicken and rhino.
We're also going to have a plant enemy in the next video.
And he's going to use player detection as well.
Also we're going to have a trunk enemy and he's going to use player detection as well.
I know that because I did this before I did prototype of this project.
So I guess it would be a good idea to move detection range.
And the player detected variable into enemy script so multiple enemies can use it.
Let's just go to enemy right over here.
Take these two variables.
Cut it out.
Let's go to enemy all the way to the collision over here.
I'm going to paste this in.
And this should be protected now.
Let's do is player detected.
So we are consistent is player detected.
And this should be protected as well in case we want to change something.
I'm going to move it.
Maybe above the player layer.
What is player?
And let's name it as player detection distance.
So it has, you know like we have it consistent with all other variables here.
Let's make it equals to 15 by default.
Now we need to go to, uh, enemy rhino.
Maybe I should do this at first, but.
Seemed right.
And here, what we need to do is to take this variable and move it inside of a handle collision and
then just, uh, have it all together within one method.
So how are we going to do this?
Well.
Let me think.
We're going to go to definition of a handle collision over here.
We're going to make this player detected equals to.
And let's just go to enemy rhino.
Actually, let's just copy this line of code.
We can do Ctrl X to cut.
Go to enemy.
Paste it in here.
Let's save this.
And now this is on a base method over here.
An enemy rhino still has access to it.
In here and I guess we can keep it like so.
Even now we just override the method and add simple if statement regarding Ondraw gizmos.
We can cut this line, go to Ondraw Gizmos over here, and we can just paste this in over here.
Just like that.
Now it's going to be drawn for all of the enemies, and now it will be available to all of the enemies.
We can go back to enemy rhino.
Over here we can remove Andro gizmos method and it's going to work same as before.
But now method is moved to the base script like the enemy script.
Very cool.
Let's go and fix it on a chicken.
So on the chicken.
Now we have this handle collision inside of base method.
So we don't need it.
We can just remove it like so.
Just Take It Away and Andro Gizmos as well.
It is on the base script, so it's pretty good.
There is something else I want to improve while we're doing this.
We have this anim set float on the enemy chicken.
We have it on the enemy rhino.
We have it on the enemy mushroom.
And we might have other functionality regarding the animator on different enemies.
So what I want to do is go to enemy, make a protected virtual void, and the animator in which we're
going to do anim set float of x velocity.
Two RB velocity dot x.
Very good.
Then we can go to update and in the base update we can do handle animator.
Thanks to this now animations will be on every update of the enemy.
And now we can go to enemy chicken.
Just remove it here.
It's going to be called on the base over here anyway.
And this method has the update of the velocity dot x, so we don't need it here.
Same goes for mushroom.
We don't need it.
Same goes for Rhino.
We don't need it.
And same goes for any enemy we're going to have in the future.
Now let's save it all.
And let's just go back to unity and make sure we did not break anything.
I did not break anything, I hope.
So we're going to go to play mode.
Everything works the same.
Uh, mushroom is not working, but only because he has zero move speed.
Let me give him some speed.
He walks.
I can kill him.
Same goes for Rhino.
Works same as before.
Very cool.
And let's go back.
I have something else to clean up here.
I think we can call this handle collision method on the base update.
So I'm going to go to enemy.
And together with the handle animator I'm going to do handle collision.
Maybe even before the animator.
Let's go to enemy right now.
Now remove handle collision from here.
Let's go to enemy chicken and remove handle collision from here.
And let's go to enemy mushroom and remove handle collision from here again.
Because now all of the enemies have this method in the base.
Let's save all.
And even if you have, uh, functionality overridden like we have on the enemy right over here, we
have override of a handle collision.
Anyway, the base method of the update over here.
We'll call handle collision method.
And if you override it on a child script, it's going to call the overwritten method, just this one.
So you don't need to worry to call it again and update.
It's going to be fine.
Let's go ahead and try this again and you'll see that everything works the same.
I'm going to trigger the rhino.
He runs to me.
And I can kill him.
Okay, cool.
Let me see if anything else can be cleaned.
Maybe this code on the right now could be taken into a method.
Extract method handle speed up.
Yeah.
Why not?
And this could be taken into a method as well.
Let's do extract method.
Speed reset just so it's a bit cleaner, and we understand what we have inside of a method speed reset,
because it's much easier to take a quick look at the method, see that we do speed reset and not worry
about understanding what the code does until we really need it.
In general, there is a good rule if you can put something into a method, do it.
Well, uh, if there is anything else, I'll let you know.
Actually, let's go ahead and click triggers on the enemies.
Since we're doing cleanup, why not let me take enemy mushroom?
So on the enemy mushroom, we need to have damage trigger lower than the body collider.
So I'm going to just reduce the height of it to something like this.
On the enemy Rhino.
We did this.
And on the enemy chicken asset prefab.
Enemy chicken.
Let's replace this damage trigger with a box collider.
Did I not?
Let's replace it with a box Collider 2D.
Let's make it wide, but not tall.
So something like this will work.
And the main trigger?
I think we can lower the main trigger as well, because it's kind of too easy to kill the chicken.
So something like this should do.
Okay.
Very good.
Now let's apply overrides.
Let's apply override for Rhino.
Oh, it is working okay.
For the mushroom.
Let's make move speed two and let's apply overrides.
Okay, now this is good.
We did a bit of a very nice cleanup.
I hope you did like it.
And I'm going to see you in the next video.