Starting the archer! - Complex Enemy Behavior - 2D Platformer - Part 18 [unity]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is up guys welcome to Barden my name is Heinrich and today we're gonna take a look at creating the second enemy in our enemy series so let's just get right into it so let's start off by taking a look at another finite state machine diagram that I made for our second enemy as you can see we're going to be reusing a lot of our states we have our move state idle State player detected state melee attack state look for player's state and then our dead and stun State and as you can see we only have two new states that we need to make our dodge state and our range attack state so let's go into unity and let's start off by importing our new sprites so let's go to our sprites folder go to our enemies folder and then let's just drag that in and then we can set our pixels per unit to 16 sprite mode is multiple filter mode is point no filter and then we can change the compression to none hit apply go to the sprite editor hit slice grid by cell size and 64 by 64 is Right begin that head apply and now we have all of our sprites perfect okay and now let's create a new empty game object for our second enemy call it enemy two and then create a child game object that will hold alive just like our first enemy and before we start with any of the scripts let's just go ahead and make all the animations for our second enemy so let's start off by clicking on the alive game object and adding our animator component and then we need to go to our animator controller folder and create a new animator controller and this one we'll just call enemy to AC we probably should rename our first one to enemy one ac like that now let's click on our alive game object and drag in our controller and now we can pull up our animation window and we can start creating our animations so let's click create and then navigate to our animations folder enemies and let's actually just make a new folder for our enemy one and then let's just drag all of our old animations in there we can't select all of them so we'll do that later let's create a new folder called enemy to open that up and let's start with our move animation so say hey enemy to underscore move go ahead and save that and then let's go back to our sprites folder and then come to our enemy two sprites and the first eight sprites is our walk animation so actually zero to seven go ahead and drag that in we can change our sample rate to 15 and now let's just make sure we know where this enemy is so just reset the transform and let's move that up here whoops like that just play this animation we probably can't see it okay so as you can see our animator added our sprite renderer component for us who need to make sure we come in and set the sorting layer to enemy there we go now we can see him and we can watch our walk animation it's a little bit fast let's maybe change this down to ten yeah I like that a little bit more okay cool now let's make our next animation which is our idle animation so enemy to underscore idle and these frames should be frames eight to twelve eleven yeah drag those in change the sample rate to five as this should be a little bit slower that looks good to me and now let's create our next animation which is our shoot animation so enemy to underscore range attack and that should be frame twelve I'm not quite sure let's see how many frames we have so we have one so frame twelve to frame twenty-two so we have frames twelve to twenty to track that in change the sample rate to 15 let's see what that looks like cool so as you can see the animation is quite fast so I'm going to just paste all the frames a little bit so we have the draw animation like that and here the bow is fully extended so let's maybe have him hold this position for about three frames before he shoots that looks a lot better maybe one more in four frames go and then here we shoot might have him hold this frame 1 Frame longer like this so now our animation looks like that I like it perfect ok ok next we have our melee attack animation for the archer so let's create a new clip call it enemy to underscore melee attack and this time its frame and this time it sprites 23 to 32 drag that in change the sample rate to 15 again let's see what that looks like as you can see it's again really quick so maybe I'll have them hold this position for an extra frame and then we'll have it hold this this frame one longer so move this one let's see what that looks like looks good to me okay so now we have our move idle range attack melee attack animations now although we have left is our dodge animation and are stunned animation so I should start off with our stunt animation because that's going to be a little bit easier so enemy 2 underscores stunned like that and that should be the last 4 frames drag those in change the separator I've looks good to me okay so now for our dodge animation let me just open up the sprite editor so we can see we have these three frames now we have our anticipation frame which is just the character crouching down before he jumps and then we have a frame for moving up and a frame from moving down so we're gonna use the same thing we did with our player if you take a look at the players animator where we have the jump full entry where we have the three different frames depending on what stage of the jump we are so let's just click on our a live game object again go back to our scene and let's create the anticipation frame so enemy - it will actually just call it dodge start save that and I think that should be frame 33 yep so just drag that in there we'll change the sample rate to 15 and now instead of only having one frame in here we're gonna copy this and paste it here this way we can change how long we want the anticipation animation to last just gonna put it like that and now let's create the Dodge up animation and the Dodge down animation so enemy to underscore Dodge up and that should be framed 34 yep drag that one in this will just leave as is because it's only one frame and then next we have Dodge down so create a new clip and then you to underscore dodge down and 35 hips of frame 35 is our dodge down animation perfect so now we have all the animations we're gonna need for our enemy - so you can just go ahead and close the animation window and now let's start taking a look at the states so first and foremost let's come to our scripts folder go to our enemies folder enemies specific folder let's create our enemy - folder and now in our enemy - folder let's create a new c-sharp script we're going to call enemy too so this is our enemy to specific entity class basically let's go ahead and open that up and then we can come back to unity let's take a look at our state diagram again so let's go the same route we went to enemy 1 and just start with our move and idle States so back in unity in our enemy specific enemy 2 folder let's create another c-sharp script and this time we'll call it e to underscore move state and create another script called e to underscore idle state ok so let's start off with our move state so let's go ahead and open that up and then we can get rid of this pre generated code make it inherit from our move state and then we can generate our constructor and now we need to get a reference to our enemy - instead of enemy 1 this time so we have a private enemy - called enemy and then in our constructor we just add enemy to enemy and then we say this dot enemy equals enemy and the next we can generate our function overrides so click on the name control full-stop generate overrides and we don't want the first three same old same old you can go ahead and save that and now before we do anything else in here let's take a look at our idle state so e - idle state go ahead and delete this code make it a narrative idle state and then let's add our constructor get the reference to our enemy so private enemy to enemy add it to the constructor inside the constructor we want to say this enemy equals enemy and then that's add the overrides just like that go ahead and save that okay now let's take a look at all enemy to class again we can go ahead and get rid of this code make it inherit from entity same as you did of our enemy one and you can always go and look at our enemy one class and see what we did in case we need to remember so as you can see first we have we declare all of our states then we have the data for all of our states and then we just initialize those States in our start function so let's come back to enemy - okay so let's start with our move state so we're going to have a public key to underscore move state whole move state and don't forget the public getter and the private setter next we have our idle state so public key to underscore idle state cold idle state public get private set and then next we have our data and in this case we're going to have a C relized field private and because we start off all of our data state names with AD underscore we can get a nice list of all the scripts we have available so we're looking for our D underscore move state that we'll call move state data and then next we have our private d underscore idle state hold idle state data now let's go ahead and create the start function and then in the start function we can create our state objects so move state equals a new key to underscore move state and for our entity we're passing this for our state machine we're passing state machine as you can see we didn't have to declare state machine again because it's part of entity which we inherit from then we need our animation boolean name which in this case is going to be move then we have our state data which is move state data and enemy two is this now there might be a way to make this process even more efficient you things like generics but I'm not quite at that level yet I'll figure that out eventually and then when I do I'll be happy to share with you guys once again so next we have our idle state which is going to be a new e to underscore idle state this for the entity's state machine for the state machine idle for the animation named idle state data for our data I see I made a mistake there add an a at the end and then this for our enemy too now if we take a look at our enemy one class again you can see that we need to initialize our first state and I also noticed I made a mistake with the start function because as you can see over here we have public override void start where as an enemy too we have private void start so seeing as we're inheriting from entity and we have a public virtual void start function in entity we need to override it here so we'll change this to public override void start and then inside the start function we just need to add face dot start again this base our start is not required you just need it here if you want to call the code from the base class that we're inheriting from so let's not forget to initialize the first stage so we'll say state machine dot initialize and we'll pass our move stage as our first state let's just go ahead and click on enemy two and generate the rest of our overrides we can look at this we might not want all these functions we don't really need to override or checks at all we might want to later on but for now we're good we don't want to override our damage hop our stun resistance our flip we can add our on draw gizmos you don't need these we don't need these and I don't even think we need our update or fixed update so really we only want to override our damage and our on draw gizmo's function let me just go ahead and check in enemy 1 if that is the case yeah see we do not have our update functions here either cool so now let's go ahead and set up the enemy so that this will actually work so let's go back to unity and to start off with let's click on our enemy to a live game object and let's just throw in a default sprite so that we can see it when the game is not running so we can come to sprites enemies and we'll just use the first sprite cool now we can see him I am actually gonna use sprite 9 so it's our idle animation that looks a bit better so now we can go ahead and drag our enemy to script onto our enemy to game object like that and now we can start seeing everything that we need so we need to have our entity data or wall check position ledge attack position player check position ground check position our move state data and our idle state data so let's start with all the transforms so let's click on our alive game object and create an empty game object and we have four transforms to add I think yep so let's just go ahead and duplicate this three more times so the first one is going to be our ledge Tec position so we'll just call it let's check the second one is our wall check position third is our player check whoops and then fourth is our ground check so just go ahead and click on enemy 2 and drag all of these in so let's check goes to let check wall check goes to wall check player check goes to player check and ground check goes to ground check now before we can start seeing any gizmos for these checks we need to add our entity data so in our enemy specific enemy 2 folder let's create our data folder so create a new folder hold it data and then in our data folder let's just go ahead and create a new entity data base data and we'll just call it e to underscore base data and as you can see all of our defaults are set there we just need to member to set ground to ground and what is player 2 player and then maybe a default hit particle so let's just lock the inspector and go to our prefabs folder and we'll just use enemy one hit particle again so go ahead and drag that in and then now we can unlock our inspector and click on our enemy to game object and drag in our base data to our entity data slot and as you can see now we can see our different check distances that's good so let's reposition our let's check and wall check and all those things so the ledge check can go around here and then wall check can just be just around there player check should be around there then ground check should be at the bottom maybe a little bit up like that perfect okay let's see what else we need so next we need to have our move state data so let's create a new state data this is going to be our move state we'll call it e to underscore move state data go ahead and drag that in and then now let's do our idle State so data state data idle state let's just call it e to underscore idle state data and then let's drag that one in and now the only thing we have left is set up is our animator so let's click on our alive game object and go to our animator and as you can see all their animations we created happen set up for us but we'll just get to them as we get to them we'll keep move as our entry point seeing as our move state is our first state that we initialized to let's come to our parameters and let's create our move parameter which is a boolean so move and then also create our idle parameter and let's set up the transitions so from the start we'll just set up so from the start we'll just set it up the way we did for enemy 1 so we'll create a new empty state that will just call empty and then we can make our first transition from move to empty click on the transition make sure has exit is not ticked go into the settings change the transition duration to zero and add a condition this condition is move must be false because we're going out of the move state now let's add the transition back to the move state again has exit time is not ticked transition duration is zero and the condition in this case is move must be true now let's add our idle state to the mix so let's drag this one down here make a transition up to empty and tick has exit time transition duration is zero add the condition that idle must be false now the transition from empty back to idle untie Kazakhs a time transition duration is zero add the condition that idle must be true perfect so everything should be in place now let's give it a little test just go ahead and run the game okay so actually forgot a lot of things or a live game object still needs a rigidbody 2d and a Collider so let's go ahead and add a rigidbody 2d under the rigidbody 2d we want to make sure we remember to set the gravity scale to 8 change the collision detection to continuous and then under constraints we want to tick freeze rotation next we want to our Collider so let's go ahead and add a 2d box Collider and there you can just edit it down to fit our sprite so like that perfect ok let's give it a try another thing we forgot to add we should probably make a list of things that we need to add so we don't forget but another thing we need to add is our physics material because currently we set the velocity and then the enemy just stopped because he has friction so let's go to our materials folder and let's grab our planar material and drag that in there now it should work fingers crossed so close so as you can see our enemies started in the move state but he walked off the ledge we have not set up any of the state transitions yet so now we can go back to our code and now we'll work in our eetu idle and e to move state so if we take a look at our state diagram that we have here this is going to be the same as it was for anyone so if a Ledge or a wall is reached from our move state we're gonna go to our idle state and then once the idle time is over we're gonna go back to our move state so let's go to our script and then in our move state thing as this is where we start all we need to do is come down to our logic update function and we'll say if is detecting wall or not is detecting the ledge then state machine dot change state to enemy dot idle state like that and we also need to remember to set our turn after idle to be true or flip after idle so we'll say enemy dot idle state dot set flip after idle and we want to set this to true now if we just really look at our state diagram again you can see we also have a transition to our player detected state so let's just leave ourselves a little note a little to do saying transition to player detected state this way we won't forget let's take a look at our idle state and in our logic update function all we need to do is say if is idle time over then state machine dot change state to enemy dot move State and now it's set up now we also want to be able to detect the player from our idle state so let's just add ourselves another little to do and that's just shorten this to PDS for player detected state you will know what it means hopefully so let's jump back into unity and see if this worked as you can see our enemy detects the ledge goes back to the other side detects the ledge perfect of course these are also just the default parameters that we can mess around and change but as you can see this was a very small amount of code that I had to write to get this behavior back in so now let's move on to the player detected state so let's come back to our enemy to folder and let's create a new C sharp script that will call E - underscores player detectives state like that go ahead and open it up in Visual Studio we can get rid of this code make it inherit from player detected state boom generate the constructor add the reference to our enemy - so we have a private enemy - cold enemy and then add that to our constructor so enemy to enemy in the constructor we say this stott enemy equals enemy go ahead and generate deep overrides we don't want the first three and again we probably don't want a lot of these usually most of our code is going to happen in our logic update but I just like having them there it looks pretty it's all just personal preference and there we go there our enemy - player detected state is set up so let's take a look at our state diagram again as you can see we currently don't have any transitions back to our move or idle state from our player detected state so we'll have to move on to these other states but we can at least add these transitions in so long so back in our idle State in the start with this one we can get rid of our to do because that's what we're doing and we'll just say yes is player in min aggro range then we want to go to our player detected state so state machine stopped change state to enemy dot player detected state now we're not gonna get an autocomplete because we did not add this to enemy two yet but we'll just go ahead and finish the line like that and now let's go to enemy two and let's create the variables so we're going to have a public key to underscore their detected state cold lair detected state create the public getter and the private setter and then we need the data for the state so serialized field whoops sterilize field private d underscore player detected state hold clear detected state data and then we might as well come and call the constructor so player detected state equals a new e to underscore player detected state with this as our entity state machine as our state machine the animation boolean name is going to be player detected and then our state data is player detected state data and this as our enemy too so the error should have gone away in our idle state which it did and now we just need to remember to change the second if - else--if because we don't want this overriding this state change now let's go to our move state and we can do the same thing so get a little bit to do then we can say if is player in min aggro range then state machine don't change state to enemy dot player detected state and then just change this to and else if perfect so now we have our transitions to our player detected state let's see which state we want to do next so the next day we should do is our melee attack state because we still need to create the dodge and ranged attack states so this will be the quickest one to do now so let's head back to unity and before we forget let's create the data for our player detected state so create data state data player detected state and we'll call it e to underscore player detected state data click on our enemy to game object and then we can just drag that in now back in the enemy 2 folder let's create a new C sharp script we'll call this e to underscore melee attack state then we can go ahead and open that up and that's get rid of this code make it inherit from melee attack state we can go ahead and create the constructor and let's get the reference to our enemy too so private enemy - cold enemy and let's add that to the constructor which is getting quite long enemy to enemy this stopped enemy écoles enemy and now let's go ahead and generate the overrides so we don't want the first three just like that so from our melee attack state we either want to go to our look for player state if we cannot detect the player or to our player detected state if we do detect the player so we don't have this state setup yet so let's just create this transition so long so in our logic update function we can come and say if and in our melee attack state we know that we can transition out of the state once the animation is over and we have that setup in our base melee attack state so you just say if is animation finished then we either want to transition to our player detected state or our look for player state so we'll say if is player in min Agra range then state machine stops change state to enemy dot player detected state and now let's just leave ourselves a little to do to come back and add the look for player state so look for a player's state cool so now we can go ahead and go to our enemy to class and set up our melee attack so we'll come and we'll save public e to underscore melee attack state call it melee attack state and the public gather and the private setter next we need the state data so sterilized field private the underscore melee attack hold melee attack state data then we can go ahead and call the constructor melee attack state equals a new e to underscore melee attack state this as the entity then we have our state machine as our state machine our animation boolean name is melee attack and then next we have our attack position which is a transform that we still need to declare so let's go ahead and do that as well so have another serialized field private transform cold melee attack position let's just pass that to the constructor and then next we have our melee attack data and then finally we pass this as our enemy so currently we already have our transition going out of our melee attack state now we just need our transition going into our melee attack state so let's go to our player detected state which if we look at our state diagram we can go to our melee attack state from our player detected state and then in our logic update function will just say if perform close-range action then state machine dot change state to enemy dot melee attack state like that now we just need to go to unity and set up everything for the attack so first of all we know we need our new attack transform so just go ahead and create that as a child game object of a live and we'll call it melee position then click on enemy to go ahead and drag that in and then we also need our melee attack state data so let's go to our data folder create data state data melee attack state we'll call it e to underscore melee attack and then we can go ahead and click on our enemy 2 again and drag that in I'm seeing I'm being very inconsistent with the naming again so I'll just go ahead and fix that so it's a melee attack state data spelling mistake here cool so now which need to set up the animation so let's go back to our animator here we have our melee attack we can just go ahead and make a transition from it to our empty animation state untuk has exit time transition duration is 0 and then we need to add our condition or our boolean parameter which is melee attack you can go ahead and change the condition to melee attack is false and it's add the transition back from empty to melee attack untick has X a time set their transition duration to 0 and the condition this time is melee attack is true the other one is false perfect now we also need to remember to add that other script that we made that allows our a live game object animations to communicate with our States so this animation to state machine script let's go ahead and add that to our a live game object just throw that on here like that perfect now we need to go back to our melee attack animation and we need to create those animation events so at the punch point we want to add an animation event and we want to call trigger attack and then at the end of our animation we want to add another event and call finish attack like that go ahead and run and see what happens and see if we forgot anything he detects us and then if we get super close he tries to punch us perfect clear detector does not exist did we forget so we have not yet added our player detected animation so let's go back to our animator now I don't have a specific animation for player detected so we'll just use the idle animation as well so let's create another state and then we'll name it enemy to underscore player detected like that let's add the parameter which is a boolean called player detected and then let's add the transitions so make a transition to RMT make sure has exit time is not ticked change the transition duration to zero add the condition which is player detected is false and then let's make the transition going back an tick has exit time transition duration is zero and player detected is true perfect oh okay so we need to also remember to come to our a live game object and set its tag to enemy even though we're not using the tag anywhere yet you know might as well and then set the layer to damageable and we want to say no only this object because it doesn't matter what layer our checks are on so you can see now he tries to punch us it's not doing any damage yet I wonder why ah okay so now we also need to remember to come back to our data and for our melee attack state data what is player needs to be set to player you should be able to damage us now yep as you can see he can damage us as you can see is gonna be stuck in the attack animation because we have not yet set up the transition if we do not detect the player but so everything is working only thing is we cannot see our attack radius so let's go ahead and add that we need to add that in our enemy 2 on drawer gizmos function so we'll say gizmos dot draw wire sphere and we're drawing that on our melee attack position position and the radius is going to be our melee attack state data dot attack radius so we should be able to see it now there we go so I should go ahead and position that a little bit better we can go ahead and take a look at our melee attack animation so our punch is around there so just move it like this I think that looks good perfect so now we have a media attack go ahead and see what that looks like again boom perfect ok let's see what's next so I think next we should create the look for players state and we'll just go ahead and create all the other states that we already have before we look at our ranged attack and dodge States so let's go ahead and go back to our enemy to folder and let's create a new C sharp script that will call e to underscore look for player state and then let's just go ahead and open that up so long we can go ahead and delete this code and make it inherit from our look for player state now let's go ahead and generate our constructor and then let's get a reference to our enemy - so private enemy - cold enemy and then add that to the constructor so enemy - [Music] enemy this dot enemy equals enemy and then we just need our function overrides we don't want the first three click okay perfect now I know I keep pulling this up but it helps us see what we need to do so currently we only have one state that goes to our look for players state because we haven't made these yet but then actually now I'll I we have to because we also have our damage function but we'll deal with that one later for now we'll just implement this transition and then from our look for player state we have two transitions either to our move state or our player detected state so we can implement all three of those states right now now it actually looks like I forgot another transition in this diagram from our player detected state to our look for players state so if our enemy no longer detects our player we want to look for the player before we go back to moving guess let's go ahead and implement that so to start off with let's head to our enemy to class and add our look for player state so we first need to have a public key to underscore look for player state cold look for players state create the public getter and the private setter and then next we need the data for the state so sterilized field private de underscore look for player state fold look for player state data and then let's go ahead and call the constructor so look for players state equals a new D to underscore look for players state and we'll pass this as the entity state machine as the state machine our animation boolean name is look for player next art data is look for player state data and then finally our enemy to is this cool so now look for player state should be in the autocomplete when we try to transition to it so let's start off by going to our player detected state and we'll just come and say else if his player in max Agra range is no longer true so we have a exclamation mark in the front then we want to transition to our look for player states so state machine dots change state to enemy don't look for player state like that now let's do it in the melee attack state as well so if the animation is finished else if not is player in min aggro range then we want to say state machine dot change state to enemy dot look for player state and then we can just go ahead and get rid of this to do like that okay now in our look for players state we want to come to our logic update function and we simply want to say if is player in min aggro range meaning we have detected the player then state machine dot change state to enemy dot player detected state else if is all turns time done then state machine dot change state to enemy dot move state like that so let's go back to unity and now we just need to remember to add our state data so we'll come to our data folder create a new data state data and we're looking for are look for payers state call it e to underscore look for flavors state data and then we can just go ahead and add that to our enemy too and now let's set up the animation so let's head to the animator again we're just going to use our idle state animation for our look for player state animations let's create a new empty state call it enemy to underscore look for layer and then let's go ahead and add the parameter so it's a new bully cold look for layer and I'm going to drag it to the top because it's gonna make the transition a little bit faster so you can make a transition from look for player to empty click on it make sure has exit time is not ticked under settings change the transition duration to zero add the condition and the condition is look for player must be false next we add a transition from empty back to look for player set has exit time to false transition duration is zero and the condition look for player is true perfect so I think that's everything we need so let's go ahead and run the game and we'll just wait from the move so now if he spots us he should stop moving and then if we leave the max agar distance he looks for us and then he starts moving again everything is working perfectly so next we have our stun state so let's go ahead and go back to enemy 2 and let's create a new C sharp script pull it e to underscore this is done state and then we can go ahead and open that up so long get rid of this code and then make it inherit from stun state then we can generate the constructor get the reference to enemy 2 so private enemy to hold it enemy add to the constructor so enemy to enemy and this stopped enemy equals enemy and now let's go ahead and create the overrides we don't want the first three perfect so now in our stun State what do we do we want to wait for the stun time to be over and then once it is over if you look at our transition diagram it looks like I did not add them so let's just ignore that but basically once the stun time is over we either want to go back to our player detected state if we detect our player or we want to look for our player so in our logic update function will say if is stun time over then we want to say if is player in min Agra range then state machine then state machine dot change state to enemy dot player detected state else so our player is no longer detected state machine dot change state to enemy dot look for player state just like that now we're just need to take care of transitioning to our stunts eight and we do that from our enemy to class so in our damage function we want to say if is stunned because remember this is a boolean that gets set to true once our stun resistance goes below zero and our current state so state machine dot current state does not equal our stun state although we have not yet declared our stun state in our enemy too so let's go ahead and do that quickly so we have a public key to underscore stun state and we'll just call it stun state have our public getter and our private setter then we need the state data so sterilize field private d underscore stun state call it stun state data then we can come and call the constructor so stunned state equals a new e to stun state will pass this as the entity state machine as our state machine the animation boolean name is stun and then for state data we have stun state data and then finally we pass this as our enemy to so now we should be able to reference it in our damage function cool so if we are stunned and our current state is not stunned then state machine dot change state to stun State okay so let's go ahead and save that and then let's try it out we seem to make sure we add the state data so we'll come to our data folder create a new data state data stun state data call it e to underscore stun state data and then we can go ahead and drag that in so we'll click on enemy to drag this in now you can click on our stun state data object again and our set our knock-back angle to 1 1 for x and y and now we just need to set up the animation so we'll come to our animator we have our stun animation already set up so it's just position that better and then we need to add our boolean parameter which is just called stun let's drag that back up to the top and then our first transition is from mt to stun state then we can click on the transition on tick has exit time set the transition duration to 0 and add the condition that stun is true then we have transitioned from stun back to empty set exit time to false transition duration is 0 and our condition this time that stun is false so let's go ahead and try that out so we can hop over and we can damage him oh the knock back is quite big as you see he comes out of the stun all good perfect oh and I just got double-teamed okay oh well so our Sun velocity might be a little bit high so let's go to you to stun state data and the stun knock back speed let's do like 8 and I also noticed when we damaged him he wasn't hopping and I think that's because our default damage hop speed is set to low so just go ahead and change that to 10 and I think now if we damage the enemy he should hop yep as you can see he hops and he gets stunned and he recovers and he'll punch us perfect so now finally before we move on to our range attack and dodge states we just need to add the dead state so let's go back to our enemy 2 folder create a new C sharp script call it E you underscore dead state you can go ahead and open that up and then get rid of this code make it inherit from dead state and then we can generate the constructor add our reference to enemy two and then add it to the constructor so enemy - let me just stop enemy equals enemy and then let's get the function overrides so this was all a bit pointless because we don't actually do anything in here yet but it's good to have it for when we want it so now I just need to come to enemy two and then create the dead state so public e to underscore dead state call it dead state and then we'll just have a public getter and a private setter then next we need the data so sterilize field private key underscore dead state call it dead state data and then we can call the constructor so dead state equals a new e to underscore dead State this as our entity state machine is our state machine dead as our animation boolean name dead state data which I misspelled again so just ctrl R twice on that and then we can get rid of the L perfect and then this as our enemy so if you wanted to play a specific dying animation first you could do it in the dead State I also forgot to put a semicolon after dead state data so I'll just add that in there and now if we go back to unity go toward data folder and then create a new data state data dead state data you can see we have the two slots for our particles such as call this e to underscore dead state data and that's lock the inspector and then go to our prefabs folder add our death blood particle and our death chunk particles like that and seeing as we disable the game lobby for now we don't have to bother setting up anything in the animator but if you want to go ahead and now before we can actually kill him when you just set up those transitions in our damage function so in our damage function we can say if is dead which is our boolean that gets set to true once our health goes below zero then state machine dot change state to our dead state like that and then don't forget to change this to else if now I want to make it so that when I hit the enemy from behind he will immediately turn to face me so we can just say else if and the condition for this if is that our player is not in front of us so we can say not check player in min Agra range then so in here we'll just transition to our look for player state but seeing as the enemy got hit from behind you know he will intuitively know to immediately look behind him so here we can say look for player state dot set turn immediately to true so remember this is the function that we added in our look for player state so if this is true we will immediately turn once we enter that state so then we can just say state machine doctor change state to look for player state like that and we forgot to add our brackets for the function perfect so if we run the game now we should be able to kill him but let's also hit him from behind and see if that worked so as you can see he turned immediately and he detected us and we can still not kill him why okay we have an error let's see what that is okay so I forgot to add the data to the enemy so click on enemy to unlock this go to our project go to our enemy to data folder and drag the dead State into the slot now we should be able to kill him let's give it a try boom and he's a dead perfect cool so I think this is a great place to end this episode we've added all of the states that we've already created to this enemy and then next we just need to create the dodge state and the range attack state and then we're done with this enemy so I hope you guys saw how much faster it was to create the second enemy and before you know it we're gonna be done with it completely and then we can move on to some other fun things now before I go I just like to give a huge thank you to all of my supporters and wonderful people over on patreon you guys are awesome and a huge special thank you to Gregory Miguel and you paw for your support on patreon you guys are awesome and yeah so I hope you guys all have a wonderful day
Info
Channel: Bardent
Views: 5,663
Rating: undefined out of 5
Keywords: Unity., tutorial, player, 2D, platformer, walljumping, wall sliding, jumping, Unity, Animation, ground, check, physics2d, castcirlce, variable, jump, height, Wall, Jumping, Movement, improvement, user, friendly, code, 2019, 2019.2.0f1, Ledge, climb, dead, cells, system, easy, beginner, animation, Dash, Ghosting, After, Image, After Image, Basic 2D Combat, Unity combat, Comabt, Combat, Melee, Melee Combat, Basic, Enen, Enemy, Patrol, State, Machine, respawn, hit, finite state maching, state machine, enemy, behavior, Charge
Id: LbdrPy8oXco
Channel Id: undefined
Length: 56min 7sec (3367 seconds)
Published: Sun May 10 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.