Roblox Dash/Roll System MEGA Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone happy Holidays wherever you are in the world it's been a while but I am back with another scripting tutorial for you guys this time talking about how to create a dash system in the previous tutorials we went over how to create a sliding system so if you haven't checked that out I definitely would but yeah let's get right into it first I will show you a quick demo of what the final result would look like once everything is said and done as you can see we are our camera is in regular mode and in regular mode you can see we have a forward rolling animation that plays in the direction the character is moving and when I'm not pressing any keys and I press Q to Dash um he will by default just roll backwards and then in shift lock modee there is the introduction of two additional animations one animation specifically for dashing to the left another animation for dashing to the right and that's it let's get right into the coding portion before we get to coding I should show you what all the assets are we are using so for one we have our animations our Dash animations and as I said before we have a backward forward left and right and we will be using one module um called The Promise module and we're going to be invoking on that quite a bit in this tutorial so um I will link the promise module down in in the um description box below but otherwise I think we are good to start for starters I've went ahead and inserted some boilerplate code for initializing our services that we'll be using and here I've referenced the promise module and here are just some commonly used variables we'll need so one thing we will need to do is to correspond the directions our character moves in to the animations for shift lock CU if you if you recall I mentioned that for shift lock uh we play different animations based on Direction whereas in um regular camera mode we just play the forward animation so I'm going to create a table called directions which will be a mapping between these um animation name prefixes and directions and then now we're going to go ahead and load our animations so that they are available when we want to play them so I'm going to Loop through the dash an folder and if the in if a particular instance or descendant instance in the folder is an animation then we want to load that animation there we have it so the way the movement is going to work is we want to quickly accelerate the character in a in a direction and then quickly decelerate his speed to zero so to do this we're going to use some basic projectile motion um equations and Concepts to help us and we will need a basic function to help us with this uh we'll call this function accelerate to Velocity and this will take a in your velocity instance it's going to take a Target velocity um this will be the velocity that we're trying to reach Target velocity that we're trying to to reach and then aile will be the how fast you know we want to reach that Target velocity so the acceleration to that Target velocity we're going to create a velocity variable this will be our current plane velocity and if you're not familiar with linear velocity plane velocity is basically a property um that allows us to apply velocity only within a certain plane and in our case our plane will be you know um parameterized by the x and z axis because we don't want um for example if there's some some sort of gravity also affecting the character which would happen in the vertical direction we don't want to you know affect any movement or touch any movement along the vertical direction we only want to um move the character along the um lateral plane and so this should actually be a vector 2 because like I said it's the x and z AES and therefore this should also be Vector 2 as well and we're going to create a variable Delta [Music] V this will be the difference between our Target velocity and that plane are current uh plane velocity I'm going to type this line out first and then explain after if you're not familiar with Delta time pretty much it's the amount of time that has elapsed between the previous frame and the current frame so essentially it's a factor of our of FPS so if if you're having 60 FPS then on average each frame is taking one over 60 seconds and you'll understand why we need this with this next line so Max acceleration here is the maximum acceleration that can be possibly done within a frame right so when we're dealing with game development um frame the the time it takes to clean one frame is our like most basic unit of time so if you're expecting something to accelerate within uh a span of time that is shorter than a single frame then we cannot reasonably do that so this is the best we can do so this is a cap essentially then let's create variable called final acceleration so we're going to see if our if the you know acceleration that we're trying to accelerate with is um if it's if if if our maximum acceleration is less than um what our our input acceleration then we're going to you know um we're going to just set the final acceleration to the maximum acceleration because essentially we're trying to accelerate faster than what is uh possible otherwise if we're not then we're just going to take Mac XL we're going to just take unit vector and then multiply by acceleration so we're saying that we want within the same direction as the Delta V but you know we make a unit Vector so that the magnitude will be the magnitude of our acceleration vector will be equal to our input acceleration and you know just remind you this is just a regular number then finally we're going to write return a promise more on this later and going to create this frames Within Reach variable which I'll explain in a bit going to say game calling gu service run service that's St so that stepped because we want to change our velocity um prior to the physics step so before any sort of physics uh simulation is done within the frame so uh if you're familiar with heartbeat heartbeat happens after physic simulation but since we're trying to modify the velocity we want to modify the velocity prior to the physics simulation and so we're going to modify our current velocity and remember we're trying to modify our current velocity such that it is um we want to accelerate it so we're going to take our current velocity and we're going to add it to find acceleration times DT so so let's say that our final acceleration was like you know 3 m/ second squared um so you know when we multiply by DT we're multiplying it by you know the time it takes for a frame to elapse so over the course o of 1 second we would have added 3 m per second to our velocity and now we have to test if we have reached our Target velocity so I'm going to take Target velocity and then subract plane velocity and I'm going to see if that magnitude is less than acceleration. magnitude times um DT times frames Within Reach so to explain basically we're trying to see if our Target velocity uh if our current velocity is close to our Target velocity and um to do this is not so trivial some of you might think why we just you know choose some random arbitrarily small number like 0.003 and if it's less than that then we have reached um but we want to be more exact than that basically I'm here I'm saying that if um if we're close to our Target velocity by one frame meaning within another frame we'd have reached our you know Target velocity then you know um then I'll consider that as reached because we don't want to compare directly against zero because you know we're dealing with you know floating point you know decimal values and such so it um it's you know it's um when you're deal with floating points you have to always work within certain range you can't be exact about it and also this is helpful because we don't want to overshoot right because maybe our Target velocity maybe our acceleration is really big and if our acceleration really big then perhaps whatever range that we chose is very possible to to um to overshoot that range and then this actually becomes like you know this goes like this becomes like negative sort of thing and then um the magnitude overshot the range where it was close to zero so this is a very precise way of making sure that we don't overshoot our ranges all right and so if we are within reach then might as well play same velocity to Target velocity disconnect this step defent we're going to resolve our promise thread so that any threads are waiting for this promise to complete will be notified and carry on so there's two things we will want in our Dash system one of those being as I've mentioned already we want the dash to be we want the initial burst of speed to be quick so quick acceleration and you know obviously we want you know a a good deceleration as well but we also want [Music] to to take as input the distance that we want the character to travel over you know the period of him dashing we want this to be predictable so if I input that o I want the character to dash 16 studs the character should be able to dash 16 studs and this predictability is very nice um well for one you know like just in terms of like you know game play and playing the game um maybe you have like a certain ability and you know it causes the character to dash in this manner and maybe you have a tool tip and you want to show that hey you know dis ability will have the character move 16 studs so that predictability is important in that context and also in the context of exploit prevention because here we're doing everything on the client and removing the character on the client and you know when we have a very predictable way of moving the character then it's very easy on the server to then verify that the character um should be where we expect them to be otherwise then it's possible that the client is trying to exploit and so with all that in mind let's go ahead and create a new function accelerate to distance this will take us input Target velocity um acceleration time that's you know put in how much time we want to reach Target velocity and the total distance we want our Dash to cover and so here is where we will create our um linear velocity instance we will serve last the constraint mode to be plain we only want to apply velocity within the lateral PL lateral plane we'll just set the velocity to Vector 2.0 for starters we want our velocity to be specified in terms of world coordinates and so to and also our axes as well are going to be set in World coordinates these are the axes for the plane so we have the X AIS and we have the z-axis all right now we will accelerate the character towards the target velocity column RoR to V function passing our linear velocity Target velocity and for acceleration we will have um Target velocity magnitude over acceleration time so the time we want to take to accelerate from uh zero to Target velocities essentially Target velocity D mag would be our speed and then remember our Accel to returns a a promise that resolves and once that resolves we want to call back to this function right here which and it resolves when we reach the target velocity so we will enter this once we've reached the target velocity and this is where we will decelerate the character to zero so from Target velocity back down to zero but recall I mentioned that we want to travel a distance of total distance over the course of our Dash so essentially we want to we first need to figure out how much distance is left right cuz you know we covered some ground going from zero to Target velocity and so we want to figure out how much distance is left for us to decelerate from Target velocity to zero and whatever that distance is we have to figure out what um what amount of deceleration will cause us to decelerate from our Target velocity to zero within a and cover a a distance a specific distance while doing that so on my screen screen here I have a couple of you know kinematic equations mapped out so recall first I mentioned we need to figure out the distance that we covered um from going to you know um no speed or whatever to reaching our Target velocity and so that distance here is um you know this kinematic equation so we have 12 a^2 and um r acceleration is uh this time here is the the time um we took to go from you know current velocity to Target velocity and then this acceleration is nothing but our Target velocity over you know the time it took us to accelerate um today so which is T1 and so once we eliminate uh T1 here T1 here we're left with this simple um formula and so the remaining distance is simply our total distance minus the distance covered on the uh on the first half I guess you could say and then this equation here is how we can figure out the the amount we our deceleration value from knowing our initial velocity in this specific case our initial velocity is our Target velocity and our found velocity would be zero okay we're going from Target velocity to zero and um this is over to D2 um D sub2 here is our um remaining distance so this should give us acceleration needed and you know final velocity here is zero so we're just left with this so let's go ahead and then plug these formulas into our code so I've gone ahead and put these formulas here and now it is time to decelerate so decelerate you use the same function and this time we are our Target V be zero and we want to take um A2 amount of acceleration or I guess deceleration to do that and once we we deated to zero then we're just going to destroy the attachment which will also destroy our linear velocity all right so now let's put it all together and complete our [Music] Dash so I'm going to get the forward animation for the forward Dash I'm going to set the moving direction to be by default the assembly linear veloc velocity direction but we only want the lateral direction of course so it's going to be by default the direction we'd want to move in um now there is some exceptions and that is when we are in mous lock mode then we want to do things a little differently so um recall we have this directions table does a mapping between the animations and the direction we're moving in and so what I want to do is I want to figure out um which of these directions is the closest to my um as as to my um assembly linear velocity direction here and based on that I will select the um animation so to do that I'm going to Loop through that directions mapping just call this direction name I guess let call this gu closeness essentially I'm multiplying this factor to hrb H you know part C frame to get the to get a relative um to um to get in World coordinates essentially so this is pretty much the human root Parts you know right vectors left vectors up vectors and you know basically I'm seeing what the angle is between the you know all the directions of the of the character and the direction he is moving in right so if he's moving in um a direction that is very you know like left then I imagine the um this left Vector would be you know would have would be close to our our movement vector and so the angle between them would be small essentially I'm doing 1 minus that because um we want this to be um like if the the small the closer they are together um because this will essentially what the do product does is if this is the exact same Vector as our um move movement Vector then this will be one if it's perpendicular then it's zero um so if it's the exact same that means like that's the maximum closeness so that'll be one and so one minus one would be zero zero to indicate that they're like you know super close to each other like that's as close as you can get essentially um I might just okay I'm just going to give it this name I don't like the name but you guys know what I mean okay so pretty much this whole thing is just to figure out what is the closest direction to our movement vector and once you figure that out then we can go ahead and say our animation will be equal to our anims our anom Direction and I'm just going to add Dash to the end right because recall that's how our animations are named so one of these directions and you just add Dash to the end okay otherwise if I were for not moving and we want to set or move there to be because we want to roll backwards if you recall when set an to be backwards backward Dash here we're just going to play the animation then we going to set Target velocity to be 60 and going sorry to distance want Target velocity times called this it has to be a vector to so I'm going to say. x c unit and then we want to take you know 04 seconds so really fast acceleration to the Target velocity and then we want to travel a total of 18 studs once you've done that um so yeah so this returns a promise so if you want to you know if you want to do something after the entire Dash sequence then you you would do it within here I don't have anything special per se so I'll just leave that alone and yep so yeah that's that so far and one thing I want to do is I want to actually return the promise that will and indicate that this has um this has finished so we will wait until the animation finishes playing and that's when this this Dash will be considered um completed I guess might be better to just do this you can change these promises and then I'm going to add so we need to add um hook the q key to this do Dash so that whenever we press Q it will do our Dash Okay so I'm going to create um I'm going to bind so when we press the q key we're going to create a is Dashing variable for debouncing and we are not dashing then we're going to say we are dashing we're going to do Dash and then once we're done dashing I'm going to set as dashing to false and that should do it so let's go ahead and test this okay we got an error attempt to index nil with play cont action Serv so our animation might be n oh who you put forward there spelled it wrong that should be correct go ahead and play only worked once I think this has to do with our promise okay yeah I don't think this right here is proper I'm going to just put this into a separate promise because I think I do need that resolve or if I want to chain I have to do this a bit um I'd have to I'd have to have i' have to do it a bit differently than what I did just there but let's just keep things simple once the animation is finished then we resolve not quite traveling that 16 studs or 18 studs that we set it to so let's see what's going on all right so I was able to figure out what was going on essentially we forgot to parent the live linear velocity instance to our attachment M and also I've went ahead and replace um the sem linear velocity here with humanoid that move Direction instead because it's more accurate for this use case and also uh I replaced some linear veloc with human move Direction here instead and that was all oh yeah and then one more thing is prior I had add this I change because you know we we always want to update a current uh velocity we want to increment we want to add acceleration to the current velocity well you know we're doing this is happening every frame and then this only happens at the beginning so we're using a stale velocity value essentially so that should instead be lv. plane velocity when we play this you see that it looks good I go to shift loock mode left right left forward there's a error see what that's about all right so apparently the eror disappeared when I remove the dot unit from move Direction uh move Direction I believe is already a unit Vector so uh it's unnecessary anyways um think I'm going to move things around slightly because only one of these happens and this way we check Mo Direction first if zero then you do this otherwise we can go ahead and do the shift lock logic you can see pretty smooth I also created a part here that's exactly 18 units long you can see here to see if we actually travel that distance oops should really anchor that but anyways let's see you can see travel exactly 18 units let's say um that you know you want double the um the acceleration to Target velocity perhaps you might want to then um double the distance that it travels so you can double both at once and boom double the impact double the distance it's going 3 36 units this is also useful in the context of knockback because when you're doing knockback on a another character quick acceleration deceleration is very important in that context um so this is not just useful for dashing but in other contexts where you need quick acceleration and deceleration and um I think this is this doubling effect is also really cool because cuz let's say that you have a character here and he shoots an arrow and hits another character and then somehow he gets this Boon that doubles his um that doubles his shot power and so when you double your shot power you would expect the other character to you know fly you know double the distance and you know it's kind of cool because you can vary the you know um the target um velocity to be double while maintaining the same amount of time to accelerate to that which will give the effect of you know more power because more power means quicker acceleration while traveling a longer distance because obviously if he has more power then you know the character will get knock back you know further so yeah I think that is all for this tutorial um perhaps in the future we might talk about you know how to do that exploit prevention aspect of it so that you can verify on the server to make sure he's traveling the right number of studs um he's ending up and and that he's ending up in the correct location um but otherwise yeah that is all for now I will see you all in the next tutorial don't forget to like subscribe because this gives me motivation and if you have um any questions any comments feel free to drop it in the comment box and if you I should mention that this you know slide this uh the rolling the sliding all this movement comes from a game that I'm currently working on as part of a team uh called Atlas so if you're interested in supporting that game I'll also leave that Discord invite in the description box thank you and happy holidays
Info
Channel: Wasiim
Views: 1,027
Rating: undefined out of 5
Keywords: Roblox, Lua scripting, Game dev
Id: 1AsgaiBfJOM
Channel Id: undefined
Length: 44min 34sec (2674 seconds)
Published: Thu Dec 28 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.