Godot 3.1: Creating a Simple 3D Game: Part 2 (Programming 101: Character Movement) #GodotEngine

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Thank you for taking the time to make these.

πŸ‘οΈŽ︎ 9 πŸ‘€οΈŽ︎ u/jood580 πŸ“…οΈŽ︎ May 20 2019 πŸ—«︎ replies

Thank you for making these tutors for beginners :)

πŸ‘οΈŽ︎ 3 πŸ‘€οΈŽ︎ u/internetofthings1 πŸ“…οΈŽ︎ May 20 2019 πŸ—«︎ replies

BornCG making Godot tutorials! This is amazing! Godot is really becoming the Blender of game engines. We don't talk about BGE.

πŸ‘οΈŽ︎ 3 πŸ‘€οΈŽ︎ u/ccAbstraction πŸ“…οΈŽ︎ May 21 2019 πŸ—«︎ replies
Captions
hello and welcome part 2 of creating a simple move and dodge game using the Kudo game engine my name is Colin and this is part 2 of a mini series and how to create this simple move and dodge game where you the character are a ball moving around a 3-dimensional maze collecting coins avoiding enemies if you hit an enemy you lose and you go to the game over screen and if you win you do that by collecting all the coins again if you've not seen that video there's a link on the screen right now in order for you to go and watch that and follow along so you understand the basics of Godot in this video we're going to be programming character movement that means we're gonna get into programming using a godot's programming language Gd script that's enough of my intro let's go ahead and jump in but of course if you like this video overlooked something go ahead and click on that like button below this video on youtube and if you want to see more videos like this one in Godot or blender or technology click on that subscribe button and click on that Bell icon to get notified whenever I upload a new tutorial so on my desktop I've got Godot version 3.1 point 1 that's what we're using if I go ahead and double click on it it'll of course launch the project manager and you can just double click on your project in your list of different projects to open it up so in the last video we got to the point where we had a simple scene setup but it does not load up by default because Godot is based in scenes over on the bottom left of my screen in this file system dock is a list of all the files in my project folder including this level T SCN file this is the level of our game so I'm going to go ahead and double click on it and as you can see I've got the three different boxes or blocks that will fall with gravity I've got a ground and a camera if I want to select things generally I do that over here in the scene dock so if I want to select the floor I can click on it it's of course made of multiple nodes in a branch of this tree but of course if you want to test this game out you can press the play seam button and that will play this scene and there we go there are my falling blocks as seen through the camera I changed them since the last video just their positions so there we go so we're gonna make a character right away and we're gonna program it now I couldn't make a character just by pressing the plus button and adding the required notes to make a character we're gonna be using three nodes will use a kinematic body we're gonna be using a mesh instance and a collision shape last two of those we have used in last video to make our floor in our blocks now I could just select my level no this is the root note of this scene and press the plus button and add these nodes but instead what I'm gonna do is make my character object into its own new scene and this is how from now on we're gonna be making new objects or assets for a game because everything in Inga Doe is based on a scene so I'm gonna press plus we have a new scene and the reason why this is by the way the reason we're making different objects in new scenes it's little they can be reused because my character it might appear in more than one level if I make an enemy a bad guy he might appear in more than one level a collectible again you want to reuse it so you don't just want to have it based in the tree of nodes in one scene you want to have them separate so we have a new scene I just press this plus button we've got a new scene I'm not gonna add a default or one of these options I'm gonna press the plus and we're gonna make our character the root of this scene the character is gonna be under spatial it's gonna be a 3d kinematic body physics object so under spatial it's under collision object and then in that collision object category or branch we have physics body and kinematic body is right here I'll double click on it or of course you can search for it in this search bar kinematic and of course you're adding if you were to search for it on the pink one with a pink icon not the blue that's the 2d version always the pink for 3d so when we add this kinematic body to our node tree as the root it gives us a little air you've seen this before in the last video if I click on it it says this node has no shape so it can't kalaiy or interact with other objects consider adding a collision shape or collision polygon as a child to define its shape what is it talking about well this kinematic body is a physics object but it needs basically a force field or a bounding box to know where it should hit other things in the scene where it should bump into other things so I'm gonna select it and I'm gonna press the plus I'm gonna add a collision shape and I'm gonna be lazy and search for it collision shape and it's right there it's the pink one not the blue ones okay and I'll double click on it to add it and it's a child because it's indented it's underneath and farther from the left of this tree of the kinematic body and it gives us an air so I'm gonna click on that little blue or blue yellow triangle and see what it says it says a shape must be provided for occlusion shape to function please create a shape resource for it so the key word here is resource a resource is not unknown a resource is information in in the form of a block of information that dough has built into it that you can tell that node what to look like or give it a resource like a mesh or a shape that Pedone knows about and has built in so I'm gonna select this question shape I'm gonna go over to my inspector on the right hand side and with a question shape selected it shows me that its shape is empty so I'm gonna select a primitive 3d shape to give it as a collision bounce now our character is a sphere that's ball but it's collision shape and this might seem weird it's gonna be a box and the reason for that is because a box sits flat on the ground a box will also hit things evenly like walls flat if you have a round sphere shape and you push really hard against it against a wall that's not much taller than about halfway up the ball the ball might actually roll over the wall so we have actually found that when I've been testing this project so we're gonna use a box closure shape but in that box were as another child of a kinematic body we're gonna add a - and it's gonna be a sphere so I'm gonna select my kinematic body again and press plus I'm gonna search for mesh and we don't want to select mesh or CSG mesh we want to select mesh instance and it's a parent of something else but that's okay we can still add it so double clicked on mesh instance and so now it gets added again as a child of kinematic body and if I select mesh instance it needs to know what polygons are what actual mesh with vertices and edges to use and again that's over in its properties in the inspector it's mesh it's empty right now we're gonna click on a little arrow next to it and select a new sphere mesh that's a primitive mesh that the dough has built-in that you can use at any point so now we've got a ball mesh inside of a force field essentially the default dimensions of any shape basically in Godot and boxes and spheres is two of these grid floor units by two by two so if I were to go into my little View menu and say orthogonal to make it more flat and then if I were to go to my let's say my front view you would see that the edges of the circle just graze the edge of the bounding box the collision shape if I select the collision shape I can see those extent little pink circle handles that I could grab but that means that my ball wouldn't hit things accurately so I'm gonna press ctrl-z a few times to undo that and my cubic body is ready to save as a scene before I do that though I'm gonna double click on its name and I'm gonna give it a better name than kinematic body my character's name is gonna be Steve so I dosed double clicked and typed and press Enter and I'm ready to save this scene it says unsaved right here but right away we want to keep organized my project folder this res folder in my filesystem doc shows me an organization of files and you're gonna start getting a whole bunch of these dot t SCN scene files we're also the recovery bringing in image textures and normal maps and diffuse maps and maybe SVG files and all sorts of things and and nothing is worse than how in a disorganized project folder if I were to minimize Godot and look at my project folder on my computer I want to create folders here but we can actually do that in Godot for a different category so I'm not gonna do it in my Windows File Explorer I'll do it right here now if I right-click in this area it doesn't do anything and that's how you think that you would make a new folder just by right-clicking and saying new folder that does not work what you have to do is actually right-click on any of your existing files or in fact this word res here our actual project folder if I right-click right on it which is a little bit weird I can say new folder and I'm gonna name this folder assets ok these are gonna be where all of the scene objects or the objects that are scenes for my project are gonna go so I'll press ok we've got a new folder called assets I'm going to go into there or just have that selected and when I go to scene and save scene it's gonna ask me where to save it and so I'm gonna go into my assets folder I'm gonna give this scene a name and it just gives it the name of the node of that scene so it's named it Steve with a capital asset Sen that's totally ok I'll press save and there we go we've got a save character I can go ahead and close this scene now because I'm gonna create an instance of it in my main game level how do you do that well I'm gonna select the roof node of this level scene so I'll just click right here instead of pressing the plus to add a new node I'm gonna press this link button it gives me a list of all the TSC n files in my project folder including asset slash Steve T Sen I'll double click on that and as you can see if i zoom in there Steve if I want to move Steve up he got put to the middle of the scene where this spatial node actually is it's at 0 0 0 hopefully so if I select Steve if I drag up it will move them up but if I hold ctrl down and it's control on a Mac or a Windows PC you can try using command on a Mac but I don't think that works so I'll hold ctrl and drag that up and it'll Knapp to the increments of the grid floor so I just moved it up by one and so now it's on the floor if I want to get rid of these boxes I'm gonna get rid of two of them I can just go over here to the scene talk and the right-click and say delete nodes and yes and look that one right click delete nodes and okay and you know what I'm just gonna leave box one there for fun but I'm gonna move it over and off to the side because it'll fall down and once we get our character moving we can actually push this box around so let's go ahead and see what this scene looks like I'm gonna do a quick control s on my keyboard to save and then press the play scene button and we have our little ball character there and if I press the arrow keys on my keyboard it doesn't work because we have to get into programming so let's go ahead and press the close window X button so to add programming in Godot you have to add what's called a script file which is a file that ends in dot Gd onto a node in your game scene window in your node tree so I'm gonna select Steve and I'm gonna press this little script button with a green plus when I do that it's gonna pop up a new window but it's also gonna get a new icon next to it right now we can hide any note in our game this little icon means that this Steve object is actually a different scene same little icon similar that we have up here it means a scene and if I click on it I can actually open up and see and edit that original steve scene but i don't want to do that so i'm gonna select steve and press this little script new button and we get this attach node script popup or it lets us select some options for the code that we're about to add including what language we're gonna use we're gonna use GD script there are more options and if you use the mono version of Godot in other words when you went to Godot engine org and you went to the downloads page and you've selected the mono version this version allows you to program in C sharp it does require a separate download it requires you to download Microsoft Visual Studio or at least the Visual Studio build tools which is quite a big download so I'm gonna do about in this miniseries so I'm just going to leave that as GD script we're gonna leave this character as inheriting the code that's accessible to a kinematic body that's kind of what that means we'll get into it later template by default it says default I would like you to change this to no comments that's the best way because mostly people just get rid of the comments that comes with a default code setup anyways if you don't know what that means that's okay and last but not least it's gonna put this code file into your assets folder and I do not want that so I'm actually going to press this little folder button we're gonna make a folder in our project folder for scripts so I'm gonna press this folder button and I'm gonna go one up to go back out of the assets folder into the res folder I'm gonna make a new folder here called scripts so I'm gonna press create folder and scripts and press ok and now it's in that scripts folder I'm gonna name this code file or script file Steve dot GD and I'll press open and I can press now create if you were to try to organize your files and folders later you would actually be breaking the path so I believe so your your character wouldn't know whether their script file is if you try to sort things out into folders after you'd already attached scripts to objects that would be a pain so we're just doing it up front to save ourselves a headache later so I'll go ahead and press create and here you can see we have the beginnings of some basic code I'm gonna go ahead and press ctrl+ on my keyboard a few times so you can see what's happening here the first thing that I need you to understand concept number one in this video is that code that you write is executed or run by your game or your computer program in the order in which you write from top to bottom so when you're writing code and you're writing instructions for your computer that's what computer programming really is it reads the very top line it does that it reads the next line it does that this is called the flow of your program and this is a very easy concept to understand but sometimes what will happen is when you're writing lines of code instructions maybe you've put things that those instructions need to know lower down on your code so when it reaches an instruction and doesn't know part of what you're talking about because it's lower down you might not understand that as a programmer I'm getting a little bit ahead of myself though the second thing that you need to know about programming is that programs can get very long in terms of the number of lines of code that you need to write nor to make the program or game that you want now if you had a program that was let's say a thousand lines long it would start to get really messy and disorganized if you can imagine a program like Godot itself took tens of thousands if not hundreds of thousands of lines of code of programming in order to write well they didn't just write that in one big long script file they have to break it up into smaller chunks and what you can do in a big program is create small chunks of that big program that are really little programs called functions functions are small programs that make up a big program now you can use these functions that are actually built into the GD script language there are many many functions they're often called methods that are built into code oh but you could also write your own functions and you can actually see one on the screen right now on line three in line four you have this thing called func ready and right below that you have the word pass now something that I want you to recognize right away is the fact that this word pass is indented it has a tab here if I select that and press the Delete or backspace key on my keyboard its lined up and if I press the tab key on my keyboard it's indented and the GD script code editor gives it those little icon that means tab that's what that little icon means and that's very important this func ready pass in a little program that a kinematic body object like Steve knows to run or call as soon as the game loads and as soon as Steve is ready to do anything in the game in other words when Steve is ready for the game so this ready function is built into a kinematic body and Gd script knows about it now this past is actually a placeholder that you're meant to delete and replace with your own code and I'm gonna ask you to do that with me right now we're gonna use another function that's built into GD script it's called print and print is the most basic function you can possibly use it prints text to the screen so if I type PR int and again we're indented from the side that has to happen in order for this function to know what's a part of the function and then I type an opening round bracket and then a closing round bracket I've just used the print function in order to print something to the screen but the print function needs some information it needs to know what it you're want to write to the screen so I'm gonna put inside those round brackets two quotes double quotes and in that I'm gonna say hello Godot what we've essentially done here is we've created what every programmer types for the first time whenever they learn a new language and that's called the hello world program the hello world program is basically the most basic thing you can do in any programming language and that is to print something out text to the screen let's go ahead and see what happens if we run this this script in this game right now if I do a quick ctrl s on my keyboard and I press the play scene button where that we're still in level one right now it'll play my scene as you'd expect so I've got my character and the following box there but look down here and this output doc it says hello Godot if I go ahead and close this scene and I change hello Godot to something else hello YouTube and I press play a scene I get at the bottom in the output a doc hello YouTube okay so we've just used a function with what's called a parameter information that you put in the round brackets is called a parameter that little program print needs to work we just run that for the first time and it ran this print function when this came addict body was ready to be a part of the game and when it is really a part of the game it runs this ready or calls this ready function that this file came with are you following if you're not don't worry you can follow along as best as you can and just type what I type in this video and we'll get through it and you'll have a care removing learning to program is not easy and I'm breaking it down for you as best as I can it's learning a new language it will take you time now this ready function is built into a kinematic body and it knows what to do it knows to run this function when a kinematic body is ready to be a part of the game but a chaotic body has many functions which are actually called methods and if you want to see all the code that's available to us when we use the kinematic body I can hold the ctrl key on my keyboard if I hover my mouse cursor over this name kinematic body and I hold ctrl + I click on that name it's gonna open up the documentation about kinematic body and tell us everything about it if you're a programmer you'll really get into this quite deep but as you can see this kinetic body has methods or functions that it can use and they're all listed here get floor velocity get slide collision get slide count is on ceiling is on the floor it's got little keywords beside it we'll get to that in a few minutes it describes what this class this type of object is about if you want to go back to your code file you can just click over here to get back to it if you want to close this you can right click and say close just so you know you can hold ctrl and click on just about anything to see what it's about and it'll show you in this window and you can switch between documents on the left hand side but what I'm gonna do now is use another function or write another function that a kinematic body knows about it's called physics process so I'm gonna go a few lines below all the code that I have so I'm gonna click on the end of my last print line I'm gonna press Enter but I'm still indented if I want to write a new function I have to press backspace or delete on my keyboard to go back to the beginning of the line and that way when I go down one more time it will be at the beginning of the line I'm gonna go ahead and write and so should you func space underscore physics and you can see that as soon as I start typing it it's giving me hints to think it's trying to guess what I'm trying to type this is what we're going to actually type so instead of continuing to type I'm going to press ENTER on my keyboard and as you can see a type 2 for me so that's a little bit of code help that GD script or this co-editor has built into it this function called physics process is a special function that acts as a game loop in other words any thing that I put any code that I write inside this function I'm gonna get rid of this line eight because it's giving me a red bar any code that I put inside this function in other words any code i put below this line six but indented so in the body of this physics process function any code that I put there it'll run it at a solid or constant 60 frames per second what can go in here well I might put in here code that checks to see if the player of the game is pressing an arrow key on their keyboard and the words I could write some logic here that says if I'm pressing the left arrow key on my keyboard move my character Steve to the left or if I press the up arrow key or any of the W a s or D keys on the keyboard it'll move the character around you might also in here do collision tests that check to see if your character is colliding with something else like an enemy or a collectable object like a coin and if your character is colliding with one of those things it'll act appropriately so your game logic will go here and it'll check it again and again and again 60 times per second to illustrate this I'm gonna use the print function down here again I need to be indented here for this to work and I'm gonna type PR int round bracket I'm gonna say hello and an exclamation mark and don't forget to close your round bracket there so if I go ahead and play my scene it'll not probably want me to save I might even save that automatically it'll launch up it'll it'll say hello a lot of times because this is writing hello 60 times per second and if I close my game I can actually scroll up and down and see how many times it printed hello a lot and the first thing it wrote because it it did this when the player was ready it wrote hello youtube and then I wrote hello a whole lot of times okay so we've talked about functions I actually don't want to print hello YouTube and hello in my game so I'm gonna get rid of those lines and because these functions are necessary or are built into kinematic body you can just leave them empty in this case we're just gonna put that word pass pas s back into both of them just for now okay the third concept that you need to know about programming is a concept called variables if you've gone through middle school math or high school math you know what variables are if I wrote a math equation like 12 plus x equals 15 well you could figure this out in your head we know that X in this equation is a variable and it equals 3 because when I subtract 12 from both sides of the equal sign in this equation then we're left with x equals 3 but this is not a math lesson so don't worry we just have to know that this X is a variable it's a letter that we're using in place of a value like a number and that value might change over time in programming we to remember things or have our program remember things all the time like in the beginning of a game you might ask your player to type in their name and then you might use that name later in the game like if they finish playing it's game over and you want to save their name to a high score list or if you want to use the name in the actual game when characters talk to each other other times you might need to remember a number like a coin counter in this game project example so at the beginning of the game we need to make a little piece of memory for that coin counter number and you can think of this like a box we're putting information in a little box little spot in memory in order to make a variable in programming we need to give that box where that variable a name though and we can name it almost anything we want I'm gonna name this variable coin counter and anytime we need to a change coin counter like add another coin with our character collects a coin we can just refer to that coin counter by its name and add 1 to it if it's a number variable so in programming how we do this is up at the very top almost of our code if I just put in a few more lines here and you can make as many new lines as you want if I type the keyword var var all lowercase that's the keyword that tells our program it's an instruction that we're making new variable a new spot of memory to hold data the next thing I have to do is type a space and give this variable a name in this case I might say the word name and if we're looking to store a person's name in a game we might give it a value to start and so what I might do is put a space and then an equal sign and a space and I'm gonna put a default username to begin so I'm gonna put in quotes player okay that's the most boring default player name of all time now I want to print this player name out to the screen we can use that print function again so in this func ready I'm gonna replace this word pass with print I'm gonna write my round brackets because you have to write round brackets to call a function instead of putting some quotes in this print functions parameter area I'm gonna write the word name and because we're using that same word we're actually referring to the value of that variable instead of just the text n am E so if I go ahead and play this scene now yes going to print something on the screen well you know what I'm not allowed to use this word name as a variable name it's giving me an error and this is a perfect example of what you might encounter if you make a mistake or you do something it's not allowed in programming is telling me it's giving me a purser error at sing member name already exists in parent class basically what it's telling me is that the word name for a variable name is already taken so what can I do well I'm just gonna name this variable something else I'm gonna call it player name and I'm gonna give the N in the middle of that word a capital letter just to make it easier to read and therefore when I print out the player's name I'm going to write player name with a capital and in the middle and now if I go ahead and press the stop button and then the placing button it should print out player on the screen if I made my default player name I don't know Susan and I press the placing button it would print Susan on the screen so as you can see you can use a variable just by calling its name now something else you can do is you can combine or join or manipulate or use data from variables together in this case I don't want to print just a player's name I want to say hello to that person so I'm gonna write the word hello in quotes in this print function call but if I just write in quotes hello and maybe a little space there it won't quite work it's gonna give me an error right away and what I need to do is what's called concatenation concatenation is a funny sounding word but it basically means join two things together so in the middle of these two types of data they're actually both strings a string is basically a piece of text or characters in quotes I'm gonna put a space and a plus symbol and a space between those two and now you can see the red bar has gone away and if I press the play scene button you'll see that it says hello Susan now variables don't just have to be words again they can be numbers as well and we can work with those as well so I might make a few more variables this is just for practice and just for fun for learning if I say var or making a new variable here a I'm gonna make it equal to now this equal sign actually means a sign of value when we're writing a var player name equals Susan we're actually taking what's on the right side of that equal sign and assigning it into the memory that we named player name same thing on the next line down we're taking whatever is on this side the right-hand side of this equal sign and we're putting it into this variable I'm gonna give it a value of 5 I'm gonna make a variable B and make it equal to I don't know 6 and down here in my print function when this object is ready I'm gonna write a space + space B so I'm actually adding together the values of these two variables and because these variables are both numbers if I go ahead and press the play scene button it actually puts 11 on the screen it's added those two numbers for me because I use this plus operator okay that's enough of this practice let's go ahead and type it we're actually gonna put in our game I'm gonna get rid of all three of these variables and I'm gonna replace this print with a pass and we're gonna create a special kind of a variable that holds more than one piece of data there are many kinds of variables there's variables that hold letters or words and that type of data it's called a data type and that data type is called a string it holds characters and letters and words and paragraphs another basic kind of data type is an integer in other words a whole number you might count coins or stars in an integer variable but if you need to hold a number that has decimal points after it so partial numbers you'll want to use what's called a float variable or a float data type now there are many more kinds of data types out there and there are complex ones that's what we're about to use right now I'm gonna go ahead and make a variable and I'm gonna ask you to copy this for the game that we're actually making so we'll type var I'm gonna type the word I'm gonna name it velocity I'm gonna put a space an equal sign and a space to assign a value and we're gonna give it a complex kind of data it's a number that actually holds three numbers in one it's not like an ordered pair in math you know when you're graphing and you have the X and the y-axis and you plot a point on that graph well the way that you would write that point is in or round brackets and you would write something like 3 comma 2 and that's called an ordered pair because you know what order they go in the X is always first and the Y is always second that's an ordered pair but we're working here in 3d so we need three dimensions we need what's called a vector three and that will look something like this 3 comma 2 comma I don't know 6 and that would be the z-axis in our XY and z coordinate system now when we're calculating velocity speed of an object we can also use a coordinate system because we can dictate the speed in all three directions using that same kind of an ordered pair so to make this kind of a vector3 data-type I'm gonna type in vector it need to be a capital yes capital V on vector three and I'm gonna use round brackets kind of like a function this actually is a function called a constructor a constructor is what you have to run in order to make a complex kind of data and I'm gonna type in the round brackets it needs to know three numbers and we're gonna set the velocity to zero zero and zero and as I type that you can see it's giving me a hint it's saying that the first value is the X the second is the Y and the third is the z-axis so there you go that's what we need to make for our variable the fourth concept in programming that you need to understand is called an if statement an if statement is a logical statement it's a test to see if something is true if a condition is met like if the right arrow key is pressed on a keyboard then move right now what I just wrote here is not actually code it's called pseudocode it's English language like text that looks kind of like code and looks kind of like English that's easy to read but the code actually isn't too much more different than this we just have to know how we actually say if the right arrow key is pressed and we have to structure or format or use syntax and in other words structure and symbols in coding in order to make this actually work and be understood by Gd script and Godot if I want to write that I'm gonna put that if statement check inside my physics process our game loop function so I'm gonna replace this pass with the word if I'm gonna put a space and you should write this as well because this is actually code for our game the next thing we're gonna see is if there's any input and to check for input in Godot you write the word input with a capital I so input inpu T and then if you type a dot you're gonna see different kinds of input if you use the arrow keys on your keyboard so I'm gonna go down a lot of these connect things don't really make a lot of sense at least not at this stage but if I keep going down you're gonna see things called action press and action released an ad joy as in joystick mapping and AD signals and calling and connect we're gonna be looking for is if what's called an action was pressed in other words if the user did something called an action so I'm gonna type is and we're gonna see a bunch of things that look like they might be the right thing there is is action just pressed there's is action just released so if they press that on a key or if they press down on a key and then lifted their finger up to release it if an action pressed without the word just and it keeps on going what we actually want it does not have the word just in it we want this third one so I can use my arrow keys on my keyboard and go down a couple of times and press ENTER and it'll type it for me and in fact because this is action pressed is a function call it's actually a method call these things that are built in are called methods but it's really the same thing essentially it put a round bracket and then it gave me a hint about what I should put inside we're gonna be putting inside is basically a keyword that in our project settings we have defined or mapped to a key on our keyboard so I'm gonna use my arrow keys and see what we have we've got oh there we go you I left okay so if this you I left there must be you I right I'm gonna select that one and press enter now you could type this for yourself and that might actually be faster so I could type you know quote you I underscore R IG HT and close the quote and I'm gonna close the round bracket but when you're calling a function or a method there's round brackets and it has to be two of them now this is called the condition input dot is action press you I write a condition in programming is really called AB lien expression the word boolean is a word that means true or false or yes or no this statement can't be ambiguous it can't be well maybe no it is or it isn't it actually translates in a computer to a zero or one and so whenever you're writing these if statements you have to be very very precise about it and this is a key is pressed or it is impressed we need to write the result now so I'm gonna write a colon this is called the syntax of the language what kinds of symbols you have to write and where you have to write them press the Enter key on my keyboard and you'll notice how it indents again this if statement is indented which means that it's a part or inside of this physics process function that loops but in order to have a condition and GE script to understand it we have to put the condition indented one more time so we have two tabs here how do we move our character right well I'm going to adjust the velocity of our character in that first slot on the x axis because X is side-to-side so I'm gonna write indented it below this if line velocity dot X is equal to five okay is that it let's go ahead and go back to our 3d view up at the top in fact what I'll do is press ctrl s to save I'm not even gonna go back to my 3d view in fact I'm gonna press the play scene button and let's see what we get if I press the right arrow key on my keyboard we've got the code for it it doesn't work and that's because we actually haven't told our character to move I'm gonna close my game and we'll talk about it for a sec when we press the right arrow key on the keyboard and that code does work it will make the velocity variable its exits first number equal to 5 and that believe it or not does work if I wanted to test that I could use that print function I could actually print velocity dot X and it'll actually print velocity dot X let's go ahead and check that out I'll press the play scene button and in our output window and by the way when you actually export a game it doesn't actually show this output dock window this is just for our testing and therefore that print function is really just for us the programmer testing out things like this in our game if I press the right arrow key on my keyboard you can see every time I pressed it it put a new 5 down here so it's working so why isn't there character moving well we actually haven't told our character to move we've just adjusted by pressing the right arrow key on the keyboard this variables value that's great how do we get things to move well it turns out a kinematic body has a special function that moves itself and that function is called move and slide so instead of printing this out I'm gonna delete it and I'm gonna press backspace one more time my keyboard to align myself up again to just being inside this physics process function loop and I'm gonna type move underscore and underscore and it's a second one there move and slide and it's a function or a method that requires at least one parameter one piece of information you need to know the velocity and it happens to want a vector3 which is what we already have so I'm gonna type its name here the velocity I'm gonna close out round bracket you can put more pieces of information here and put a comma it could take more parameters it could except you telling this moving slide method which way is up if you're making a game with gravity but in this case we're just gonna give it the first one and that's okay so now let's go and see what happens if I press the play scene button and I press the right arrow key my character moves to the right but I let go it just keeps on going there's no stopping in this game we have to program that ourselves okay so I hope you see where this is going I'm going to copy in fact I'm not gonna be lazy and copy and paste I'm gonna go down right below this first if statement we're gonna write for if statements in a row so I'm gonna press ENTER not to go down one line backspace I'll write this up again if input with a capital i dot is action pressed I can go down with my arrow keys and press Enter I'm gonna say a UI left close round bracket colon enter it indents it for me because it knows what i'm doing here i'm gonna say velocity dot x equals negative five okay I hope you see what's happening here I'm gonna be lazy here I'm gonna copy and paste those two ctrl C press Enter make sure that you're not pasting right here you have to press backspace to line me is all up otherwise it will not work and I'm gonna change is action pressed you I up and down and if we're moving up and down the axis for that is not X its Z so I'm gonna use Z and Z there so this should work it's testing to see what keys I'm pressing and then it moves the character so I press the play a scene button now and press the arrow keys on my keyboard I can go right I can go left I can go up and down but my up and down are actually backwards so I'm going to switch those around if I press up we're gonna use negative five and down will be positive five so now if I go and play my scene I press the up arrow key it'll move away from me and the down arrow key will move towards me so I'm gonna go ahead and close this game window and I need to point out to you that this code is quite inefficient if I try pressing the right arrow key and the left arrow key at the same time one is gonna override the other because one comes first in our code and is checked first and then executed if we're pressing the right arrow key down and then we might be undoing what we just did if we're also pressing the left arrow key so right now if I'm pressing the right arrow key it'll make our X velocity equal to five but then if I'm pressing the left arrow key at the same time well that's gonna overwrite the velocity X with negative five and therefore if I hold both keys down left and right at the same time it's gonna make me move left that's what I deal so what I'm gonna do is I'm gonna consider left and right in other words our x axis movement and our z axis movement up and down two separate blocks and I'm gonna use what's called an else if statement instead of the second if now in Gd script unlike some other languages like C sharp or Java you in those languages you might type else space if in GD scripts they know coders are lazy so you just use LF okay L F stands for else F so I'm gonna make both of the second if statements in each pair and LF but really what that means is I'm still only gonna have one action opening or the other if I try playing this right now and then I press the right arrow key it'll move right but I hold right and left it's still moving right because it's only gonna check to see if we're pressing left if we're not pressing right that's what this word else means it means otherwise so it won't even check this if I am pressing right and right will now take over same thing with up and down it doesn't really work quite how we want to instead what I'm gonna do is I'm gonna check first to see if the user is pressing or right and left at the same time and if that is not true then otherwise or else we'll check if they're pressing right otherwise we'll check to see if they're pressing left so I'm actually to make these X and Z arrow checking presses in chunks of three or sets of three this if is going to become an LF for else if and the first one we're gonna write an if I'm gonna do two and one so I'm gonna say if input dot is action press and I'm gonna say UI right and I'm gonna close that I'm gonna use a compound operator here I'm gonna use the word and yeah you're allowed to type and D and what this means is we're actually making a compound boolean expression two things that can be true or false if we use this word and both of them have to be true in order for the condition to be met and the code to be run that's right below it so I'm gonna be lazy here and copy and paste this input line and paste it right here and yes this is getting too long so I'm actually gonna expand this middle section out so we can see it all and I'm gonna say if the right arrow key is pressed and the left arrow key is pressed then I'm going to write my colon and press ENTER to indent on the next line if we press left and right we want velocity dot X to equal zero we want to stand still otherwise if I pressing right move right if or make their velocity a five to the right direction and but depressing left otherwise make the X velocity in negative five same thing for up and down I'm going to fast forward through this because it's redundant code I'm gonna copy and paste and I'll change my right and left up and down and my X to Z and let's go ahead and see what this looks like I'm going to go ahead and do a ctrl s to save and press the placing button and I'll press the right arrow key and then the left arrow key and then I'll press both at the same time and my character stops it's a little bit finicky because if I let go not at the same time and I did there it will move in that direction so it is a little bit finicky but we might be able to fix that later same thing with up-and-down I can press up and down I press both at the same time is that not working let's try that one more time up and down and at the same time you know what that's not quite working because I have UI up and you write down oh you know what I forgot to put my second if as an Elif or an else if so what I'll type LF there maybe you spawned back before me hopefully did let's go ahead and try this out if I go and play it and I press up it works down it works and I press both my character stops and if I let go at the same time they it makes the character stop so that's great but we need to our character to slow down so I'm actually going to check to see if my player is doing nothing or if I'm not pressing left or right and if so I want my character smoothly slowed down so these sets of three conditional statements or if statements it's actually gonna turn two sets of four we're gonna do one more statement it's called an else statement so I'm just gonna write the word else and this will cover everything else that could possibly true be true if they're not pressing right and left or they're not pressing right or they're not pressing left it means they're pressing nothing and that's the last case in this set of logical conditional statements that are in sequence so if I'm not pressing anything I want my velocity dot X to equal to zero or do i well that would make the character stop right away and that's fine you can have that but instead I'm gonna use a function that's built into Godot called lerp le RP and it's a function so it needs round brackets lerp is a method or a function that when you call it it will take and I typed that one bracket so you can see what it wants it'll take a number and it'll linearly interpolate it to another number over time by a weight that you specify in other words a percentage so if you want to take Flossie X and make it equal to a slightly lower number than it currently is all the way until it gets to zero that's what this lerp function is for so it takes three parameters it takes a from a two and a weight and this way to decimal a percentage decimal like zero to one so 0.5 or 0.1 or something like that for 550 or 10% so I'm gonna type the value that we want to start from in other words where you start from where it currently is so I'm gonna type velocity dot X then a comma what do we want to get to we want to get to zero I guess not a comma again what's the weight well I'm gonna lower it each time because this whole function 160 times per second right I'm gonna lower each time by 10% in other words zero point one I'm gonna close that okay let's go ahead and copy and paste this because we're lazy programmers here I'm gonna copy that else go down below the and I'll scroll down hopefully I maybe I'll press ENTER a few times so we can see this higher up on our screen and I'm gonna go down below the last elsif press ENTER and backspace paste that in I need to change my velocity x2 ZZZ and there we go let's go and see what this looks like if I play it in my game if I press the right arrow key my character moves left but then they slow down nicely to kind of a skidding stop if I press left and I can't remove it I press both the same time and let go hey it worked and it didn't it didn't glitch on me if I let go of one too badly up and down works the same way and now we have a moving character and hey you know what I can even push this block off the edge of my games floor but there is still one more problem with our code and that is that we're using hard coded numbers in our game we're using this five number a bunch of times and this is considered bad practice instead of using this five number we're gonna use a special kind of a variable called a constant a variable you're allowed to change as your code runs during the runtime of the game but a constant so I'm gonna go below this velocity variable and I'm gonna write Const that's the key word to make it I'm gonna call this Const speed and one of the things that's normal to do in GE script is to name constants with a capital name all caps okay I'm gonna give it a value of 106 and instead of using this five all the time I'm gonna write speed and the benefit to doing this is if you want to change the speed later you just have to change it once so I'm gonna write in fact I'm gonna be lazy and copy and paste this is bad programming practice that I'm teaching you but we're gonna paste in speed and I might make that a little bit smaller so all my code fits in the screen and I'm gonna do a quick control save and play my scene and now my character moves a little bit faster and it all works I can push that box off the edge and I want to make my character move really fast let's try 20 I don't know that's City and now my character move super fast and it's hard to control but that can be up to you that will be it for this for you all I'm gonna close this window so you can see the code that I have you should pause the screen now if you need this code because if you like this video or if you learned something go ahead and click on that like button below this video it really helps me out and if you want to see more videos like this one in Godot or a blender or a tech in general click on subscribe or that Bell icon to be notified whenever I upload a new tutorial check out my facebook page at facebook.com slash born CG that's where I post sneak peeks and previews of what I'm working on next and where I can communicate with you guys the most thanks for watching it'll be it for this one you you
Info
Channel: BornCG
Views: 123,805
Rating: 4.9681673 out of 5
Keywords: tutorial, lesson, 3D, model, beginner, programming, godot, gdscript, code, coding, character, player, game development, godot engine, game engine
Id: WpzI2ytz5MA
Channel Id: undefined
Length: 53min 16sec (3196 seconds)
Published: Mon May 20 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.