Let's make transition between animation state of this anime game object.
So we're going to select our anime one GameObject in the hierarchy.
And let's move over to this animator tab.
So we need to make transition to this shooting as well.
So we're going to do that.
Uh so let's right click on this idle make transition to shooting.
And let's select this transition.
Go to Inspector.
And we're going to untick this as exit time.
Because as soon as this player game object inside of that attack range, this anime game object need
to play this animation clip right away.
So that's why we untick and we're gonna set the transition duration to zero.
Scroll down.
We need to choose a condition in which condition?
We just want to play that shooting animation clip of this anime game object.
So I'm going to choose a condition.
So let's go to Animator Parameters.
So let's click on this plus icon.
And we're going to choose a bool parameter which can be true or false.
And we're going to call this something like attack.
So I'm going to choose this condition if this attack parameter is true.
So we're going to click on this plus icon in the inspector.
And we're going to check if attack is true then it's going to play.
Or we're going to play this shooting animation clip right away.
Because as you can see we have chosen that condition.
So I'm going to click on this play button in the top.
And I'm going to set this attack parameter manually.
Check basically means true.
And as soon as we're going to set it it's going to play the shooting animation clip.
Why?
Because we have chosen that condition, as you can see in the inspector.
So I'm going to check this manually basically shooting up.
Plus basically you can simply say we're just setting it manually.
And if I set this attack parameter to uncheck basically false, we can see we're not moving over to
at all our idle animation clip.
So what we need to do we need to play the idle animation clip one more time depending upon where the
player is.
If the player is far from attack range, Then it should.
Then this player game enemy game object need to stop playing that shooting animation clip right away.
So let's get out of play mode and we're going to make transition back to this idle.
So let's right click on this shooting.
And we're going to make to back to this idle.
And let's select this transition.
And we're gonna untick this as it has exit time as well as I'm going to set the transition duration
to zero.
And we're going to set this condition to false.
So let's set this to false.
Basically we have chosen if the attack parameter is false it's going to play this idle animation clip
one more time.
So what we need we need a reference of this animator component.
So if I select my enemy game one GameObject in the hierarchy hierarchy and go over to Inspector, we
can see a animator component is attached to this enemy one game object and this one is responsible for
playing our animation clip.
So what we need?
We need a reference of this animator component.
Then we can set this attack parameter to true or false, depending upon where player is a player is.
So basically we need to get a reference of this animator component.
Then we can set this attack parameter through our code.
So let's grab the reference first of this animator component.
So we're going to open up our anime script.
And let's go to the top of our class.
And we're going to make a public.
And the component we're looking for is animator.
So we're going to type animator.
And we have to name name it.
So I'm 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 now let's move over to unity.
So underneath our anime script we're going to able to see that animator parameter.
So we need to drag this animator component into that slot.
So we're going to have the reference.
And then we can set this parameter to true or false depending upon where player is.
So as soon as script gets compiled we can see animator and it says none animator basically empty.
So we're going to take our animator and drag it there.
There.
Now we can see we have one anime, one animator component reference.
And now we are ready to set.
So let's double click that.
And over here as you can see if call info is equals equals to true basically means this player game
also gets inside of this attack range of this attack range enemy game object attack range.
So what are we doing?
We just printing out a message in the console.
Console says attack on player.
Instead of printing out message, we need to play that animation clip.
So we will go to animator and we're going to use this set bool function.
Set bool and make first bracket.
Why are we using this set bool function.
Because if we move to unity this attack parameter is a bool parameter.
So that's why we need to use this set bool function.
Firstly we moving over to animator, basically this animator.
And then we using that self bool function in order to set this parameter as first argument.
This set bool function, it's going to take the parameter in quotation the name of the parameter.
So make sure you type quotation and you need to spell it correct, which is attack.
We do know, so make sure you just go to unity and check it.
And the second thing comma it's going to take the value that you want to set.
So we're going to set it to true means checked.
So once the player get in attack range then we need to play the attack animation means we need to set
this attack parameter to true means state.
So when set to true.
And let's close that up with semicolon.
But let's suppose this player game object.
It's not in attack range of this enemy game object.
Let's suppose this player game object a little bit far away from this enemy game object.
In that moment, this enemy game object need to play the idle animation clip instead of the shooting
animation clip.
So that's why we're going to check.
Else a else and then make some curly brackets.
So else our enemy is our player is not in attack range means call info is true in that moment that enemy
game object need to play the idle animation clip instead of the attack.
So I'm going to move over to animator.
We're going to use this set function in order to set.
Let's make first bracket.
And in quotation we're going to pass our parameter.
And the second thing we need to set the value.
So we're going to set equal set false uh basically unchecked.
And we're going to press down control S in order to save our script.
And now let's move over to unity.
So as soon as this player GameObject gonna get inside of this enemy one game object, uh, attack range,
you will see this enemy game object.
Just gonna play the shooting animation clip right after this, uh, player game object gets inside of
that attack range.
So let me show you.
Firstly, I'm going to select my player GameObject in the hierarchy, and I'm going to drag it a little
bit further away from this enemy GameObject somewhere over here.
And now if I just click on this play button, as soon as this player GameObject gonna get inside of
that attack range enemy GameObject, then this enemy GameObject will play the attack animation or the
shooting right away.
So as you can see, it just normally playing the idle animation clip, and as soon as player GameObject
gets inside of this attack range.
As you can see, it's playing.
Now the shooting animation clip.
And if we just, uh, passes through this enemy GameObject, now we can see this enemy GameObject just
playing the idle animation clip instead of the shooting one.
Why?
Because this player game object.
As you can see, it's not inside attack range of this enemy GameObject.
So basically that is the thing.
And let's get out of play mode.
And now what we have to do this enemy game object need to shoot a bullet.
So if I select my enemy one game object in the hierarchy, go over to the animation tab and then click
over here.
In order to choose the shoot animation, we want to shoot a shoot something over here.
So this is going to be a perfect time in order to shoot on this player game object.
So what we're going to do we're going to make a shooting function.
And then in that shooting function we're just going to spawn our bullet.
For now let's create that.
So let's go to script.
And we're going to do it somewhere over here.
And make sure you just making this function public.
Because we're going to call this function over here in animation tab over something over here.
So that is the reason.
And for function we're going to type void.
And we have to name the function what we want to name it.
So I'm going to call it something like shoot and make first bracket and some curly brackets.
For now we're going to print out a message and later on we're going to spawn our bullet.
So we're going to type debug dot log in order to print out a message.
And we're going to print out something like a shoot.
Shoot.
Let's close that up with semicolon.
And let's press down Ctrl S in order to save our script.
And let's move to unity.
So we need to call the function we just made the function.
We also need to call the function.
But where we're going to call it we're going to call it through animation event.
So if I select my anime anime one game object in the hierarchy, go to animation tab and then click
over here in order to choose the animation.
And we're going to call the function over here.
So this is going to be perfect time as you can see.
So we're going to click on this Add event in animation tab in the left bar over here.
Basically you're going to get it select that.
Go over to Inspector and it says no function selected.
So let's click there and let's go to anime script methods basically function.
And we're going to choose the shoot function.
Basically we just going over to this anime script And then we just are calling this function.
As you can see, we created over here, which is the uh function.
And now and we're just calling it over here.
So and what should function is doing.
We're just printing out a message in the console.
So as you can see.
And let's go to unity.
And I'm going to select my console tab.
And let's click on this play button in the top in order to play our game.
And as soon as this play enemy game object uh gonna play attack animation clip, you will see that message
in the console.
Just gonna pop up says shoot because we're calling the function through our in animation event.
So just wait as soon as this player game object just gonna get inside.
As you can see, as soon as get it's just playing the attack animation, this enemy game object and
the short message just getting pop up every single time.
This enemy game object playing the short animation clip because we calling the function inside through
uh, inside through animation event.
So I'm going to get out of play mode.