Hello guys.
Welcome to the video.
So before we go and develop any other enemies, we need to set up interaction between player and enemies.
And also we need to set up interaction between two enemies because that is also possible.
So first of all, if we go to the play mode right now and I'm going to just walk into the mushroom,
this is what is going to happen.
I'm going to push him like that.
Morton that if I take two mushrooms and place them.
On the level like so.
Taking a walk and then they're going to push each other as well.
Not cool.
So first we need to disable interaction between these three so they can walk and not touch each other
okay.
So let's go to preference.
And sorry not preference.
We need project settings over here.
Then we're going to need to find physics 2D.
Let me open this wider and over here below.
Hey, where is it?
It was here, man.
Layer collision matrix.
Okay.
New unity version has a special window for this.
Very cool, I like it.
So over here, we need to disable, um, collision between enemy and a player.
So let's find a player and deselect enemy over here.
And also we need to find enemy and disable collision between enemies like that.
So now enemy cannot collide with enemy and player cannot collide with enemy as well.
That's what we need.
Very good.
Now let's close this.
Let's go to play mode and let's see how do they behave right now.
I can walk by and they can walk by each other as well.
Very good.
Now we need to add a damage functionality to the mushroom, right.
So it can damage the player.
Uh, we're going to just delete one of them.
And on this one we're going to make.
Uh, 2D sprite square just so I can see the damage area.
Let me do the enemy.
I'm going to place it somewhere here.
Or actually, you know what?
We can just remove the sprite renderer.
We can go to enemy mushroom copy box, collider copy component.
Then we're going to go to square itself and do paste component as new.
Nice.
Let's rename this to Damage Trigger and we're going to set it to Ease Trigger over here and give it
Damage trigger script.
Okay.
Now we just need to be sure that player can enter this trigger area.
For that we're going to go to the enemy mushroom.
Select the collider and make it a bit slimmer horizontally.
And maybe let's reduce a bit from the top like so.
All right.
From the bottom.
Not necessary I think, because otherwise the enemy will just like fall through the ground a bit.
But you can take damage trigger and you can increase.
It over here as well.
Why not?
This would work, but that feels a bit unfair because then player oh actually it's fine.
Why not?
All right.
Uh, now, even though we have this damage trigger area, it's not going to work because this object
damage trigger has layer enemy.
And if you remember, those two have disabled collision between them.
So to fix it, what we need to do is select this damage trigger and change it to default for the damage
area.
Trigger should be default because otherwise they will not be able to collide.
Now if I go and bump into the enemy.
I'm going to get damaged.
Very good.
Now, what we need to do is to make detection of the enemies when player is fallen, so he can jump
on their head and kill them.
Right.
That's what we need to do.
How it's going to be.
We're going to go to player and we're going to set up.
Uh, a bit of a collision here.
So first we need a layer serialized field.
Serialize field private layer mask.
What is enemy?
Because we want to do check only for the enemies and nothing else.
We also need serialize field.
Private transform enemy check that's going to be used for the enemy check position.
And we're going to need serialize field.
Private float enemy.
Check radius.
And let's put a space over here so it's easier to check in the inspector.
Nice.
Now let's go and draw gizmos for this.
Going to be super easy over here we're going to do gizmos.
Draw wire sphere at enemy check position for the enemy check radius.
Now we're going to have gizmos.
Also, this collision check could be done in this method.
Handle collision or.
I could make some other method for this.
This entire method for the enemy check.
And that's what I'm going to do.
I'm going to type over here, handle enemy detection.
Now I'm going to click create button.
Quick actions generate method very well.
Inside of this method we need to type a bit of a script here.
So first of all we want this to work only if player is jumping on the head of the enemies means it's
supposed to work.
Only when we are falling for that we're going to do.
If rb velocity dot y is bigger or equals to zero, then return, then it's not going to work.
Now we took care of that.
Next would be to detect all of the enemies that are below the player, and all of the enemies that got
into the enemies check circle.
So we're going to do this collider array.
So we creating a colliders as a local variable.
And we're going to fill this colliders with the objects that got into a circle that we have.
Uh, maybe let me show you the circle first real quick.
Let's go back to unity.
Now we're going to need new empty transform.
Enemy check.
Can we be consistent?
What do you think?
Let's be consistent.
I'm going to do enemy.
Check with capital letter and I'm going to do here as well.
Ground and damage trigger.
That's what I want to do okay.
So on the player we're going to assign enemy check over here.
What is enemy going to be.
Enemy and enemy check radius.
Let's find the player going to be something like 0.25.
Now let's do bigger.
Let's do.
Point four.
Point five.
Point five looks good.
Why not?
Then let's take this anime check and move it below to the feet of the character.
So if enemy gets into this area, then he can be damaged.
And that's good for now.
Now let's go and make sure it's working.
We're going to go to player script.
And don't forget we need to detect enemies inside of this circle.
And that's why we're going to go here and say that colliders will be equals to physics.
2D overlap circle all from enemy check position within enemy check radius on what is enemy layer.
So this will make a local variable of a colliders as an array.
And it's going to fill it up with all of the colliders that will get into the circle that is drawn on
the enemy.
Check position within enemy check radius on what is enemy layer.
Very nice.
Now we can check this colliders and see if there is any enemy in there.
But before we do that, let's just make sure there is something I'm going to make for each loop.
And we're going to just type here var.
So we're going to detect like any variable.
Basically it's like any variable that can be.
And I'm going to type it as enemy in colliders.
And then I'm going to do this.
If enemy not equals to null then destroy enemy GameObject.
And also let's call jump method.
So that's all what we're going to do.
For now, let's just make sure we can destroy the enemy when we jump on their head.
Gonna go to play mode.
There is the enemy.
And wait a second.
He'll destroy the enemy.
Cool.
Now it is working and all.
What we need to do is to make it pretty, right?
Uh, how do we make it pretty?
Uh, if you remember, I showed you.
An asset reference on how it looks here.
So as we can see it is going a bit up.
Then it rotates and falling down okay.
We know how to change velocity.
We know how to rotate the object.
We know everything here.
Let's go and set up functionality.
Uh, we're going to go to the enemy and over here somewhere.
We need a method that would say something like, let's do protected virtual void.
Um, the right method.
So we're going to call this method when we want to kill the enemy entirely.
When we do this, we're gonna add a bit of a velocity.
Let's do and let's do a header so we can manage the variables real fast.
I'm going to do header death details okay.
We're going to do serialize field private.
Let's do protect.
Actually we don't need protected here.
Private float um death impact.
Also we need serialize field.
Private float.
Death rotation speed because we're going to rotate the character.
And also we need a boolean that would say if we are dead or not and it might be used later on.
So I'm going to do protected bool is dead.
Okay okay okay okay.
Now first thing we want to do.
Uh, when we die, we still trigger animation.
Right.
We're going to do anim set trigger.
It nice.
We also want to give enemy a bit of a velocity up so he can fall down.
RB velocity equals to new vector two.
RB velocity dot x.
And for the y we're going to do death impact.
Nice.
Uh then we want to allow rotation.
How can we do this?
We can make a.
Private void.
Handle death.
And over here, we're going to rotate the character in the same way as we did with the arrow.
Let's go to Trap Arrow.
Over here we have handle rotation.
I'm just going to take this method actually just lines of code from here.
Go back to the enemy paste them in here.
Now the side of the rotation will be random.
I think this is how it should be.
So I'm going to just do how can we do this.
We can make it equals to one.
Wait.
Wait a second.
We need to make a variable over here.
Uh, private int.
This rotation direction.
Let's make it equals to one.
Then I'm going to do random range from 50 to 100, and it's going to be like 50% chance that we're going
to rotate to another side.
This is how it's going to be.
Um, if random range from.
0 to 100 is less than 50.
The Earth rotation direction equals to the Earth rotation direction multiplied by minus one okay.
And over here we can just use.
The Earth rotation speed multiplied by this rotation direction.
So we have handled this rotation nice.
We can call it in the update handle death.
And it's going to be done only if is dead.
Okay, so this is enough to test something.
Let's go and try this out.
Let's see how it works.
Then we're going to go further.
I'm going to go oh wait we destroying the object on the player script.
We need to fix it.
Over here instead of destroying it, we need to call that method die on the enemy.
So this is how it's going to be.
Instead of all of this, we're going to do enemy.
New enemy equals to enemy.
Get component of the enemy.
And then if new enemy is not equals to null.
Not equals to null, then we're going to do new enemy.
Uh.
Is that method not public?
Let's do public virtual void.
Okay, so now it is public.
We can go back to player and call this method over here.
Very cool.
Now let me go and see this.
Also, I think we need to stop anything else if we do this.
So probably would be a good idea to go here and do.
If is dead, then return.
Yes.
Let's go back.
Going to select the enemy mushroom.
Over here we have this impact.
Let's try something like uh five.
And that's rotation speed.
Let's try 150.
Not sure what is going to be.
So I'm going to jump on his head.
Okay.
Almost what we need.
Almost.
First of all, we need to go to asset animations.
Uh, we need to find.
This enemy mushroom hit uncheck loop time.
This is very good.
Also on the animator of the enemy mushroom.
Whereas the animations.
Okay.
If something like this happens, it is better to restart unity.
So that's what I'm going to do.
I'm going to save the project.
I'm going to restart unity.
All right.
Now it's fine.
Here is the animations.
And we go to enemy mushroom hit.
From this trigger and then we not transfer back.
So this is how it should be.
Why didn't it work?
Really?
Why did he kept walking?
Is that return on the enemy is that and handle death.
And we need to make it true.
I forgot.
Let's do this is dead equals to true.
Okay, let's go back.
Just so I have a bit easier time testing, I'm going to let move speed to zero, and I'm going to duplicate
this mushroom and place him over here and one over here.
Now let's go.
Yeah, almost.
Almost perfect.
Yeah.
Okay, okay, I like it.
Now we need to disable all of the colliders that we have on the object.
As of now, we have only one.
And as you can see for this one, it's a box collider.
For other enemy.
It can be another collider.
So this one will have to handle on the enemy itself.
I'm going to just go to enemy over here.
And I'm going to make it.
Oh it's worth oh it is virtual void.
Okay.
So on the enemy mushroom we need to get a private box Collider 2D Collider.
Then we need to override awake and it cannot be called Collider.
So I'm going to be calling this as CD.
Sorry.
Then we need to override awake and over here.
We need to do CD equals to get component of a box collider.
And then we're going to go somewhere here.
We're going to override die method.
And in here we're going to do CD enabled equals to false.
Perfect.
So we're going to disable Collider.
And Mushroom can fall down.
And I think it would be a good idea to disable damage trigger area.
So player cannot be damaged accidentally.
Um.
I don't have a better place for this right now.
So I'm going to just maybe in a collision, I don't know.
I'll just do it above.
For now.
I'm going to do serialize field.
Um.
Centralized field.
Protected game object.
Uh.
Damage trigger.
Let's put a space here so it's a bit easier to see.
And then when we die, we're going to do damage.
Trigger set, active pulse.
All right.
Bunch of things that are helpful.
Let's rename this to handle death rotation.
Okay, a bit better.
Uh, yeah yeah yeah yeah yeah yeah.
Let's go.
Now, let's see.
This supposed to be cool.
Supposed to be fine.
It's going to be different for the enemies that have an rigidbody kinematic, because we're going to
need to switch it off to dynamic.
But other than that, it's good.
Wait, I need more mushrooms.
Oh my God, what is happening?
Are has not been assigned.
I'm so sorry.
We need to assign this damage trigger.
Damage trigger.
Okay.
This is good.
Let me place one here.
This one can be here.
And this one over here.
Let's go.
Now.
He's not dying.
Why not?
How it's okay.
Yeah, this looks cool, I like it.
Wow, this is so fun.
I like how it works.
So if you want him to bounce a bit higher, you can increase, um, death impact here.
And if you want him to rotate slower or higher, you can change this value over here.
Simple as that.
Um.
Maybe I'll rename this to Death Impact.
What do we need?
We need some sort of speed.
Death impact.
Speed.
I don't know.
And I make it equals to five by default, and this one going to be equals to 150 by default.
Okay.
So we don't have to set it on each enemy.
Also let's do this for idle duration.
And move speed as well.
Why not?
So we have some value always when we place an enemy on the level.
I mean, making a new enemy.
Um, yeah, that's pretty good for now.
Oh, there were some sort of a misbehavior when I jumped on his head, right, I was damaged.
Let me see what we can do about that right now.
What was that?
I think for this we can take the player and maybe take his enemy check and place it a bit lower.
Yeah.
Okay, good.
Pew, pew!
Yeah.
This is so fun, I like it.
All right, I'll see you in the next video.