Godot 3.1 Tutorial - Smooth Top-Down Movement

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
good morning afternoon or evening wherever and whenever you are my name is Benjamin and welcome to a Godot tutorial it has been a little while since I've done my last tutorial video and sometimes it feels like the longer you go without making one the harder it is to make one so we're just making one today let's do this so we're going to be doing a top-down movement example for Godot and this is what it will look like in the end let me maximize it well maximizing it doesn't really help since we don't have a view but you can see you've got a character moving around on the screen the movement is pretty fluid and the character will slide along walls now this isn't anything too special nothing too complicated to do but it would be a good start if you wanted to make like a top-down shooter with zombies or something like that because it's got nice movement and working collisions so let's start it I I'm using Godot 3.1 got one stable so we will click new project here we're going to come to this folder and we'll call this top-down top-down movement like that and we need to create folder and then create and edit so we're in Godot here this is kind of a beginner tutorial but but I'm not going to explain in depth different areas of programming I'm just kind of cover it as we go so first we need to create a new scene I'm going to add a custom node and just do a node like this we'll call this world and we're going to switch to our 2d view now we're going to create a player and this player we'll be a kinematic body so kinematic body nodes and to do that we can just add add a new node search kinematic and we've got a kinematic body to D these nodes are used for creating characters that you want to that you want to be able to interact with solid objects in the room to collide with them but you want to be able to manipulate their physics manually in code and kind of fine-tune them so platform characters stuff like that it's a pretty common node to use we'll name this one player and we're actually gonna make this its own scene which just means it will be kind of its own little object in the room so we're gonna right-click and do save branch as scene save we're also going to save our world scene we can just do ctrl s in order to do that save and it usually names the scene after the node so once we've done that you'll see this little scene icon up here right here we can click on that and it will actually open up a new tab for our player scene and if we click on that you can see all we've got is the player here so click on the tab for our player scene and we're going to add a sprite node so click Add node and sprite we're gonna just drag the icon the Godot icon classic we'll be using it and we're going to add a collision as well if you can see there's a little error message or a warning message here saying that this node doesn't have a shape and so it can't collide with any objects so we need to give it a shape so let's add a collision shape node so we can do collision shape 2d and I'm just going to rename this Collider and we'll come to our shapes and we'll do a new circle shape so once you click on a node over here on the right you'll be able to see the properties of that node and we need to give it an actual shape this Collider so let's click on the empty and we'll do new circle shape 2d and [Music] once we've done that we can click on the circle shape and it will give us information about it and we're gonna give it a radius of 60 nope 32 there we go so we've got a nice circle Collider for our character like that we'll save if we come back with will save with ctrl s if we come back to our world scene here we can actually select the player and place them somewhere in the room but make sure you do that in the world scene and not in the player scene and the player scene we want to leave it just the way it is okay now let's add some motion to our character so we're going to click on our player well just come into here click on the player and attach a new script with this icon right here then we can it should just save it as player Gd automatically we can create this new script and it will open up into the script area so inside of here we want to create some variables so we'll create a variable called max speed and I'm going to set this equal to 500 and we're gonna do an acceleration i'ma set this equal to 2,000 now Y is my acceleration so much faster than my max speed well with the kinematic body the we're going to use a function called move and slide and it automatically applies a delta value to the motion that you pass into it which is great but when we're accelerating and decelerating we need to apply we need to apply Delta to that as well so that our acceleration and deceleration looks smooth with with the framerate of our game and so it automatically applies Delta to the motion in the function but we have to manually apply Delta to our acceleration and deceleration and so this number is huge right now but once we apply Delta to it it will get quite a bit smaller because the Delta value is really low then we're going to create another variable and this one will be called motion and we're going to set it equal to vector two and so a vector two is just an X and a Y value that will represent our motion on the Cartesian plane so we'll be traveling either X positive x negative Y positive Y negative and those two values combined will create a 360 degree motion for our character so once we've done that we can come into here and we can well let's create a few functions here so we're gonna create a function called get access okay and we'll just call pass right now and we'll create another one called apply friction and it's going to take an amount we'll just pass for now and another one called apply movement and it's going to take an amount as well and we'll just pass for now on that so we've got these three functions we're going to be using them inside of a built-in function this function is called physics process so this function is run every single physics frame of the game or it's it's run it's it's updated on the physics frame I'm fairly certain so it's run every single physics frame of the game and we can use this to move our player around so the first thing we're going to want to do is get our access of our access equals get axis okay so we're calling our we're calling our get access function right here which currently doesn't do anything but it's going to and let's actually rename it let's call this get input axis get input axis so this is going to be getting our input and converting it into an axis value that way we can easily use that to determine which direction we're heading so once we've done that we're going to say if axis equals vector 2.0 then we're going to apply the friction and our amount is going to be acceleration times delta else apply the movement and the amount is going to be axis times acceleration times Delta like that and finally we need to actually move our character so we'll have motion equals move and slide motion so let's talk about this a little bit this logic so we're going to say if our axis is equal to zero vector to zero that's a that's a vector that just has zero for the x value and zero for the Y value it's basically the origin so if our axis is at its origin centered then we're going to apply friction if not then we want to apply movement so this we can use this to determine whether or not we we are getting some sort of input from the player now you can imagine that this axis is like an analog stick on a controller right and when the when the analog stick is at its resting position in the very center that would be vector to zero zero like this but if you press the analog stick into a direction then it would create some sort of a value depending on the direction that you you pressed on your analog stick so we apply the friction when they're not moving if they are moving we apply that force the movement force now the move and slide function is built into Godot and it will just attempt to move it's built into the kinematic body it will attempt to move a kinematic body and if it runs into a wall or something it will attempt to slide along that wall and this function returns whatever motion was left over so that's why we're setting our motion variable equal to whatever was left over that allows us to continue sliding it allows us to remember the previous motion after the collision basically whatever was left over after the collision okay so one other thing I want to mention here is that you can easily hold ctrl and click on a function like move and slide and you will get the documentation for it or you can search the help and just type in kinematic body like this and you can see all of the functions that it has who it inherits from and the documentation is very nice that way the physics process here we could also see if we click on it it's under the node called during the physics process step of the main loop physics process means that the frame rate is synched to the physics so that's what it is it's synched to the physics and it says the Delta variable should be constant because of the physics so once we've done this we can then we can then fill in these functions that we created earlier so how are we going to get the access the axis so we're going to create a variable and we'll set it just equal to vector 2.0 like this so that'll give us a actually let's do this one up here dot zero as well dot zero so we got a zero doubt vector then we're going to get the x value for this so VAR axis x equals int well let's just do this input dot is action pressure let's see pressed you I write minus input dot is action pressed UI left okay this is something that I would do in game maker a lot of the time which is a very common thing I think Sean Spaulding kind of popularized it with his videos but you will get whether or not they're pressing the right key and if they are this would return one and then whether or not they're pressing the left key let's say they are pressing the right key but they're not pressing the left key then we get one minus zero which would just be one and that would be our x value would be one so positive one if they're not pressing the left the right key and they are pressing the left key this would be zero this would be one so then we'd be negative one which would be to the left negative one an axis of negative one would be to the left so it's a really clever trick however we've got a problem Oh first of all we need to remove the VAR statement there because we've already declared that variable up here so we don't need to read it clear it but the problem is that these these values return a bool which is true or false we need to convert them to an int which would then give us a 0 or a 1 so we just type int parentheses and then add our ending parentheses here to encapsulate this function and then we'll do another one right here int and end it here to encapsulate this bit here to call it on this so we're going to do the same thing but with the Y value stop y equals int input is action pressed and for this we're gonna do down first and then up just like that so we've set the axis now we want to normalize this vector and what what that basically means is let me show you here real quick if we open up paint.net let's say we have our vector right here here's our whoa that was not very not a very straight line here's our cart Cartesian plane I can't pronounce that right now okay so let's say we've got our point 1 1 right here that vector right here this vector would have a length of greater than 1 because you can see if it if it was actually 1 it would be around this circle like this and somewhere around here so it's length is greater than 1 however let's say we had just a vector of 1 and the X so this right here would be 1 in the X and technically negative 1 in the Y right this one down here let's say it is 1 in the X and 0 and the y this one right here would have a length of 1 ok so if you normalize a vector what it does is it makes its length 1 no matter which direction it's pointing or how long it is it always just cuts it off right at this circle and makes its length 1 and this is a very very scribbly drawing but that gives you the general idea of what we're what we're doing here so we can say returned axis dot normalized and this function right here will just give it a length of 1 and that's beneficial that way we don't move faster at diagonals than we do at the directions if you remember a lot of times when you're making a game that's a problem that needs to be solved and a lot of older games they would actually just leave it so you just move faster at in diagonals than you would at the cardinal direction so this fixes that for us and then we return that value back out so our get input function is now working so let's apply friction so here's how we're going to do the friction we're going to say if motion dot length is greater than the amount so the amount of friction that we want to apply then we'll say motion - equals motion dot normalized times amount so we're taking our current motion and then we're normalizing that same motion and we're adding or we're multiplying it by the amount so let's say our friction amount is 10 for example then then this vector is going to have a length of 10 so if we come back into here let's say don't save let's say we're headed in this direction and our vector length is 100 okay so what we're going to do is we're going to subtract this same vector so let's take that same vector but we're going to normalize it which would give us a length of 1 right normalizing it would give us a length of 1 and then we multiply that by our amount which let's say is 10 and we're going to get a length of 10 right so then this is a length of 10 let's see that that's a length of 10 we're going to subtract this vector from this vector which would basically be mean that we're going to go in the opposite direction of 10 like this so we're taking our motion that was this amount 100 and after we subtract the 10 it's going to be the same direction but instead it's going to be 90 instead of a hundred so that's what our vector math is doing right here and once we've done that that bits done however if the motion dot length is less than our amount then we're just going to want to be at zero at that point so we'll say motion equals vector 2.0 like that okay now let's apply our movement so this one's pretty easy too first all we have to do is do motion plus equals amount so that's just going to add our new amount this might be acceleration let's let's name that better we'll name it acceleration like this so this is our acceleration vector whatever it is we'll add that to our current motion vector and that's it however we do want to cap our speed to make sure that we never go faster than our max speed so in order to do that we can say if motion dot length is greater than max speed then motion equals motion dot normalized remember so we're taking our motion but we're setting its length to 1 then we can multiply that by our max speed so that allows us to set our motion to the max speed now there's another way to do this actually I just remembered you could use the clamp function I think which is part of part of the vector class so we could say motion equals motion dot clamped and then just pass in max speed like this so that's that's actually a better way of doing it because then the clamp function does the math for us and it would it would be a little bit faster so we'll just do that instead but the other method works as well so we can save come into our world and run the game it's going to need us to select a main scene so we're gonna select our world scene for that and our character should be able to move around in this world it's got really nice smooth movement and of course you can play with your acceleration and Max speed values in order to get different in order to get different a different feel for the movement of the game so let's add a new let's add some color items that we can collide with we'll add in a a rigidbody 2d or a static body 2d and we'll add it needs a Collider so we'll add a new collision instead of doing a shape we'll do a collision polygon and so in order to do this we're going to actually set snap on so we'll come up here we'll configure snap will do 32 by 32 close will turn snap on by clicking on that you can see we get our grid now but then we'll also click on view and show grid so we can see this area then when we have the collision polygon selected you'll see these new buttons up here and we'll click on the green one this will allow us to add new points to the collision polygon so I'm just going to click there and then click here and click here and click here and maybe here and then I'm going to make some shapes here something like this and then I think I can click on that and then click up here so we've created this weird collision polygon shape for our static body and I'll just save the game and then in debug mode we're going to do visible collision shapes if I click up here click visible collision shapes this will allow us to see the collision shapes while we're moving around in the room and you can see we've got nice smooth collisions with these edges our character can move around in this world so that's going to be it for this tutorial I'm planning on doing more tutorials kind of like this some simpler ones I'm also working on a new course for Godot and I'm still working on my games once I've once I've kind of got the course started I'm going to be doing tutorial content and course content like support and stuff on Mondays Tuesdays and Wednesdays and then going to be working on my game on Thursdays and Fridays that's the current plan my two games actually I've got two in the works right now but so I'm excited to tell you more about the course and if you want to follow my twitch channel that's usually where I stream development on like games there will be like a link in the description where you can follow my twitch channel as well thank you all so much for your continued support and I will talk to you all later
Info
Channel: HeartBeast
Views: 64,074
Rating: undefined out of 5
Keywords: Godot Engine, Godot 3, Godot Tutorial, Godot Game Engine, Godot 3 Engine, Godot Engine 3, Tutorial, Make Games, gdscript, Open Source, godot, game development, game engine, godot tutorial, godot 3, open source, godot engine 3, Top-down Shooter, Movement, Smooth Character Movement, kinematicbody, kinematicbody godot
Id: BeSJgUTLmk0
Channel Id: undefined
Length: 25min 30sec (1530 seconds)
Published: Mon Jun 17 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.