Welcome back my fellow game developers to
a brand new video where we will be adding animations to our enemy.
Look, he is idling.
I am idling.
If I get close enough and notice that he's looking my way.
If I get close enough, There you go.
You can see he starts walking,
starts trying to kill me.
I can still push him back.
I run away. He stands there. I run away.
He stops.
He starts eye-opening. I get close.
He started teaching. I can shoot him.
He doesn't feel anything.
Okay, so let's stop wasting time and let's get started in animating our enemies.
0 case.
So let's get to animating our enemy.
So if we go into the sprites and to the enemies,
the monster, Monsters, and you have
the skeleton and you have all these different sprites.
So what I'm going to do is I am going to go ahead and dissect all of these.
And I'm just going to show you how I'm going to do this animation differently.
And then you are going to do
all the rest because you already know how to create animations.
Now, the only difference this time,
As I'm going to go ahead and do the animations immediately on the body.
And I'm going to then access the animator from the enemy script writing.
We're going to access the animator on the body.
Why?
Well, honestly, it's always better to use
the animations and create them the way we did with the doom player.
But for one, I think a change of the way we create things will give
you a better perspective and a broader understanding of how things work.
Plus, we get the chance to use a new method,
which is get the component and child,
which I would like to show you all the methods that are possible and just like that.
So let's go ahead and add a component and then you
can choose which is the better way for you.
Whereas the animation window,
okay, let's go to Windows.
So maybe when we change the layout, the animation disappeared.
I'm going to set the animation right here.
I'm going to go ahead and create an animation on the body of the enemy.
So create and the enemies assets animations,
right-click, create the skeleton and animations.
This will be the skeleton.
Skeleton idling.
Save that.
I'm going to choose the idling.
Where is it? I will choose all of those,
drag them in here.
So now when we run,
you can see he is idling really fast.
He's extremely excited to be idling.
I'm going to move those.
Let's see how that looks.
And for some reason.
So I think the first,
second 1 are the same.
And why is that? It's that.
So let's run. No, wait.
Oh, okay, so the first,
second 1 for some reason that I not choose the Swan.
No worries. Let's go ahead and do this.
Okay, great.
So you can see the last one is already similar to the first one. Or no.
Let's copy this.
Set it as the last one. See how that looks.
Yep, there we go. This looks much better.
Should we make it faster? I don't know.
But we're going to save this.
And this one is the entry.
So I just wanted to show you how we're doing this.
We are going to continue and I've written skeleton wrong, no worries about that.
And we are going to continue this way on the body.
And then I'm going to use the magic of editing to do all of this in a couple of seconds.
But you take your time,
create the animation slice of the sprites.
And I'm was going to show you a challenge which is to choose one of the enemies.
It doesn't have to be the skeleton.
So create the ICD-10 walking attack.
You have the attack of throwing a soul and you have the sword throwing animation.
So I believe there is a sword throwing animation.
So pause the video right now.
Slugs up the sprites, create the animations.
I'll see you in just a bit.
Hey, how did you get on with that?
So I've been slicing up sprites.
I've been creating animations.
Let me show you what I did.
If I click on the enemy,
on the body, you,
you can see that I have idling,
attacking, walking and throwing the sort.
So I throw the sword, just like that.
I attack this guy.
Running and walking pretty easy.
That's how I did everything.
I hope it wasn't too hard for you.
I hope you made sure that all of them were 150 by 150.
So you can see all the slices need to be exactly the
same just like I told you when we were slicing to doom play.
Okay, and let me show you the animator so you can see that I have
all of those four now we are not going to be doing anything.
We are not going to be thrown the salt or the skeleton attacking will keep that for.
Later on, we will be walking.
So I'm going to create a transition from the skeleton walking to the skeleton idling.
I'm going to choose both of those by holding Control, choosing both.
Remove on the exit time on both,
remove the transition duration on both.
I'm going to create a parameter in here that's going to be called as walking.
So it's the same.
Oh, no, wait, I created the wrong parameter.
I created a layer.
I'm going to create a Boolean parameter here is walking.
There you go, and I'm going to stay.
Okay, so I'm in this transition,
I am going to add the parameters in here.
So if I am walking is true,
we start walking and walking,
false, I stop walking.
Say all of that.
Now, let's go back to our scene and let's open up our scripts in here.
So what should we be doing?
The first thing we need is a reference to the animator.
So in the enemy controller right here,
I'm going to create a private and it's going to be the enemy.
And he made toward that,
oops, it's of type animator.
And he may, toward save that.
I'm going to get the component.
But as you saw,
the animator isn't actually on the enemy,
so I cannot access it.
When I, as we've said, Get component,
the components should be on the same object as the script.
So to fix this,
I'm going to have to access the animator on the body.
To do that. I'm simply going to say that the animator,
I mean the enemy animator,
is equal to get component in children.
And you can see right now,
let me just finish this off.
Let's hover over it.
Okay, so there is no explanation,
but I think it's pretty self-explanatory get component just like this one,
but in children, so we find the animator in the children,
and because we only have one animator, that's no problem.
But we use this carefully.
So next thing we're going to do,
we're going to check for when we add a velocity.
So in here, we're going to check if the direction to move
and is different than vector to the 0 or vector 3,
excuse me, because this one,
I remember it was a factor three.
Oh, there you go.
How did that autocomplete?
I don't even know.
I'm going to access the enemy animator dot set Bool.
So this is extremely easy.
I believe we've all ready done this.
So you just get a small peak at the behind the scenes.
Copy this, paste it in here,
and we're going to set it to true.
Then else statement.
We are going to duplicate this line for it in
here and set it to false and remove the extra white-space.
So what happens in here?
As soon as we have direction to move in,
it's different than vector 3 0.
The animation is walking is set to true and we transition to the animation.
If we're not walking, it's false.
Okay, great. Let's test this out back in our game.
Let's put the animator.
Where should we put them all? Let's keep him in here.
Game, run the game. Let's see.
Okay, so the enemy is idling.
If I get close to him,
he will start walking.
And there you go. You can see that he is walking,
but there is a slight problem.
I feel as if there is something.
Okay. So he is hovering a little bit.
Why is he still hovering,
did we not okay.
So obviously we did not remove the transition time from this one.
Run the game again, he should start walking immediately as soon as he starts moving.
There you go. He is walking towards us.
Excellent.
Now, one more thing that we need to take arrow and
that is the direction the enemy is looking at.
So I'm going to do a similar thing, just like here.
If the mouse position not acts just like way we flipped our player,
we are going to flip our enemy.
But instead of looking on the screen point,
we are going to compare the direction or the x of the player to the acts of the enemy.
So if the layer to chase dot,
the player to chase dot position,
dot x is less than the transform.py position x.
What we're going to do is we're going to access the transform local scale.
And it's going to be equal to a new vector 3 and minus1 f. What's that?
Minus1 f. And we'll keep the y the same.
And the z, Z,
z, Z, whatever you wanna call it, the same.
And else, if the player is on the other side,
the transform local scale is equal to a new vector, three dot one.
If I'm not mistaken, No.1,
one, what's the problem? Vector 3.
Okay. So I shouldn't put, let me see.
Okay, so no new, excuse me.
So whenever we're not creating a new vector 1,
we just have a vector one that we're setting
the local scale to save that back in our game.
Let's see this in action.
Run the game. And there you go.
You can see that he is looking in the right direction.
As soon as we come close,
he starts walking towards us. Okay, Great.
I tried to run away.
He looks at us and he keeps chasing.
Now the only problem I noticed is that
the enemy is hitting the walls or just like we have the problem previously.
Remember when we wanted to have the enemies.
So you can see because of the collider on the enemy,
he cannot move past this wall right here you can see these are his limitations.
If you want to make your game more detailed,
I do recommend that you add two colliders,
just like we did on the doom Player.
One of them will be just to get heard.
The second one will just be where we walk.
Where is our limitation?
Feel free to do that.
I encourage you to do that. This is your game.
You can make it as perfect as you want.
With that done, we are going to obviously set everything in here.
You can see how many animations I've created.
I hope you enjoy it.
I'll stage all of that and I'll see you in
the next video where we will be actually damaging our enemy.
We can currently shoot him,
but he doesn't feel anything.
That's what we're going to fix in the future.
And I do recommend that you also create the animation
for the ticket and the animation for the Deaf,
which will be very fun to do next video.
So I'll see you then.