Dash - 2D Platformer Player Controller - Part 26

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is up guys welcome to barton my name is heinrich and today we're gonna give our character the ability to dash so here's just a quick little demonstration of what we're going to make as you can see when the player gives the dash input the character goes into some sort of slow motion mode where he can then choose the direction that he wants to dash into and then once time runs out or the player lets go of the dash button the character will then dash in that direction the character will also have a after image trail that follows him okay so let's get started now before we can jump into the dash unfortunately there's some stuff that we have to fix from last episode the first thing we need to do is come to our project animation animations player and take a look at our ledge climb animation over here we need to go ahead and untick loop time this will stop our ledge climb animation from looping which will avoid some animation glitches that sometimes happen when we finish the ledge climb animation so that's the first thing the second thing is a bug that a user on the discord channel pointed out and let me show you what that is if we go ahead and come down here or just go to an area where we have a single block high little ledge and we run up to it and then jump our character proceeds to grab the ledge and as you can see this isn't quite what we want so let's go ahead and fix that quickly what we need to do is open up our player in air state so let's go to scripts player player states sub states and then player in air state so in this state we can take a look at our transition to the ledge climb so over here we say else if is touching wall and is not touching ledge then we transition to our ledge climb state so what's happening here is when the character jumps we go into the in air state we're not detecting a ledge but we are detecting a wall so we grab this ledge but we can use the fact the character is still very close to the ground to our advantage and so we can say don't do this transition if we are detecting ground so all we need to do is simply come to this else if and in the if conditions add and not is grounded so that's going to stop that from happening let's give it a try so as you can see we're over here and we jump we no longer go to that ledge climb state but we're not quite done yet if we go back to that same position and then hold control so we want to climb the wall the same thing happens so what's happening here is we go from our player grounded state to our climb state or our wall grab state and then from there we go into our ledge climb state so let's stop the character from being able to grab the wall if he's no longer detecting a ledge so all we have to do is come and open up our player grounded state and over here we have our transition going to our wall grab state so all we need to add in here is our ledge check but we're not doing a ledge check yet so let's go ahead and add that so we just need a private tool call it is touching ledge and then let's go ahead and add that to our do checks so is touching ledge equals player dot check if touching ledge we can then go ahead and come to this transition where we go to the wall grab state and just say and is touching ledge so that should take care of that problem let's give it a little test again so now if we grab the wall we no longer transition to that state perfect but again we're still not quite done there this bug is deeper than you think so now let's say we're standing in the same position we're holding in control and then we jump the same thing still happens and that is because we also have a transition going from our in air state to our wall grab state but again it's a simple fix so let's just go and add that same transition in there so in our player in air state here we have our wall grab state all we have to do is come and say and is touching ledge and that should do it let's just make sure it works so now if we grab the ledge jump nothing happens perfect okay so that's it for bugs now let's take a look at a little feature that i forgot to add and this is simply adding the ability to wall jump while we are hanging from the ledge like this so if i push spacebar now or a i want to just perform a wall jump so let's go ahead and add that quickly all we need to do is open up our player ledge climb state so sub states player ledge climb and then just simply add another transition over here or another else if in our logic update so we can just say elsif and here we want to read in our jump input so it's going to go ahead and create a variable for that we need a private bool call it jump input and then go ahead and read that in in the logic update so after we read in our x and our y we just say jump input equals player dot input handler dot jump input then in this else if we just say if jump input so we push the space bar and we are not currently busy climbing the ledge so not is climbing if this is the case we just say state machine dot change state to our player dot wall jump state simple as that we're almost done now we just need to remember that our wall jump state has a function called determine wall jump direction that takes in a boolean of whether we're touching a wall or not now in our ledge climb state we're not doing a wall check but we know if we're hanging from the ledge that there's a wall in front of us so we know what direction we want to jump in so all we need to do is come and say player dot wall jump state dot determine wall jump direction and we'll just pass it true seeing as we know that there's a wall in front of us and that should be everything we need to add for that let's give it a try so now if we go and hang from this ledge and we push spacebar we jump perfect that's exactly what i wanted awesome so now we're ready to get started with our dash state sorry that took so long so let's start off the way we always do by creating a new c-sharp script that we're just going to call player dash state then let's go ahead and open that up now if we take a look at our state machine diagram over here you can see that our dash state is part of our ability super state that's just because it's we give a single input we perform the dash and our ability state is going to take care of transitioning back to what whatever other state we're supposed to be in so our player dash state needs to inherit from player ability state we can then go ahead and get rid of this pre-generated code and then generate our constructor so right-click quick actions and refactoring generate constructor now let's go ahead and set up everything that we need in our player script so a little shortcut that we can do here and this should work for you is if you just hold in control and then click on player over here it should open up player like this so now we can go ahead and expand our state variables region and let's just go ahead and declare another public player dash state and we'll just call it dash state don't forget to give it the public getter and the private setter and that's it go ahead and minimize our state variables region and open up the unity callback functions region in our awake function we need to initialize our dash state so dash state equals a new player dash state with this as our player state machine as our state machine player data as our player data and then finally our animation boolean name is just going to be in air because we're just reusing our in-air animations for everything of course if you have a different animation for your dash feel free to add that in here and that should be all the setup we need to do in our player script now before we can move on to our dash script we need to take care of some input so let's go ahead and go back to unity and then if we go to our input folder and open up our player input actions asset let's go ahead and create a new action we can just call this action dash and we can leave the action type as button as it's just a button that we press to trigger an action we don't have to pass any values through now we can add some bindings so we can click on the first one click on path and then listen so for keyboard i'm going to use shift as my dash button so left shift keyboard and just make sure that you select keyboard as your control scheme go ahead and add another binding and listen and this time for controller i'm going to use the x button on the controller which is the west button so go ahead and select button west gamepad and then choose gamepad as your control scheme perfect now we can go ahead and open up our player input handler and set up our on dash input function now our dash input is going to work very similarly to our jump input where we have a jump input start boolean basically that gets set to true as soon as the button gets pressed and then automatically gets set to false after a certain amount of time or if the action gets used and we have another boolean that registers when we let go of the button so we can go ahead and create a new public boolean call it dash input give it a public getter and a private setter and then next we need our dash input stop so public boolean dash input stop with a public getter and a private setter and then finally we need to have our dash input start time so private float dash input start time like that now let's go ahead and scroll down and create our on dash input function so public void on dash input and of course this function takes an input action dot callback context as a parameter and we'll just call it context and now inside this function we can say if context dot started so this is as soon as the button gets pushed down then we want to set dash input equal to true we want to set dash input stop equal to false and we want to go ahead and set that start time so dash input start time equals time dot time then after this we want to go ahead and say else if context dot cancelled so this happens as soon as we let go of the button we just want to set dash input stop equal to true so dash input stop equals true just like that now we just have to go ahead and create a use dash input function so that we can set dash input to false once we enter the dash state so we have public void use dash input and all this function does is set dash input equal to false then we need to make a check dash input hold time function like we did for jump so private void check dash input hold time and now inside this function we can just go ahead and say if time dot time is greater than or equal to dash input start time plus our input hold time if this is the case we just say dash input equals false and that should be all the setup we need to do in here we just have to remember to take our check input hold time and put that in our update function now we just need to remember to go back to unity and set this up in our player input component so go ahead and click on player and then come to the player input component click the events drop down gameplay and over here we have our dash event so let's go ahead and add to the list and then drag in our input handler now from the functions drop down we can go ahead and select player input handler and on dash input perfect so now we have a dash binding now we can go ahead and work on our transitions to our dash state so before we do this there are some things that we need to consider so there are two things that we need to consider one is when are we able to dash so from which states should we be able to transition to our dash state and then we also need to consider that once we have dashed when do we reset the ability to dash so now in my case i'm going to keep this fairly simple i'm going to make it so that i can dash from any of my grounded states or my player in air state and my ability to dash is going to get reset when i'm on the ground but that leaves us with a slight issue because if being on the ground resets my ability to dash and i can dash while i'm grounded then i'm just continuously going to be able to dash which is fine we're just going to add a small cool down at the end of the dash say about half a second before we can dash again just to stop people from spamming dash on the ground but it's okay if the character keeps running and dashing to the side it's not going to take away anything from the gameplay of course you can go ahead and tweak the parameters of when you're able to dash to whatever suits your game but so let's go ahead and open up our dash state script and let's start off by declaring a public boolean variable that we're going to call can dash this variable is going to hold whether we currently are able to dash or not so we'll give it a public getter and a private setter and then next we need to go ahead and set up a cooldown for the dash so let's go ahead and open up our player data script so we come to our data folder player data and let's create a section for our dash state so square bracket header call it dash state and let's just go ahead and declare a public float and we'll just call it dash cooldown by default i'm just going to set this equal to 0.5 f so half a second now back in our player dash state in order for us to be able to use a cooldown we need to keep track of when was the last time that we left the dash state so let's just declare a private float and we'll just call it last dash time so now we need to use a combination of can dash and our last dash time to determine if we're able to transition to our dash state or not so let's just go ahead and create a function that's going to tell us whether we can transition to the state so let's create a public boolean and let's just call it check if can dash now in this function all we need to say is return can dash and time dot time is greater than or equal to last dash time plus our player data dot dash cooldown time so this function will return true if can dash is true which is going to get set when we enter our player grounded state and our cooldown has run out so if both these parameters are true this function will return true so let's take a look at how we're going to use this function when we transition to our player dash state so like i said from our player in air state we'll have a transition to our player dash date so the first thing we need to do is read in our dash input so let's come back up to our variables and let's declare a private boolean called dash input now we're getting quite a few booleans here it's getting kind of messy so let's just create some comments and organize this a little bit i'm going to create a section for input and another section for checks so let's go ahead and move all of the variables to the right position so here we have is all these are checks here we have some input grab input we have our dash input coyote time wall jump created time is jumping is touching ledge is a check and then these are just some other variables of course you can organize this however you want so now that we have a boolean for our dash input let's go ahead and read that in in our logic update function so after we read in our grab input we'll say dash input equals player dot input handler dot dash input now to set up the transition to our dash state before our final else let's go ahead and create another else if and in here we'll just say dash input and player dot dash state dot check if can dash so if we're giving dash input and our dash state is telling us that we can transition to it we can go ahead and make that transition happen so state machine dot change state to player dot dash state just like that now we might as well just go ahead and copy this because we're going to set up the same transition from our player grounded state so let's head there add this at the end in our logic update function and now we just need to remember to set up our dash input boolean so private bool dash input and then after we read in our grab input we'll read in our dash input so dash input equals player dot input handler dot dash input perfect so now we have the ability to transition to our dash state from in air and our grounded state and we already have the checking built in to make sure that we actually can transition to that state awesome one last thing we need to do is come back to our player dash date and create a can dash reset function that gets called from our player grounded state so public void reset can dash and all this function does is set can dash equal to true so if we then come to our player grounded state and in the enter function we say player dot dash state dot reset can dash we should now once again gain the ability to dash whenever we touch the ground let's go ahead and make sure all this is working so far so if we come to unity and then hit run if we now hold shift or x as you can see we are stuck in the dash state and of course it's playing our in-air animation currently but perfect everything is working so far so now we're free to start working on the actual dash logic and so let's talk about that quickly there's two parts to our dash we have the part where once we start the dash time goes into slow motion and we have the ability to select the direction that we want to dash in and then the second part is the actual dash itself now this could technically be broken down into two sub states a whole substate and a dash move substate but they'll both be really simple states so there's really no need we can just do it of a simple if else statement but if these two parts were to get more complicated we could easily break this up into two substates so before we start working on that logic let's go ahead and come back to our player data script and let's just declare the rest of the variables that we're going to need so after our dash cooldown let's go ahead and declare a public float that we'll call max hold time and by default i'm just going to set this equal to 1. so once we start the dash in that slow motion state how long can we stay in that state before we're forced to dash that's what this variable is going to be after that let's go ahead and declare another public float this one is going to be our hold time scale and by default let's set that equal to 0.25 and this variable is going to hold what we want to set our time scale to once we're in that hold state so if this is 0.25 that means our time is going to progress a quarter of the speed it usually would next we have another public float this time it's going to be our dash time itself so this is once we start dashing how long do we dash before we leave our dash state by default 0.2 works for me once we have our time we also need our dash velocity so public float dash velocity and by default i'm going to set this equal to 30. now another variable we need is again another public float and this one is just going to be called drag by default i'm going to set this equal to 10 and so what the drag variable is if we take a look at our player real quick on our rigidbody2d component you can see there's this drag property linear drag so drag is basically how the air density affects your character's velocity and so setting the drag to 10 while we're dashing makes the dash feel a little bit better it's a very subtle difference but i've had lots of different people test this this was actually suggested to me by somebody on the discord server and i've had some people blind tested for me and everybody chose the one that had the drag applied so we'll just go ahead and go with it so yeah after this we need another public float and this one we're going to call dash end y multiplier and by default i'm going to set this equal to 0.2 as well and so what this is is basically the same thing as our variable jump height multiplier and so when we leave our dash state we're going to multiply our y velocity with this just to make it so that we can't fly off really high it just makes it feel a lot better now our final variable that we need is yet another float and this time it's going to be the distance between our after images so distance between after images and by default we'll set that equal to 0.5 and that's it for our player data might as well go ahead and close out of this now now we can finally start working on our player dash date again now to start off let's go ahead and generate all the functions we need so just click on the class name control full stop generate overrides go ahead and deselect all and all we need is enter exit and our logic update function if we then go ahead and click ok here we have all the functions we need i'm just going to go ahead and move them above these other functions that we have just like that perfect so now once we enter the dash date we can go ahead and set can dash equal to false as we can no longer dash until we touch ground again and we also need to let our input handler know that we've used the dash input so we'll say player dot input handler dot use dash input just like that so now seeing as soon as we enter the dash date we have that little slow motion holding time we might as well create a bool to keep track of whether we're still in that part of the code or not so let's create a private boolean and let's just call it is holding and then we also need to have our dash direction so let's go ahead and create a private vector2 and we'll just call it dash direction and then back in our enter function let's just go ahead and set is holding equal to true as that's always going to be our default case where we enter the dash state and then let's give our dash direction a default value as well we can just say dash direction equals vector 2 dot right multiplied with our player dot spacing direction so now our dash direction by default faces the direction that the player is facing now next we also want to change our time scale and go into that slow motion mode and the way we do this is by just setting the time scale so we just say time dot time scale and you can see if we click on this and just read the description it says the scale at which time passes this can be used for slow motion effects cool that's exactly what we want so let's just set this equal to player data dot hold time scale but now seeing as we're doing it this way there's some things that we need to note and that is code like this where we say time dot time is greater than or equal to last dash time plus our cooldown no longer works properly while our time scale is something else if our cool down time is one second but our time scale is set to 0.5 this will only become true after two real time seconds because of the fact that our time scale is 0.5 so if there's things that we still want to happen at a constant rate of time we can use a handy little parameter of time called unscaled time but in this case this condition down here is fine because this never happens when our time scale is not equal to one but seeing as we want to keep track of how long we've been in the hold state to make sure we dash when that time runs out we need to save our unscaled time so let's just go ahead and set our start time equal to time dot unscaled time like that and that should take care of all of our issues and that's it for the enter function for now we can now head on to our logic update function in here let's immediately just go ahead and say if not is exiting state as all of our transitions are going to happen in our player ability state class we don't want any of the code where we set the velocity to interrupt that now in here we're basically just going to have two different sections one section is going to be for when we're holding and determining our direction and the second section will be for when we're actually performing the dash and setting the velocity so let's go ahead and just say if is holding and that is our first section now in here what we need to do is determine the direction that we want to dash in and set that little graphic so now before we can continue here we have some more input to set up so let's go ahead and save this and head back to unity and let's come back to our player input actions let's go ahead and create a new action and we'll just call this dash direction now in this case dash direction is not going to be a button action type it's going to be a value action type as we're going to be sending a vector 2 through to our input handler so control type is set to vector 2. now our first binding let's go ahead and add the one for controller so click on path listen and then just move the left stick we can go ahead and add left stick and set that up as our gamepad control scheme now for our keyboard and mouse input it gets a little bit interesting we need to think about how we want to determine our dash direction when playing of keyboard and mouse originally i just had it so wasd would determine the dash direction same as you know for normal movement and if that's what you want to do you can go ahead and just click add binding add a 2d vector composite and then just set up your was and d key but a great user by the name of zio suggested that we use the mouse to determine our dash direction and i tried this out and i actually like the feeling of it a lot better it's a lot easier to control so we're gonna go ahead and use that approach keep in mind this does complicate things a little bit but it's a great opportunity to learn some new things so let's just go ahead and add another binding and for our mouse binding we don't need to add a 2d vector composite we can just add a normal binding go ahead and click on that and for our path it should already be by the mouse settings if it's not you can just come click on mouse and then select position now what the position property is is the mouse's screen coordinates so if it's down here it's zero zero if it's up here i think it's a screen resolution i'm not 100 sure about that you can go ahead and just debug it and see what it gives you that's all the setup we need to do here just remember to set the control scheme to keyboard and so that's it for our input actions let's go ahead and come back to our player input handler and let's go ahead and create a on dash direction input function so we have a public void on dash direction input and of course we have input action dot callback context call it context and now in this function we need to read in our value but now it's important to remember that we have two different bindings that are going to give us two completely different types of input both will be in the form of vector 2 but if we're using a controller that vector 2 is going to give us the direction that the joystick is being pushed in but if we're using keyboard and mouse that vector 2 is just going to give us a vector from the bottom of the screen to the cursor so now we're going to learn how to adapt our logic in these functions based on our control scheme and this is actually really cool so let's go ahead and come back up to our variables and let's declare another public vector too and we'll just call this raw dash direction input give it a public getter and a private setter and now before we go back to our on direction input function we need two more variables the first variable we need is going to be a private player input variable and we'll just call it player input and what this refers to is the player input component on our player so over here we're going to use this to get information about what our current control scheme is next we need to have a private camera and we'll just call it cam and so what this is going to be is a reference to our main camera over here and this is going to give us access to a function that is going to allow us to translate our screen coordinates from our mouse into physical world coordinates and that's quite handy so let's go ahead and set up the references for these two variables we can go ahead and create our start function and in here we'll just say player input equals get component off type player input and then next we can go ahead and say cam equals camera dot main and that's going to set up the reference to our main camera perfect now let's go ahead and come back down to our on dash direction input function and let's start off by reading in our value so we'll say raw dash direction input equals context dot read value and the value type is going to be a vector 2. now again if we're using wasd to determine the direction you don't have to do this next part but if you're interested next we're going to adapt our logic based on if we're using a keyboard and mouse or controller so all we need to do is say if player input dot current control scheme is equal to keyboard now this keyboard string that we have over here needs to be exactly what you called it so if we take a look at our input actions over here we have keyboard with a capital k so it needs to be exactly the same way as you wrote it here so now this is going to go to our player input component and check what our current control scheme is and if it is keyboard we're going to do something special with our raw dash direction input so what we want to do now is convert this from screen coordinates to a direction vector that points from our player to our mouse and to do this is really simple all we need to say is raw dash direction input equals cam dot screen to world point and this is a function provided by our main camera that allows us to give it a screen position and it'll give us back a world position now this function takes in a vector 3 position but because the value that we're reading in is a vector 2 we just need to make sure that we cast it as a vector 3 first so we just say bracket vector 3 raw dash direction input so now we have some world points now to turn it into a vector pointing from our player to our mouse in essence pointing from our player to this world point we just need to subtract our player's position from this so we just say minus transform dot position so that's basically it for our on direction input function there's one more thing we need to add here in just a little bit but we'll get to that in just a second so before we head back to our player dash date we just have to remember to add this to our player input component so come back to unity click on our player come to the player input component click the events drop down gameplay drop down and then over here we have our dash direction event add to the list and just drag in our player input handler and then from the function drop down go ahead and select player input handler and then select on dash direction input so the input is set up let's go back to our player dash state and let's start off by reading in our dash direction input so let's come to the top create another private vector2 let's call it dash direction input and then in our logic update function inside of our if is holding we'll start off with dash direction input equals player dot input handler dot dash direction input or raw dash direction input in this case so now what we want to do is update our dash direction based on our dash direction input but only if our dash direction input is not zero so if we're not giving any input the dash direction should just stay the last direction that it was so to do this we just say if dash direction input does not equal vector 2 dot 0. if this is the case let's just say dash direction equals dash direction input and then let's just go ahead and normalize our dash direction so dash direction dot normalize now let's go ahead and add in that direction indicator just so that we can test this out and visualize it so let's go ahead and navigate to our sprites folder player and let's just import our indicators right we can then just go ahead and click on it and set our pixels per unit to 16 and set our filter mode to point no filter and the compression to none we can then go ahead and hit apply now to get the indicator on the player what we're going to do is create another child game object on the player so create empty call it dash direction indicator and then let's go ahead and add a sprite render component to this game object we can go ahead and drag in our dash direction indicator into the sprite slot that's not a sprite render what am i doing so a sprite renderer there we go so go ahead and drag in the dash direction indicator and then we just need to set our sorting layer equal to player and then we can just change the ordering layer to negative one so that it appears behind the player so now we have this game object that is a child of our player that we can rotate to face the direction that we're going to be dashing so let's go ahead and figure out how we're going to do that first of all we need to get a reference to this game object so let's head to our player script and in our components region let's just go ahead and declare a public transform and we'll just call it dash direction indicator and give it a public getter and a private setter and so we'll use this to interact with our indicator from our dash state we can go ahead and close down our components region and come to our unity callback functions in our start function let's just go ahead and say dash direction indicator equals transform so this is referring to the game object's transform that this script is attached to so our player then we say dot find with a capital f so this is going to find a child with the same name that we give it and so the name that we give it is dash direction indicator and of course this has to be exactly the same as it is in unity so don't misspell anything so now we have a reference to our dash direction indicator perfect let's head back to our player dash state so now after we've determined what our dash direction is based on our input let's go ahead and work out what that actual angle is to do this all we say is float angle equals vector 2 dot signed angle and so what side angle does is it returns the angle in degrees between the two vectors and so to this function we're going to pass vector 2 dot right as our from so this is basically our reference line and then we're going to pass our dash direction as r2 so this will return the angle between vector2.right and our dash direction we can then give this angle to our dash direction indicator so that it points in the right direction and we do this by saying player dot dash direction indicator dot rotation equals quaternion dot euler and this is going to allow us to define a x y and z rotation our rotations are going to be 0 for the x 0 for the y and then we're going to use angle as z but seeing as our sprite is initially at a 45 degree angle we can just go ahead and subtract 45 f from this and there we go this arrow should now be pointing in our dash direction let's go ahead and try it out again so if we go ahead and run the game and then jump and dash you can see the arrow points perfectly towards my mouse now if i just change over to controller and start using the left joystick you can see it does the same thing very cool and what's even cooler is the fact that from our input itself we're getting two completely different vector2 values so yeah i think that is very cool okay so if this is the kind of control that you want over your dash direction awesome but i want to lock my dash directions to either be 0 degrees 45 degrees 90 etc etc like you saw in the demonstration at the start of the video so let me show you how to do that let's go ahead and come back to our player input handler function so now basically what we're going to do is we're going to take our raw dash direction input and just convert it to either be pointing straight up 45 degrees or horizontally so to do this let's start off by declaring a new vector 2 underneath our dash direction so we have public vector2 and this one we'll just call dash direction input as this is the one that i'm actually going to be using give it a public getter and a private setter just like that and then come back to our on dash direction input function actually i made a mistake this should not be a vector 2 it instead should be a vector 2 int and what that is is basically exactly the same as a vector 2 except instead of having float x volt y it is int x int y and you'll see why we use this in just a second so let's go back down to our function and after this if statement what we'll say is dash direction input equals vector 2 int dot round to int and so what this does is it converts a normal vector 2 to a vector 2 int by just performing a round to end on each component of the vector 2. so basically if our x component is 0.5 we'll go to 1 if it's 0.4 we'll go to 0. and so this will give us exactly what we're looking for so now to this all we need to do is pass our raw dash direction input dot normalize so that way our dash direction input only has a magnitude of one and sorry this should be dot normalized so now basically this dash direction input can only have values 0 1 1 1 1 0 etc etc so it's going to give us all the directions we want left right up down and at the 45 degree angles so now if we go back to our player dash state instead of reading in the raw dash input let's just read in dash direction input let's go ahead and try this out so if we run the game perfect as you see it locks 45 0 90 180 perfect and so yeah that's a neat little trick that i learned recently we can probably do the exact same thing with our normalized x input but we can just leave that as is for now anyway back to our player dash state so after we've set the angle or the rotation of our dash direction indicator we're basically done with this holding part of the code the next thing we need to do is check whether we are done holding or not so this is going to depend on two things did the player let go of the dash button or has the holding time run out so the first thing we need to do is read in our dash input stop boolean so let's come back up to our variables and let's declare another boolean so private we'll call it dash input stop and then let's go back down to our logic update and let's just read that in so dash input stop equals player dot input handler dot dash input stop then after we've set this dash direction indicator dot rotation we can say if dash input stop or our time dot unscaled time and remember we're using unscaled time now because our time is currently slowed down so if this is greater than or equal to our start time plus our playerdata dot maximum hold time so if this is the case we now know okay move on to the actual dash part of things so now inside this if statement we can start off by setting is holding equal to false so this is how in this state we now know we're no longer holding move on to the else part of this if else which is the dash part of things after this we can go ahead and reset our time scale with time dot times scale by setting it equal to one then let's go ahead and set our start time equal to time dot time and the reason we do this is so that we can track how long we've dashed for again like i said if if the start time so the time that we actually entered the state is important within your state you can go ahead and just create another variable for this but in our case this is fine next we can go ahead and check if the character should flip so if we're busy dashing in the direction we're not facing by saying player dot check if should flip and to this we need to pass math f dot round to int we'll just pass in our direction.x next we can go ahead and set that drag that i talked about before by saying player.rigidbody.drag equals playerdata.drag now finally the last thing we need to do is set our velocity now the thing is we're setting our velocity based on a velocity speed that we defined in player data and our dash direction we don't currently have a function that can handle that so let's head back to our player script open up our set functions region and over here we have a set velocity function that takes in a velocity an angle and a direction so this actually does not allow us to dash downwards so let's just go ahead and create another public void set velocity function and this function is just going to take in a float velocity and instead of angle and direction we just have a vector 2 direction now inside this function we just say workspace equals direction multiplied with our velocity so that's basically going to scale our direction which has a magnitude of 1 to match our dash velocity then we can say rb dot velocity equals workspace and we can update our current velocity with current velocity equals workspace back in our player dash state we can just go ahead and say player dot set velocity and to this we just pass our player data dot dash velocity and our dash direction and that's basically it we can go ahead and test it but not much will happen we'll just see the character kind of hop but it won't look like a real dash yet because we're not sustaining that dash velocity but at least we can see that it works okay let's go ahead and move on with the actual dash part so now we can go ahead and add an else to this if is holding so else this means that we're actually performing the dash and then all we need to do in here is say player dot set velocity player data dot dash velocity and we pass in our dash direction then all we need to do is check our time to see if we should leave the dash date or not and all we say is if time dot time is greater than or equal to start time plus our player data dot dash time so if this is the case we'll say player dot rigidbody dot drag and reset that back to zero of course you should probably reset this back to whatever you have if you don't have it set to zero it might be good to also declare this as a variable instead after this we just say isability done equals true this lets our super state know that we're done performing the ability and it is free to transition us to a different state last thing we need to do is set our last dash time equal to time dot time this is just to make the cooldown work let's give it a try so if we run the game and then dash boom almost there we're almost done we just have two things left to worry about well three things actually one is the fact that our dash direction indicator just is always visible second that we dash so high and then thirdly we don't have an after image so first let's focus on our direction indicator all we need to do is come back to our player dash state and then in our enter function we can go ahead and say player dot dash direction indicator dot game object dot set active and to this function we just pass true so whenever we enter the dash state we turn on the dash direction indicator and then what we want to do is once we actually perform the dash so in this if statement over here we just turn it back off so we say player dot dash direction indicator dot game object dot set active and we set it to false and then if we come back to unity and just turn off the dash direction indicator by default run the game as you can see while we're holding we can see the arrow and then it just disappears perfect okay next let's take a look at this y velocity problem and that is why we have that multiplier so all we need to do is come to our exit function and in here we'll say if player dot current velocity dot y is greater than zero then player dot set velocity y to player dot current velocity dot y multiplied with our player data dot dash and y multiplier and the reason we put this in an if statement like this is we don't want to decrease our y velocity if we're dashing down it looks weird because we're moving fast then we slow down and then we start speeding up again so we only do this if our y velocity is positive let's go ahead and try that out that's a lot better now the last thing we need to worry about is our after image now i'm not going to go into a lot of detail on how to create the actual after image itself you can go ahead and check that out in the previous dash episode i'll link it in the description but basically we have our after image pool that we can just turn back on let's come back to our dash state and then let's go ahead and create two new functions they're both going to be private and the first one is going to be private void place after image like that and all this function is going to do is tell our pool to place an after image and then save the position where we place that after image so let's quickly go back up to our variables and declare another private vector2 we'll just call it last after image position come back down to the function and in here all we say is player after image pool dot instance dot get from pool like that and that's basically just going to tell the pool to place an after image the pool will turn on and after image component which will take care of placing it in the right position by itself let's just save this position by saying last after image position equals player dot transform dot position now the next function we're going to create is just going to check if we should place an after image or not so we'll say private void check if should place after image and all this function is going to do is compare our current position with our last after image position see if the distance between those two points is greater than what we set up in player data and if it is we'll just call the player after image function so we just say if vector2 dot distance this function takes in two vector twos and returns the distance so we'll first pass in our player dot transform dot physician and then we'll pass in our last after image position so if this is greater than or equal to our player data dot distance between after images then we just call our place after image function again now the last thing we need to do before we finish is just call this function while we're dashing so after we set the velocity let's check if we should place an after image and we can also call place after image right before we start dashing so in this if statement over here at the end we'll just say place after image so if we save this and test it out everything should be working fine except we have an error and i always do this i forget my semicolons how did that happen okay let's try this out so now if we dash you can see we have that after image effect following us perfect and that does it for the after image video and before i go i would just like to give a huge thank you to all of my supporters and wonderful people over on patreon and a huge special thank you to binary chef sa cody lee pyro says and miguel gray for your support on patreon you guys are absolute mad lads and i hope you guys all have a wonderful day
Info
Channel: Bardent
Views: 7,303
Rating: undefined out of 5
Keywords: Unity., tutorial, player, 2D, platformer, walljumping, wall sliding, jumping, Unity, Animation, ground, check, physics2d, 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, Unity combat, Comabt, Melee, Melee Combat, Basic, Enemy, Patrol, State, Machine, respawn, hit, finite state maching, state machine, enemy, behavior, Archer, FSM, New, Input, drop, dash
Id: 7YnmIE6R7Y4
Channel Id: undefined
Length: 52min 53sec (3173 seconds)
Published: Fri Sep 18 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.