8 Directional Movement | GMS2 [1]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys today we're going to be doing a directional movement so in this first video we're gonna be getting the movement working and then in the next video we're going to hook up the sprites to our system now this video is aimed at beginners and I am hoping that you have a little bit of familiarity with game maker basically just that you know what sprites and objects are so I already have my 8 directional sprites so I got this off a website called open game art and I'll leave a link to this in the description if you don't have your own up for this already so I have the eight Direction split up into eight different sprites and you could also do it with just five sprites where you have for example up write down and the two diagonals and then you could just clip those to get the rest of the directions alright and to start off we the sir I already have a player object if you don't just create an object and assign it whatever sprite you want and we'll come right into its step event alright and hopefully you know what this is but this is the event that gets called every single frame with the game so unlike other events like the create event they are kind of one self events this one runs every frame so this is where we want to update the movement the first thing we have to do for any kind of movement is of course get the players input and I'm going to be breaking this down into the horizontal and vertical input right so we're gonna be using the right and left keys for the horizontal movement and the up and down keys for the vertical so the first one we'll get is the horizontal input so I'm going to call this H input and now this variable is going to contain a kind of sum of the left and right cable checks and of course we check the keyboard by using keyboard check and I'll go VK right and then I'm going to subtract kibo check VK left so if you're not familiar with this kind of way of getting the input the reason I'm doing this is because this function right the keyboard check function what this does is it checks if the player is pressing the right key and then it will return by the true or false and the same with this one and in game maker true and false is actually equivalent to 1 and 0 so if we're pressing the right and left key at the same time then this is going to equate to 1 minus 1 right so those will cancel each other out just like they kind of shoot if we're pressing both of the buttons but if we press for example just right then we'll have 1 minus 0 and it's just left's then we'll have 0-1 so you can see we're going to get different values for every single combination of the right and left checks so this is a valuable piece of information for the horizontal input so we'll do the same for the vertical input so I'll just copy and paste that and we'll put V input and then we'll use the down and the up and the order here is important because remember the XY axis of GameMaker it starts at the top left 0 for X and 0 for y so if we press the down key or the right key then we're going to be increasing the X and the y and that makes sense because if this side of our little sum if these two are being pressed then that sum is going to equate to 1 which is a positive number which means we are going to be increasing from where we are currently and if we're going left or up then we're going to be going in the negatives which is what we want so we have to make sure that these are in this order so now that we actually have the input let's apply this to a change in the X and y position of the player so now I'm going to go X plus equals H input and y plus equals V input that's technically all we have to do to get eight directions working although when we run the game you'll see that there is kind of an imperfection with coding it like this so I already have my room set up right it's just a small little room with my player in it so if you don't have something like that set up go ahead and do that now but I am going to run the game and have a look if this is working all right sir is my player its animating in the spot right now because I didn't turn the animation off or anything so it's kind of just running through that walk animation if I press down and up I can see that the player moves up and down and left and right also works and you can see if you press down and left it does move in that direction but it moves at a speed that's actually faster than the horizontal and vertical directions we move faster on the diagonals and just to illustrate why that is I'm going to show you the maths of what we're actually doing so when we move left and right or up and down we're moving one pixel in one direction right so the total distance we're traveling in one step is just one pixel but if we move at the up left diagonal then we are moving left one pixel and also up one pixel and if we draw a line between our first position and where we end up and then we measure the distance of that line that doesn't actually equate to one right you probably remember from school that that hypotenuse is going to be longer than the sides so this might not be what you want we always want that player to be moving one pixel or whatever your speed is and by the way if you wanted to actually set a speed what we could do is just multiply this value times a set speed value say times SPD and I'm using SPD of course because s peev is already a built-in variable so we can't use that one and I'm actually going to set that to a small value because my game is very small and if I just quickly rerun that you can see that the player now does move slower but as for these diagonals we're going to have to use a different method than what we're currently doing and we're going to be using a bit of vectors but there are some built-in vector functions for game maker that make this really easy so a vector is just a line with a direction and a length or a magnitude so what we need to do is get that direction that we want to move and then we want to calculate the length of that vector if it's length was equal to our speed so for me that would be 0.5 so once we have that vector we need to get the horizontal component which is kind of the distance we need to travel to get to that vector and the vertical component because that's how we end up moving in GameMaker we don't really have true vectors we're always going to be manipulating the XY coordinates sir first off getting the direction of our vector is actually really easy and I'm gonna call this da dir and this is just called point direction that will get the direction between two points and remember this direction is where we want to move in this one frame so our kind of change in direction so we could use the players X Y position here but we could also just imagine the player at the origin and then use the H input and V input to show us what direction we want to head in so the H input and V input is going to be the wrong length because it doesn't matter at this point how long the vector is we just want the direction that we want to move and then in our next step we're actually going to use some functions to get that exact length that we need to move to get the vector that we want so for the first point I'm actually just going to put 0 0 and then we can actually just use H input and V input because remember those were all going to be 1 0 or minus 1 and we can actually create a kind of unit circle using those directions and I'll take this opportunity to point out that directions in game maker are exactly like the unit circle so to go right that direction would be 0 0 degrees and you're up that's 90 and here is everything in between so let's just fill that in so series your H input V input right so that will get our direction and now with that direction we need to get the horizontal and vertical components of the vector and to do this we're going to use a very handy function the length the x and y functions so exactly as it says here if we're starting off right here and we want to move there then our X component of that vector can be calculated with length two all we have to give it is a length in a direction and it will tell us how far over that X needs to be to be kind of in the right place for that vector and the same with the Y using that function so we could reuse these variables but just to make it clear what we're doing I'm gonna make a couple more so I'm gonna say move X which is going to be that X component of the vector is equal to length to X and then if the length we want speed and the direction of course do and then the same for the wife and we use those exact same arguments and now instead of just H input we can replace all of this right here because we're already using the speed with this function so now if I run it hmm so the player just moves over to the right and I didn't even press any buttons so why is it doing this well have a think about it if we're not pressing any buttons then this is going to equate to zero zero zero zero and if we get the direction of that that ends up acquainting to just zero degrees which we know is to the right so that's why it's moving off to the right as it is so what we should do is only be updating the players movement if the horizontal input or the vertical input does not equal zero right so if we're pressing some kind of button or if the checks aren't cancelling each other out because in those cases we should not be moving so we can just wrap this entire thing in an if check if each input does not equal zero or the input does not equal zero so if either of those are equated to not zero which means we're pressing something then we can do all this all right let's give that eager there we go so we're just stationary as we should be and if we press the buttons now there we go we're moving and you can see when I'm moving on the diagonals I am still moving as fast as I am on the horizontals and the verticals alright so that is it for this video we have got the movement working and in the next one we're going to get the sprites hooked up to our system I hope you guys are well and I will see you in the next video
Info
Channel: FriendlyCosmonaut
Views: 47,080
Rating: undefined out of 5
Keywords: 8 directional movement, 8 directional movement gamemaker, 8 direction gamemaker, movement gamemaker, movement tutorial gamemaker, 8 directional movement gms2, friendly cosmonaut, gamemaker tutorial, gms2 tutorial, gms2 movement, gamemaker studio 2 tutorial
Id: 0-a0Fak7cjk
Channel Id: undefined
Length: 11min 17sec (677 seconds)
Published: Mon Dec 24 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.