We will make the animation of this anime two game object first, and then we're gonna code it.
We're going to control through C script.
As you seen, we just printing out a message in the console says player in attack range.
Instead of that this anime game object need to attack on our player game object.
So let's make the animation first and then we're gonna control those through our C script.
So let's do that.
So I'm gonna move over to my project tab, then sprites basically where I have the skeleton sprite sheet.
So in my skeleton folder as you can see.
So I'm going to choose the ideal one.
So let's click on this arrow.
And let's select all of these individual sprite by holding down shift.
And then drag it over to this anime two game object in the hierarchy.
And it's gonna make the animation right away.
So what we will do, we will move over to the assets folder in the top left.
I'm going to click on this back arrow.
Let's go to assets.
And we do know in assets we created this animation folder as you can see.
So we're gonna open that sub folder by double clicking on it.
And also we have anime animation.
And I'm gonna create a another sub folder for this anime two animation.
So let's right click then new folder.
And I'm going to call this folder to something like anime two underscore animation anime two underscore
animation.
Let's hit enter.
And let's open this sub folder as well by double clicking on it.
And we have to name the animation what we want to name it now.
So I'm going to call it something like anime two underscore idle animation.
So I'm going to call it anime two, anime two underscore idle.
And let's click on this save.
And now let's select the I'm going to drag my anime to game object in the top of my hierarchy so I can
see all the time.
And let's go to animation tab.
And if we click on this play button now as you can see, our skeleton just playing the idle animation
clip over again and again.
So we also need to create the attack animation as well.
So let's do that.
So we're going to move over to project tab and let's choose the attack one.
So we have it over here.
Let's click on this arrow.
And we're going to select all of these individual sprites by holding down shift.
And let's drag it over to a animator GameObject in the hierarchy.
Let's go to assets and then animation Enemy Animation and Animator Animation.
And I'm going to call this animation clip to something like enemy to underscore attack because this
is the attack one.
And now let's click on this Save and move.
If we move over to animation tab and then click over here and choose enemy to Underscore attack.
And then click on this play button.
We can see the attack animation just playing over again and again by now as you can see.
So we have the animation by now.
So we can create a attack animation clip as well.
But I'm going to leave it for now.
And if we select our animator GameObject in the hierarchy, move over to animator tab.
We can see we have two animation clip.
We have the idle one as well as attack.
So if I just click on this play button in the top, you will see this idle animation will be played
right after the game starts.
So we also need to play this attack animation clip once our player GameObject in attack range.
So just wait for it to compile.
As you can see, enemy is now playing the idle animation clip over again and again, so it's also need
to play this attack animation clip.
So let's get out of play mode and let's make transition from this idle to attack.
So in order to play the attack animation clip.
So I'm going to right click Make Transition to Attack.
And let's select this transition or this arrow over to Inspector.
And we're going to untick this has exit time because as soon as this player game object in attack range,
this enemy two game object need to play the attack to animation attack animation clip right away.
So that's why we just untick the has exit time as well as we're gonna set the transition duration to
zero.
And let's scroll down.
We need to choose a condition in which condition we want to play that animation clip.
So we're going to choose some certain condition or parameter in order to do that.
So let's go to animator tab.
In the left side you can see layers.
And in the right side we can see parameters.
So let's click on this plus icon and then choose this bool parameter.
And you can call this parameter to anything that you like.
So I'm going to call it something like attack.
And I want to choose this condition.
So let's select the transition.
Let's go to Inspector.
Scroll down and we're going to click on this plus icon in order to choose the condition.
So if attack parameter is true.
So if we set this attack parameter to true basically check you will see this idle animation will be
played right away.
Sorry not not idle animation.
Basically the attack animation clip.
It's gonna play right away.
And also we need to make transition from this attack to idle.
Once our player game object is not in attack range of this enemy two game object.
So we also need to back transition from this attack to idle, or we will stack forever into this attack
animation clip.
So let's right click on this attack animation clip and let's make transition back to idle.
And let's select the transition.
Go over to Inspector and we're going to untick this as exit has exit time as well as I'm going to set
the transition duration to zero.
And if we scroll down.
So we need to choose a condition in which condition we want to play the idle animation clip.
So I'm going to choose the same condition which is attack we recently created as you can see attack
parameter.
So we're going to choose that condition.
So let's click on this plus icon.
And now we're going to check if the attack is false.
So if we set this attack parameter to false basically uncheck you will see this idle animation clip
of this enemy game object will be played right away.
So let's have a look.
And we're going to set this attack parameter Manually and later on we can control this through our script.
So let's click on this play button and let's have a look.
So if we set this attack parameter to true manually basically check you will see this attack animation
will be played.
Why.
Because we have chosen that condition from this idle to attack.
So if I set this attack parameter to true, as you can see, as soon as we have set it to true, the
attack animation is just playing over again and again in this enemy two game object.
And if I set this attack parameter to false, basically uncheck.
Now this enemy game object is playing the idle animation clip, as you can see.
So what we basically need to do, we need to control this attack parameter through our script depending
upon where player is.
If the player is close enough to this enemy game object, then this enemy game object need to play the
attack animation clip.
Basically, we need to set this attack parameter to true.
So let's get out of play mode.
I hope you get the idea that how you can do.
And now we need to control this attack parameter through our script.
So let's do that.
So we're going to do that.
So let's select our animator GameObject in the hierarchy.
And let's open up the animation to script right away.
And as you can see uh, we when we created all of this animation clip, basically this attack, uh,
enemy to idle and this attack enemy to attack, we can see an animator component just added right away.
Once we created all of this animation clip.
And this animator component is responsible for switching our animation.
So what we basically need we need a reference of this animator component of this animator game object.
Then we can set the parameter.
So let's open up the animator script right away.
And let's uh go to the top of our class and let's grab the reference first.
So I'm going to make a public.
And the component we're looking for is animator.
So we're going to type animator sorry any mater.
And we're going to call it something like animator.
And let's close that up with semicolon.
And we're going to press down Ctrl S to save our script.
And let's move to unity.
So underneath our enemy two script we will see that animator field.
And by default it's going to be empty.
So what we need to do right there we need to drag this animator component into that slot.
So we're going to have this animator reference.
Then we can just set this attack parameter through our script.
So as you can see animator and it says animator.
So we'll select our animator and drag it right there.
Now we can see we have the animator anime to animator component reference, and we are ready to set
this attack parameter through our code.
So let's go to script and we're going to move over to this call info.
As you can see we just printing out a message in the console says a player in attack range.
So instead of printing out message what we want to do once our player GameObject get inside of this
circle, then this enemy two GameObject need to play the attack animation clip.
Basically, we need to set this attack parameter to true.
To true true.
Uh, to uh to true through our code.
So we do know we have the animator reference.
So let's do that.
So what we will do, we will go over to animator component.
And then we're going to use this dot set bool function.
So why are we using this set bool function.
Because this attack parameter we can see in the animator tab is a bool parameter.
This attack we have chosen a bool parameter.
So that's why we need to use this set bool function as a first argument.
This set will in in quotation or in.
It's going to take the string name or the parameter name in quotation.
And the second thing it's going to take the value.
So first thing it's going to take the parameter name.
So make sure you just do inside this quotation.
And the parameter name is attack.
And the second thing comma it's gonna need the value.
So we're gonna set it to true basically means check.
And we do know once we set it to true means take.
It's going to play the attack animation clip right away.
So that's why as a second argument we're going to pass that true.
And let's close that up with semicolon.
So basically what we're doing we're just moving over to this animated anime to game object, this animator
component.
And then by this animator component which is setting this attack parameter, uh, this attack bool parameter
to true, as you can see, uh, over here.
And let's suppose if the player is not in, uh, inside of this attack range of this player, sorry,
anime to game object means, uh, this player game object a little bit far away from this anime to game
object.
So in that moment, what this anime to game object need to do it need to play the idle animation clip
instead of the attack, because our player game object is just gonna be far away from this anime to
game object.
So what we will do, we will go to script and we will check else, so else.
The player is far from our this article is of this anime two game object, or this player game object
is not collided to this anime two game object circle yet.
So in that moment what we it's this anime two game object need to do.
It's it's need to play the idle animation clip instead of the attack one where basically a player game
object is little bit far away.
So that's why so else our player is not attack range means our player game object is far from our anime
game object.
So in that moment our anime two game object need to play the idle animation clip instead of the attack
one.
So we're going to move over to animator, and we're going to use this set function one more time in
order to set the pool as a first argument in quotation, it's going to take the parameter name.
So in our case this attack.
And the second thing it's going to take the value.
So we're going to set it to false basically means unchecked.
So we do know once we uncheck or set to false it's going to play the idle ideal animation clip instead
of the attack one.
Basically, we have chosen that condition as you've seen.
So we're going to close that off with semicolon.
And let's press down control S in order to save our script.
Now let's move over to unity.
You will see this, uh, this enemy two GameObject.
Uh, just gonna play the idle animation clip once our player GameObject not inside of this attack range.
Uh, attack enemy to a game object.
So just wait.
And now if we just click on this play, uh, play button in the top, you will see that.
So let's get inside first.
We jumped a lot.
So let's as you can see now enemy just uh, playing the attack animation clip over again and again.
And once we go outside of this attack radius or attack range of this enemy two game object, we can
see anime to game object is now playing the idle animation clip over again and again, so we can take
a look in animator tab as well as you can see.
So we're going to get out of play mode.
I hope you get the idea that how you can just do that and we're going to move over to this overrides
and then click on Apply All in this animator game object.
So the change is over here we made it's going to be saved into this anime two game object prefab as
well.
And I want to move this animator component to the up.
And I want this anime to script in the bottom.
So how we can do that?
In order to do that, we need to open up this or in prefab mode so we can see in the right section open.
So let's click there and we're going to right click on this animator component.
And we're going to click on this move up in order to move it up.
And now this anime two game object is now in the town.
And we can go back by clicking over here in hierarchy and into our scene view.
And we're going to press down Ctrl S in order to save our project.