Simple Top-Down character movement in Godot: Grid-based movement 1/4

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in the next few tutorial we will look at how to make a character move on a grid and do that in a way that will be future proof that will allow you to expand upon it the system works as such you have a character and it's inside of a grid just like every obstacle or enemy and all and the grid is responsible for telling every one of its children whether they can move to another cell whether there's something on the cell or not that's a way to make sure that you you don't have any problems when for instance an AI and the player want to move to the same cell in this video we will see how we can make a top down character movement you will see that the two are related because moving on a grid is just moving like normal but you move to a Target and you don't go past it then we will look at how to create the grid system that will check where the player is in the world maintain an array of every one of its children and their position on the game board and last but not least we will connect the player movement to the Grid in a third video I already made a video related to the design of grid based games in the past you can go watch it now that said let's get started as always you can find the project on GitHub in the start folder so the setup is quite simple you have a player that's a kinematic body 2D with a script attached and it has a Sprite that green round it Square just so that we can see where it's smothing click on the script to access the script itself which is just a very simple boiler plate it is set to process at fixed time intervals and that's it so now we can build the character we'll need to do a few things first of all we need to be able to get the player Direction where the player wants the character to move so the first variable we'll create at the top of the script is the direction variable and that will be a vector 2 simply because the character can move both on the X and Y AIS in a top down game a vector 2 is all we need to represent four or eight directions then I recommend one thing it's one thing I like to do is to create constants for the directions just so that they are easy to read in the script to create a constant WR BEC keyword followed by its name starting with top the top Direction it's going to be a vector 2 the top direction is zero on the x-axis it's nothing on the X however on the y-axis it's minus one that's how you create a unit Vector pointing up duplicate this line you copy and paste it and we're going to create the right bottom and the left so for the right it's one on the x-axis zero on the y- axis followed by down or butt whatever you want to call it and it's going to be zero on the x-axis one on the y-axis that's a vector pointing down next up we have left to wrap this up and it's minus one zero the advantage of having these vectors is that if we add left and down together we get the down left Direction because each of our vectors just make a difference on a single axis the next step is to detect the player's input we have the directions but they don't do anything for now and to do that we can just use the input object dot is action pressed so in this base project there are a few actions inputs defined for for you for move right up left and down if you go to the project settings input map you will find them at the bottom so let's start with move up I'll just use the same order that I used at the top here if the player presses the up Arrow we want the characters Direction our Direction variable to be equal to the top Direction else if if the player is using the right key is going to the right we want the direction to be equal to right and you get the idea for the next two you can copy and paste and just replace move right with move down for the down Direction and for the last one it will be move left the order in which you put those conditions makes a little difference it defines which key has the last word for example if the player is pressing the up key he won't be able to move right down or left at the same time the upkey is going to dominate over the others this input allows us to detect if the player wants the character to move in which case we will set the character speed to a positive value this means we have to create a new variable called speed and it will be equal to zero by default Also let's create a constant max speed and set it to whatever you want the character speed to be so let's say 400 whenever the player inputs a key we want the character to move in which case we could write speed equals Max Speed there's no acceleration in that case the character moves instantly now we don't want to copy that line full time so one thing I like to do is to to set up a Boolean value to check if the player wants the character to move let's create that it will be a local variable we just want to recheck for the same input but all inside of that variable we can create a Boolean value by using the operators or or and for instance and that's what we'll do so if the player presses the up key or if he presses the right key or if he presses the down key or if he presses the left key he wants the character to move note that we don't have to write all of that we can also create a a new input in the project setting to just check if the character is moving but that will be an input that will contain all the keys from the move right left down and up arrows I prefer to do it that way because at least it's a bit clearer the character is moving if the player presses one of the four possible inputs so now we can add an extra condition and Nest all the input ones inside of it if the player is moving then we check where he wants to move and because we know that one of the four keys were pressed we can confidently set the speed to Max Speed then if the character is not moving if there's no input that frame we want to set the speed to zero and believe it or not for a simple example we are almost done we just need to calculate some velocity if you remember the previous series on the platform game movement I told you that the velocity is a motion Vector so we have to multiply speed by direction to get the right direction in which we want to send the character but also by Delta the time interval between two frames this is the very basics of motion you'd see in Newtonian physics last step we have to move the character using the velocity Vector otherwise nothing is going to happen and you can try out the game at this stage and move the character in all four directions the setup is quite similar to the the platform game we did before to the horizontal movement and yes it's the same principles all the time the only thing is because of the added directions when we refine such a movement when you want to make it a bit more elegant it's going to add a bit of complexity all right let's add a little extra with simple changes we can make the character move in a Direction first of all we have to group the motion on the horizontal and vertical axes to together so we'll remove the L if on the Move left and we'll check if the player is either moving to the left or to the right together and we'll also check if it's moving up or down we separate the two axes we'll reset the direction to zero to nothing on every frame and then depending on the input will not set the direction but rather add to it and as I've told you when we created our Vector at the top here we just set them to be the top Direction the right direction down and left if we add those vectors the right and left are opposite however if you add the top and left because you only add minus one to the X component and minus one to the Y component you effectively get the top left Direction so just changing the a equal signs to plus equals so we add the constants instead of just setting the direction variable to it we can now test the game and you'll see the character can move in a directions there's a little thing there if we leave the game that way the character is actually moving faster when it's going diagonal than if it's moving on the X or Y AIS only this is due to the fact that our Vector can not only have only one unit on one component the x or y axis but now it can have one unit on the X and the y axis if you remember the Pythagorean theorem the length of the diagonal of the triangle's hypotenuse is the square root of two because each side is of length one so we have to normalize the direction vector by calling the normalized method this method is available for vector two objects and direction is one of them so we can call it here now if you play the game it will still move about the same way however now you can be sure that the character moves at the same speed in all directions thank you kindly for watching this tutorial to the end and remember in the next one we will work on the grid see you then
Info
Channel: GDQuest
Views: 59,510
Rating: undefined out of 5
Keywords: Top-down movement tutorial, kinematicbody2d, kinematic body, top-down character, zelda-like movement, Godot tutorial, Godot game engine, open source, learn game creation, game development, GDquest, Nathan Lovato
Id: BWBD3i00AfM
Channel Id: undefined
Length: 11min 22sec (682 seconds)
Published: Tue May 30 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.