hi guys mike here from com3 interactive welcome back to the channel today we're going to be taking a trip down memory lane i'm going to go back to basics back to the game boy style movements which are grid movements now currently i've got two scripts set up and i'll just give you a quick example of what we'll be dealing with here so the first one is something i'm going to call free movement and that is the ability to move in all directions freely so if i go ahead and remove that and put in my grid
movement script we'll see what our target movement style is actually going to be so now if i just press one of the arrow keys once we see that we snap within a grid and i can hold it we'll continuously move just like before but when we let go we'll always be confined to this grid now this is an extremely easy thing to do but it's a thing that i don't often see in modern games now don't get me wrong the free movement is perfectly fine perfectly valid but to me if you're making a pixel art
game more than anything i like to see this confined to a grid movement style my personal preference don't know about you so you can drop a comment below and let me know which kind of movement you prefer free movement or grid movement but this video we're actually going to be focusing on the grid movement as you may have guessed by the video title and if you've clicked on it i assume you're interested in grid movement as well so what i'm going to do i'm going to remove that grid and delete it from the project we're
going to create that from scratch so the basic movement script is something that you may already have in your game as you can see really basically using the old school input methods we've got wasd and all that's going to do is move our player up down left and right using the move player method and that's how we get our free movement so we're going to create a new script we're going to copy the majority of this but then we're going to tailor it to limit our player movement to that grid but before we do that
i just want to thank gigatank 3000 for sponsoring this video i've got links down in the description go check him out on twitter and on his website and keep up to date with the latest game that he's got in development and i'd just like to thank all my patrons you guys are fantastic okay so let's get into it then we'll go ahead and create a new c-sharp script and we'll call that grid movement open it up in visual studio and like i said before we'll just take this input and drop it in here for now
and we'll remove that start method so the first thing that we're going to want to do we're going to set up a few variables the first of which is going to be a boolean so we can keep track of whether or not a player is already moving so we'll call that is moving we also need to keep track of our original position and our target position though we'll use these in just a minute but we just need two vector threes to keep track of these two positions that's private vector three orig pulse original position and
target pause um one final feel that we need instead of a movement speed for this what we're going to need to do is we're going to need to keep track of the time we want it to take for the player to move from one grid square to the next private float we'll call that time to move and i'm going to set this to 0.2 so this is in seconds so that's going to take one-fifth of the second to move our player from point a to point b and the first thing we're going to want to
do is we're going to want to make sure that if we're already moving we don't actually start another movement because that'll mess up our grid so inside of each of these if statements i'm going to check and make sure that the player isn't already moving if the player is moving we're not going to start another movement next we'll create a core routing to actually take charge of our movement so that'll be a private ie numerator and we'll call that move player and this is going to take in vector3 just like before and that is going
to be our direction so inside here we want to make sure that we set our is moving boolean to true and false when it's relevant so right at the start of the core routine we're going to set is moving equal to true and then right at the end we're gonna set it back to false so that way if this call routine's already running and we press another key it won't start moving in another direction it'll wait for this to finish and then if your key is still pressed it'll move in that next direction next we
want to make sure that we keep track of the elapsed time how much time has actually elapsed since we started this core routine so we'll create a float inside here and we'll call that elapsed time and we're gonna set it to zero every time we come in here so we'll just default that to zero right there next we need to set our positions so we'll set our original position equal to whatever our transform dot position currently is and then our target position is going to be uh original position plus the direction that we pass in
now we can do a plus on this because we're doing a mathematical formula or mathematical equation whatever you want to call it we're doing addition on two vector threes which is perfectly valid so now that we have our positions we can actually start our movement and the way we're going to do this we're going to use something called lerp now what lerp is it takes in two values in our case it's going to take in two vector threes and then it takes in a third parameter which is known as the step and the step is
going to be the elapsed time divided by the amount of time we want it to take to move so what that's going to make sure is it's going to make sure that we learn from the original position to the target position in the exact time that we want it to take so why do we do that well we can start with a while statement and we want to make sure that uh elapsed time is less than a time to move so that means we're still within our movement range next we'll set the player's transform dot
position equal to vector3 dot lerp we want to start at our original position we want to end at our target position and then we want our step to be like i said before elapsed time divided by our time to move so that'll start our movement going and we need to make sure that elapsed time actually increments so we'll add time dot delta time to that to keep it moving in real time and then because it's a core routine we need a yield statement so we'll just yield return nova and that would actually work perfectly fine
but what i like to do at the end of the movement core routine you can sometimes get a little bit of um a little bit of an offset and what i mean by that is if your original position is zero zero zero your target position is one zero zero as it's moving if this elapsed time doesn't give it enough time to complete that full movement you may be 0.01 of a unit out and that may not sound like much but if you move 100 times 100 squares then you're exactly one unit out of sink so
to rectify that what we can do after we've done our movement we can make sure that our player is at the target position just as simple as setting the transformed opposition equal to the target position so we always know that at the end of our movement we're going to be exactly on top of our target position and then we just need to go up here and change these move player methods into core routines so we'll do start core routine i'm just gonna copy that and move these if we save that that should be everything that
we need so we can come back over to our player and attach our grid movement script and we see we have perfect grid movements we can press it once and we snap to grid we can hold our keys we'll get continuous movement always constrained to this grid so it's just as simple as that i really hope you've learned something guys i really hope you start putting this kind of movement into your games it's a really good throwback to retro gameboy eras and it really pleases nerds like me so that's all i've got for you i'll
see you again next time thanks for watching guys don't forget to like comment and subscribe and follow us on facebook and instagram for more bites as unity hints and tips you