Godot 2 tutorial: Horizontal platform movement

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this video like several upcoming tutorials will be in two parts first we'll introduce a new concept and then we'll put it to use in our platform game project in this tutorial you will learn how scripts connect to your objects and control them how they inherit from other scripts and then we'll create our character and give it some basic movement so you can see the result right there that's what we're going to get by the end of the video a character that moves horizontally with acceleration and deceleration that's basics for our game and you can see the code right there you'll find some companion files in the video description I'll put them up on github or something like that and for the purpose of this video you will need to create a new project I went ahead and did that there we go yeah no problem so we have an empty scene to start with and we just have to do a little bit of setup work to get it working so I'll add a node called world and then a kinematic body 2d that's going to be the skeleton of our playable character the kinematic body 2d works with physics basically it will allow us to use collisions and to control our character I'll rename it to player and note the naming convention and godo it's Pascal case so every word starts with a capital letter for objects all nodes if I were to call it the main player or player main I would give main a capital letter okay so we have our player and to be able to visualize it when we play our game we'll need to add a sprite note look for sprite in the search bar and add a sprite so that's the node that's responsible for displaying pictures on the 2d world by default and your resources on the left you will see you have one image the go root icon so we are going to use this one like the sprite and the scene tree and then click and drag the icon to the texture slot you will see it highlighted with a thin orange line and when you drop it boom our character appears in the game world I'll then select my player again press the W key to call the move tool and move my sprite to the middle of the scene there we go we all set up I'm going to save the scene now by pressing control s and I'll call it the world it's our game world and now let's just check if we test the game for me I already set this scene to be the one used by default by Godot but if you haven't already good will ask you to choose it ok and there we go we're setup we can get going now we want to add a script to our character to the kinematic body so right click on it add script and then I'll just enter the path I'll call it player the GD there we go can click on create and there we are working on our script so you can remove the comments that are there by default we'll just leave the ready function and I mentioned it really briefly in the past but the extends keyword is at the start of every script in GD script and it tells Godot which class this script which is a class in itself inherits from ok our kinematic body to the object is going to inherit from kinematic body 2d which is going to inherit from the node and objects and like physics body 2d so it will give us a lot of functions to work with like to move the character and all that stuff you can search for the classes themselves like by type kinematic body 2d you can see all of the inheritance hierarchy right there so we have kinematic body 2d at the bottom of the list I'll just write its name fully so we only have the class hierarchy for this object and there you go you can see it inherits from physics body 2d collision object to denote 2d etc up to object so it has all of the properties of all of these classes now you can extend from a base class from the engine like kinematic body 2d and you will just have to write its name or most of the time good-o will write it for you when you create the script but you can also extend from another script file from your own GD script file let me show you how I'm going to do a bit of wizardry behind the scenes there we go so now I have a second script called parent Gd and my player script I can just type extends parent GD because they are in the same folder it's going to work I can try out the game and I won't get any errors so there you go that's how you inherit from another file then I'll delete the file there we go well ready to get working on our GD script now we have a few call functions to look at which you are going to use all the time so you know that a game works in a loop your code is going to run thirty to sixty times per second most of the time and every time there's a new image that appears on-screen things have moved or whatever it means that Godot has processed all of your game logic all of your script and it's going to use key functions for that the most important one is called process in order to call this one we have to tell Godot that we wanted to process the script on every single frame so we'll add that instruction to the ready function well just type set process and then we'll use the boolean true to say set this script to process on every frame so now we have to add the function process and Godot is going to fill in a parameter Delta and that's the time interval between two rendered frames because it doesn't render frames at the same speed although it will display them at even time intervals I'll just add the pass instruction not to get an error and there we go when the game loads guru is going to look at your ready function it's one of the first ones it will look at and it will see the instruction set process equals true it's going to then take a note okay on every single frame I have to look at the process function and run all of the code that's inside of it there's another built-in function that can be useful it's called fixed process so we can tell Godot to use it by calling set fixed process the thing is we process the time intervals will not be even but if we add the fixed process function godo will do its best to call it at even time intervals so that's generally where you do physics for example but we won't need it just yet we'll work with the process function for now we can remove set fixed process and that's all of the built-in functions will look at fall now they are more to work with but we'll look at them in future videos for now we will get started coding our game character here's what we want our character to do we wanted to respond to using the left or the right arrow and we wanted to accelerate smoothly and decelerate smoothly when we release the key there's a little extra our character will slow down when it changes direction so let's see how we can do that we'll need a few variables to start with these are going to be the parameters that are going to control how our character moves first of all it can move to the left or to the right so I'll add a variable called direction and it will be an integer will be equal to zero one that's going to the right or minus one then we need some speed variable same thing I'll set it to zero by default this is going to be the speed of the character regardless of the direction so if it's going at 200 pixels per second to the left the speed will be 200 200 pixels per second to the right the speed will be 200 as well and the direction will allow you to modify in which direction the character is moving then we'll set the max speed as a constant we'll use the keyword Const and it's going to be 600 note how I typed it it's a convention you use caps to write the words and separate them with underscores so it is still readable then we'll need some acceleration and deceleration I'll just copy these real quick there we go now trust me on that one we need a variable called velocity and we'll talk more about that later but basically we're going to work with vectors and our speed is not going to be a vector it's not going to be a piece of information that allows us to move our character for that we need to combine the speed the time it takes to render the frame and the direction all together into a horizontal vector that's going to tell Godot okay move this character by I don't know 20 25 30 pixels on this very frame so now let's get started first of all we need to work with our input we need to listen to the players input we'll talk more about it in the next video because input is the topic for next week just follow along for this time and trust me I'll explain everything in due time so we'll start with a condition if input that is action pressed that's how we detect that a key is pressed and we'll use UI left let's the left arrow by default that's some default input that's defined by gooder every action pressed is UI left if the player presses V left a road want to set the direction to minus 1 I told you that's the left direction L if the input is UI right we want to set the direction to one and the thing is we need to handle the case where the player doesn't use any key if none of these keys are pressed direction is going to be set back to zero there we go now we need to work on our speed I told you I want us to use acceleration and deceleration so here's how we're going to handle both cases if the player presses the left or right arrow we want the character to accelerate and if the player doesn't press any arrow we want the character to decelerate how can we check that well using the direction because if the direction is set to one or minus one it means that the player is brace pressing either the left or the right arrow can use the condition if direction if Direction is something other than zero that's what that means zero or false if the player is pressing a key the speed is going to gain we're going to add acceleration times Delta to the speed so let me explain that first of all the plus equal characters mean add what's on the right to the variable on the Left we are going to add not just the acceleration but acceleration times Delta because it's the time interval between the last two frames that's how we make the motion smooth we accelerated for a certain amount of time only right else we're going to subtract deceleration times Delta and I made a mistake here acceleration should be like that and should be a constant and then the slow raishin as well so that's not enough just yet we want to do a little something more to see that we are going to give acceleration and deceleration some values so I'll use 1,000 pixels per second per second for the acceleration and 2,000 pixels per second per second for the deceleration well just add two lines of code to control our character's movement we'll set the velocity to speed times Delta and multiplied by the direction and then we'll move the character using the move method by a vector to this function needs a vector to we'll talk about vector a bit later and we'll use velocity for the X component of the vector and zero and now if we try out the game oh it's all messed up why is that well it's because our speeds can go up and down to any value so we need to use a little handy function called clamp we're going to write this instruction after the speed has been modified we'll call the clamp function so we'll set speed to clamp itself speed between two values which are zero and max speed the clamp function is going to take the speed value the first parameter and if it's below the second parameter it's going to clamp it to it so if speed is lower than zero it's going to be set back to zero and if it's higher than max speed is going to be set back to max speed let's try our game again and it works it now finally works now we have one little problem left look at what happens if I press the right key or the left key what happens when I stop the character stops right away and what do we want to get smooth deceleration the reason for that is because whenever the player stops pressing the left or the right arrow we set the direction to zero and then we use that direction to calculate our velocity so as soon as you release the left and right arrow keys velocity will be set to zero so the character will stop moving we need to handle not only the input of the player but also the last direction the character was in we could rename direction to input direction and then just add a variable called direction we need to replace these instances of direction by the input Direction variable our press control R to call the replace tool and I want to replace direction my input direction I'll select the area of the code in which I want to replace that variable check selection only so it's only going to modify down there and click on replace all there you go and now we need to do a few extra things we need to set the player direction to the input Direction okay but we not only need to do that because we need that to always be equal to 1 or minus 1 we don't want it to be set to 0 so to do that we're going to add the if input Direction condition again you'll notice one thing we have the same condition twice in our code I'm using the same condition at the top and at the bottom technically I could put this instruction right below speed plus equals acceleration times Delta but I won't do that and the reason is I want to keep my code organized you don't have to worry about performances like limiting the amounts of instructions and all and simple games like these instead it's much more important to keep your code readable so here I have everything that's input related and then down there I have the actual calculations that allow the character to move and I don't want to get them mixed up so now we have our direction which we're going to use for the velocity and now if we try out the game when you release the key your character smoothly decelerates until it stops that's how you create good motion for a side-scrolling platform game and then you'll just have to play with max speed acceleration and deceleration to get the right feel for your game but that's polishing and that's something we'll look at later towards the end of the project I want us to add a little extra thing when I change direction the character keeps its speed and it's really strange for some games it's going to work fine especially if you don't have acceleration and deceleration very much simpler platformer game but we want the character to slow down when it changes direction so we can add a condition for that if I'll put it in the movement section of the code if the input direction and our character's Direction are opposite will divide the speed you can type divided equally something like 2 or 3 maybe there we go and you can try out the game and now the character slows down and it makes for a much more natural movement something a little more realistic and that's it for this video thank you for watching please leave a like it makes a huge difference for the channel and in the next video we'll look at input which is very very important in platformer games but not only and we'll then progressively explore new tools and gooder
Info
Channel: GDQuest
Views: 33,472
Rating: undefined out of 5
Keywords: platformer movement tutorial, godot tutorial, gdscript, godot engine, game design, platform game tutorial, platformer tutorial, open source, Godot quest, nathan lovato, gdquest
Id: bSj7l25bdJ4
Channel Id: undefined
Length: 20min 21sec (1221 seconds)
Published: Thu Jan 19 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.