Hello guys.
Welcome to the video.
And this one we're going to make Last Enemy in this section.
Going to be a very fun one.
Let me show you an example in my prototype project real quick.
So there is a snail.
You can jump on the top of the snail to bounce it off the shell.
Then you can jump on the shell to make it move like that.
And if you stand on the way, it will damage you.
If you jump on the top of the shell, it's going to fly away.
Simple as that is just like the mushroom enemy, but with a little bit of extension.
So that's what I'm going to show you in this video.
First, let's set up the game object.
This one's going to be pretty easy to do.
We need a mushroom I'm going to duplicate it.
Place it over here.
Then let's do prefab unpack completely and let's name it as Enemy Snail.
Then we're going to remove the script and we're going to go to Graphics Enemies snail.
Make sure you have pixel per unit 16.
Then you go ahead to Sprite Renderer.
And take idle sprite from here.
Okay, cool.
Now we need animator.
Let's go here and make a folder.
Uh, snail.
Then we need the animator controller.
Enemy snail AC.
Gonna put it over here in the animator.
Now we need a bunch of animations.
We need quite a few, like 5 or 6.
So let's begin.
We're going to click create.
First we need enemy snail idle.
Going to go to graphics.
Enemy.
Uh, snail, there is the idle sprites going to drag them here.
Sample rate 20.
Then we need new clip.
Enemy.
Snail.
Move.
Going to take this one.
Sample rate 20.
Now we need one more for the heat.
So enemy snail heat gonna take hit sprite.
Drag them here.
Sample array 20.
Now this is going to be the simple animation for the just walk in functionality.
Then once we jump on the top of the snail, we're going to switch to another set of animations where
we have no body of the snail but only shell.
So let's make animations for the shell.
We're going to make enemy snail shell idle.
These are the sprites.
Then we need new clip.
Enemy snail shell heat.
This is what we're going to use for the jumping on the top of the snail.
Like to display the damage.
And also an enemy.
I mean, when snail bounces off the wall, we need another animation here.
It's going to be enemy snail shell wall hit.
This is the one.
Uh.
We'll hit.
Let's take it.
Put it here.
Sample rate 20.
And this is good for now.
Now let's go ahead to assets.
Animations.
Enemies.
Snail.
And we're going to take hit.
Hit.
And we'll hit and uncheck loop time.
Now we need to set up animator.
Going to be not that difficult to do.
Let's select the snail and let's make a new blend tree.
This one going to be.
I'll move.
I need to restart unity because I have this bug.
Give me a second.
All right, I'm back.
Let's select and do idle move.
Then we're going to open this blend tree.
This parameter are going to be x velocity.
We can add motion field.
Three times.
Let's move.
Move idle.
Move and check automate threshold.
And we're going to have minus one zero one.
Okay, now we need to go back and set this as a default one.
Handle and move can be deleted.
Then from the idle move, we can transfer to heat to display the damage.
And after that we're going to transfer to shell idle to show snail without a body.
So we need a trigger heat.
We're going to transfer to enemy snail, hit with a trigger and check exit time.
Transition duration zero.
Then we're going to transfer from hit to shell idle over here to display like a next stage of the snail.
Here we're going to have exit time one.
Transition duration zero.
Now after we jump on the head of the snail for the first time, this is going to be the default state
of the snail.
And we will be able to transfer to a shell hit when we damage it.
And we'll hit when snail bounces off the wall.
So let's make transition here with a hit.
And transition duration should be zero as well as the exit time.
Then transfer back can be with the exit time of one.
Transition duration zero.
Cool.
And for the wall heat, we need another trigger.
Sorry.
We need the trigger.
We'll hit and we're going to make transition here.
With the wall.
Hit exit time zero.
Transition duration zero.
And then transition back with the exit time one.
Okay, let's all what we need on the animator.
Now let's go back.
Let's place the snail over here.
Maybe so it will flip immediately.
Now we need a scripts.
And let's make new script for enemy snail.
Okay.
Okay, okay.
Let's put it on the game object and let's go inside our script.
Over here.
We want to inherit from the enemy.
And we just want to take the script from a mushroom to make sure it's working.
And then we're going to, uh, improve the script, upgrade it.
So let's go to mushroom.
Let's take everything from here.
Just these three methods.
I'm going to copy it.
Go back to snail over here.
And we want to, uh, just make a boolean.
Let's do public.
Public bool has body.
And let's make it true by default.
And this will help us to define what we need to do with the snail when we jump on the head of the like
of the snail itself.
Okay.
So we're going to override the method die.
Let's do override die.
And usually we just call method die when we detect the enemy.
But here we want to do this.
If has body then we want to stop movement.
So can move equals to false.
We want to say the snail is losing its body.
So we're going to do has body equals to false.
And we want to disable the idle time so it can run left and right and bounce off the walls.
So I'm going to do idle duration equals to zero.
And also it would be nice to trigger animation.
So I'm going to do anim set trigger.
Hit.
So this one can happen.
And in another case, if doesn't have anybody, then it's going to die.
Let's do else.
Die.
Just like that.
So this will do a lot of functionality.
We only need to go to handle movement over here and add another check.
If can move equals to false then return also.
Now can move.
Boolean is false by default on all of the enemies.
So let's go to enemy script.
And on the enemy I want to make can move equals to true by default.
Because of that, this will be true for all of the enemies we have, and I think we need to go to the
enemy rhino.
And make it can move poles at the start.
Okay, now let's get back to snail.
It can move.
Once we jump on the head of the snail, it's going to stop.
Oh, by the way, let's do our velocity equals to vector zero.
So it's going to stop.
Yeah.
And then when we jump on the head again, it's going to die.
Now let's go ahead and try that.
And we only need to assign a couple of things here.
So ground check what is player?
What is ground.
We don't need player detection but we need to reset.
Collider.
Reset.
Let's adjust the size.
To something like this.
Yeah.
And also I want to take damage trigger and I want to make it wider but slimmer.
So maybe something like.
This.
All right.
Cool.
Uh, something else, I think.
No.
Oh, yeah?
Well, detection.
We need to increase walking distance.
How?
Maybe one will be just fine.
Okay.
Ground check is in front, so it's fine.
Let's go ahead and see this.
So there is an enemy walks just like any other enemy.
Then I can just jump.
It's going to enter the shell state without a body.
And if I jump on it again, I'm gonna die.
Cool.
Very nice.
Now let's make a buddy real quick.
And for that, we're going to need a script.
Enemy script.
Enemy snail body.
On this nail biter.
We want to have a rotation and a bit of a velocity.
We can take it from the enemy snail which has this information on the enemy script.
We just need to make these variables protected over here.
Protected.
And.
Protected.
Okay.
Now let's go to the enemy snail body.
Over here.
We need rigid body.
So I'm going to do private rigid body 2d RB.
And we need private float z rotation to set rotation on the object.
Then we're going to need a public void set up by D which will take float y velocity for the death impact
and also float z rotation to store the rotation we supposed to have on the body.
And this is how we going to do this.
And we need.
We need component of the rigid body so we can do RB get component of a rigid body 2D.
Then we're going to do RB dot velocity equals to a new vector to rb velocity dot x and y velocity.
And also we want to save this rotation to this variable.
So we're going to do this z rotation which refers to a variable we have over here.
And it's going to be equals to z rotation which refers to variable.
We pass through the parentheses.
Now that we have it, we need update.
And in the update we're going to do transform rotate.
And here we need vector three.
Let's do zero zero.
And for the z we're going to do z rotation.
Multiply by time dot delta time.
And it's very similar to what we had on the enemy.
When we handle this rotation, right?
Very similar.
All right.
Now we have this method.
And we can make a game object, put the script on, and then use this game object with a script to create
a body, just like we did with a visual effect or bullets on other enemies.
Let's go and set up a game object.
So you know what I'm talking about.
We're going to need graphics enemy.
Uh, snail.
Let's take the snail without shell.
You're gonna switch layer to enemy, and I want it to be in front of the snail at all times.
So I'm going to give it order in layer one.
Now we need to add rigidbody 2D.
Let's do, uh interpolate continuous and we want to add snail body script.
Very good.
So prefab is ready.
We only need to change the name.
I'm going to do enemy snail body.
Then we can go to asset prefabs and just drag it here.
Now we can remove it from the scene and on the enemies nail, we need to make a variable so we can drag
it here.
And then we can create an object.
Let's go to script.
Uh, I'm going to close all but this.
Then I want to make a header, other snail details and we want to have serialized field.
Private game object.
Um, by the prefab.
Actually, let's not use GameObject.
Let's use script component so we can easily get access to it once we create it.
I'm going to do enemy snail body.
This can be private.
Now we know how it works.
And yeah, that's good for now.
Let's go somewhere below and let's make a method private void create body.
It's not going to be super simple.
We're going to need GameObject, not we need.
I'm so sorry.
We need enemy.
Um snail body.
New body equals to instantiate, and this will create a game object of a body prefab.
And at the same time it will get component of the script.
We just need to make transform.position here and quaternion identity.
And then.
We're going to do a new body set up body for the Y.
We're going to do death impact speed for the rotation.
Uh, we can do random one.
Yeah, let's do random.
We need int random index.
Now.
Wait, how do we do that?
I forgot, how did we do that on the enemy?
We did random range from 50, right?
Yeah, from 0 to 100.
So this is what I'm going to do.
Let's copy this.
Go to enemy snail.
Paste this in.
We just need.
To go here now and do.
Death rotation.
Speed multiplied by death rotation direction and it's going to be random.
Very good.
And just in case we want to destroy game object after we create one, uh, like after 10s, maybe.
So let's do destroy.
New body GameObject after 10s.
Okay, now we need to call this method when we hit enemy for the first time.
And we can do that on the animation.
Let's go back.
Here on enemy snail hit we have sprite where we flashing it white and I want to create body at this
frame.
And these frames are extra because I want it to look like we detached body from the shell.
So I'm going to add the event over here.
Choose.
Method created by de.
And these three will be deleted.
Now let's go ahead and assign prefab.
Our snail buddy over here and we can check this.
Let's go to play mode.
There is a snail and I'm going to jump on the head.
You and we lost the body, by the way.
We probably need to flip it, right?
Uh, other than that, looks good to me.
Okay, let's go flip the sprite.
And also, uh, yeah, we can just do it here on the setup by the method.
Let's go to definition.
I'm going to do int facing direction plus int facing direction.
And then over here we're going to do this we need.
I want sprite renderer SR.
Can get component here as I equals to get component of a sprite renderer.
And then if facing direction is equals to one, then sr flip dot x flip x equals true.
Okay.
Now we're going to flip the body.
This is good.
We don't need to do anything else here.
Going to go back to enemy snail and pass here facing direction.
Other than that, we can set up movement of the shell when we hit it for the second time.
So for that I want to speed up the movement.
So for that we need serialized field, private float, max speed.
Let's make it equals to ten by default.
And then functionality is very simple.
We just need to add another else if statement.
Into this method when we jump on the head of the snail.
So when we hit it for the first time, we stop the movement, we lose the body and then it waits there.
Simple as that.
When we do the second jump, we can do else if.
Can move equals to false.
So if not moving and has no body.
Then let's fix it.
Uh, wait a second.
How it's supposed to be curly bracket here and now this one.
Okay, so if we cannot move and we don't have a body, means there is just a shell there, and we can
launch it left and right.
So it's going to move real fast and bounce off the balls.
This is how are we going to do that.
Let's do anim set trigger.
Of heat.
And then we're going to do can move equals to.
True so we can move again and move speed is going to be equals to max speed.
So it's going to move real fast.
Also when we flip the enemies I want to check if we detected the wall.
So let's override the flip method.
Now let's do it below.
I'm going to do override flip.
And here we're going to do if as by d equals to false means only shell is moving, then we're going
to do anim set trigger.
Of wall hit and this is going to work fine because we're not going to flip enemy when we flipping off
the ledge.
We're only going to flip enemy when we're hitting the wall.
But I'm going to show you that in a minute.
First, let's go ahead and check how it's working right now.
YouTube speed up the shell or not okay.
Let's save this and let's go back.
Going to go to play mode.
There is a snail.
I'm going to jump on it now.
It stands there with no body and I'm going to jump again.
And it moves left and right and bounces just like that.
It damages me as well.
And if I jump on the top, I'm going to destroy it.
Very nice and very fun one.
I like it a lot.
And the last adjustment we need to do is to fix the behavior of the ledge.
Uh, first we need to make it heavier so it's going to fall faster.
Let's do gravity scale three.
And also we don't want it to rotate because sometimes the snail might just move off the ledge.
Not here where it's supposed to die, but somewhere here, let me actually show you that I'm going to
make another snail.
Place it here.
I'm going to switch off the spike ball.
And let me.
Erase this platform over here.
Okay.
So now, what happens when we.
Kick it and then kick it again, and it just goes here and there, and we need to switch off this ability
for the shell.
So we're going to go back to scripts of a snail.
And we need to change condition in which it can flip or the is not ground detected.
We can simply add here and has body just like that.
If it looks a bit confusing, what we can do is to add bool.
Bull can flip from ledge.
It's going to be equals to no is ground detected.
And has body.
And now we can use it here and it's a bit cleaner and more understandable.
Can flip from ledge.
All right, so the flip of the latch will work only if we have body in the snail.
Very good.
Now let's go ahead and see this.
It's going to have a couple of bugs.
We're going to fix some real quick.
Let me just switch off the fire and checkpoint.
Okay.
You.
You and it goes off the ledge.
I want to switch off rotation because I believe if this platform is higher then this snail might really
twist it like.
So give me a second.
Now imagine that.
Wow.
Yeah.
So we need to switch off Z rotation over here, and let's, uh, let's delete the extra.
Let's take this one.
Switch off z rotation.
I'm going to bring level back to what it was.
Something like that, I believe I don't remember lets me do control Z.
Yeah.
Like so.
So I'm going to delete the extra here.
I'm going to take this one.
Give it freeze rotation Z.
And also you probably noticed it sticked to the wall for the same reason that player had.
So we can fix it in the same way we did for the player.
We just need to go to the collider and attach player material over here so it will not stick to walls.
Now let's go ahead and remove this platform over here and I'm going to see how it does it behaves.
It flips off the ledge when it has a body.
Then when I pick off the body.
It flips off the wall, but it does not flip off the ledge and it just falls there.
Very nice.
Just what we need.
The last adjustment I want to do is to destroy the enemy when it goes into a dead zone.
Uh, so let's go to dead zone.
Over here.
And we're going to check enemy.
Enemy equals to get component of the enemy.
And if enemy is not equals to null then enemy die.
This is what we need.
And lastly, I think we do not destroy the game object when we kill the enemy.
So this is what we need to fix.
Let's go to the definition of a die method.
And over here.
I want to do destroy.
GameObject after 10s should be fine.
All right.
This is supposed to be good.
I think we are ready to go to the next video.
I'll see you there.