Godot 3.2: Let's Build a 2D Platformer!: Part 13 (Creating Enemies + RayCast2D's!)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Thanks for posting. I will definitely check it out!

👍︎︎ 3 👤︎︎ u/[deleted] 📅︎︎ Jul 05 2020 🗫︎ replies

I love how BornCG explains code. Always clear, layman's terms. He never assumes we know anything, never rushes. A great example of how computer science should be taught.

👍︎︎ 3 👤︎︎ u/VRbandwagon 📅︎︎ Jul 05 2020 🗫︎ replies

This looks well done. Nice.

👍︎︎ 2 👤︎︎ u/lenzo1337 📅︎︎ Jul 06 2020 🗫︎ replies
Captions
hello and welcome at 13 let's create a 2d platformer in the cadeau game engine my name is Colin and in this tutorial not so many series we're creating this 2d platformer video game of course in this game you control the player on the screen using keys on your keyboard of course you can walk run jump and fall you can collect coins jump on enemies and squash them get hurt by enemies and lose lives of course you can shoot fireballs to wall jumps find keys to unlock doors you know how to deep lat for more games work so in this tutorial in part 13 we're gonna start creating our game level enemies our enemies in our game are gonna be little blob objects that squish up and down essentially as they walk around and so this video will be about creating our enemy object which is gonna be a new scene in Godot which we can then reuse and create instances of in our game level and then in the next video in part 14 we'll handle our enemy player collisions because when we collide with our character into the enemy from the side that does something different than if we jump on top of the enemy so we'll handle that in the next video in this video we have quite a bit of programming to do though because we have an enemy that will fall with gravity and we as the programmer get to decide for each copy of our enemy if that enemy will fall off the edge of a platform and then maybe fall and land on another ground or or a different platform or if that enemy will detect the edge of a cliff and turn around likewise our enemy has to be able to detect a wall if it detects a wall it should turn around and move in the opposite direction so we've got quite a bit to do but if you have not seen part 1 to 12 of this mini series and how to create a 2d platformer in the cadeau game engine I'll put a link to this whole playlist containing all of my cadeaux 3 tutorials up on the screen right now check out that playlist if you haven't seen any of the previous parts of this mini-series but let's go ahead and jump into the Godot editor of course if you like this video please go ahead and click on that like Pablo's video it really helps out me in my channel and if it wants more videos like this one in the Godot game engine or in blender or other technology click on that subscribe button as well and click the bell icon to be notified whenever I load a new tutorial so as our game project stands right now we have our little character and as of the last three videos in this mini series we have coins to collect in our level including a heads-up display that on the screen that stays with the camera displays how many coins you collected in the game and of course if you collect all the coins in my case three you win the game and the game restarts so let's go ahead and check that out I'll press the play scene button our game level will load and my character falls I can move around and I can collect coins and yes it counts them up here and if I collect all three hey my game restarts but our game other than the fact that we can fall into the abyss if we fall off the edge of a platform well it doesn't have many obstacles so let's add some enemies to add an enemy of course we're going to create a new scene that's how you create new objects that you want to reuse in Godot so I'm gonna press the plus button our enemy object is going to be a physics object that we have to program not only gravity in but also different behaviors like detecting the edge of a cliff and controlling our characters left and right movement to make our enemy walk so we're gonna start off this new empty scene using the physics type of node that our enemy is going to be based on and that is gonna be a kinematic body 2d object now there could be a way of making an enemy out of something like an area 2d that's the same kind of an object or node as our coins because our enemy essentially could just detect our player entering that area of the enemy and then our player could get hurt or we would lose level and restart level or go to way you lose screen but in this case our our enemy is going to fall with gravity it needs to be able to detect platforms it needs to be able to hit our player and it our player needs to be able to jump on it so I think a kinematic body 2d is the way to go so in my empty scene I'm gonna press the plus button I'm going to search for I'll clear my search out I'm gonna add a kinematic body 2d so I have got in my reasons here I can search for kinematic as well make sure adding the blue to d1 and not the 3-1 of course so I'll double click on that that's now my root node of my scene I'm gonna double click on it I'm gonna call it enemy that makes sense and of course to it I need to get rid of this error which basically just says we need a collision shape to D of course so I'm going to press the plus button and I'm gonna add a collision shape to D not a collision polygon a collision shape there we go our collision shape is gonna be a rectangle we'll deal with that in a few moments but first let's select the root node of our scene one more time I'm gonna press plus we're gonna add a sprite although our character or our enemies gonna be animated so we're gonna add an animated sprite so I'll search for mmm and then I'll find animated sprite and of course it's the blue one so I'll double click now for our game we're gonna be using another one of the assets that I'm providing to you that I got from triple w Kenny dot and L they are an amazing resource for game assets including sprites three models fonts sound effects you name it they are not a sponsor of this video but I'm able to provide these sprites for you from them because once you get one of their sprites packs or asset packs you can then use them in your own projects and distribute them for free they are royalty free so I'm able to give this folder to you which is called enemies and it has three pictures in it so go ahead and download the zip file of this folder with these three slime blue slime blue blue and slime blue squash they're all PNG images which means they have transparent areas that's what this checkerboard is around the actual little blob enemy so I'm going to it get that enemies folder I'm gonna right click and copy it and go into my project my game project folder into my assets folder and I'm gonna paste this enemies folder the assets folder by the way is just for that I created to keep all of my game assets that means sprites and fonts and sound files things like that all in one place you don't actually have to have an assets folder okay let's go ahead I've got that enemies set of images in there by the way these enemy or slime blue pictures they were not all the same size as I got them from Kenny NL and so I actually edited them beep these picture files so they're all the same size if I hover over them you'll see that they're all 57 pixels by 34 pixels and if they are not all the same size and you try to use them all in an animated sprite you might find that they might not stay like they're on the ground and they might move up and down which doesn't look good so I've edited them and I'm providing those edited versions to you in the description area below this video on YouTube so they're in my project folder if I go into cadeau they'll import really quick I'm gonna select my animated sprite of my enemy I'm gonna go over to the inspector with that animated sprite selected it of course it has a frames resource which we need to create a new sprite frames resource I could do a resource in and then I can click on the word sprite frame it'll open up my sprite frame dock at the bottom in which we have an animation which I'm gonna call crawl I'll get rid of the default name and this crawl animation is gonna have those two frames of my enemy kind of walking which means kind of squashing up and down a little bit so I'll add slime blue and slime blue blue I'm not sure why they're named like that so now you can see that if I go through my frames 0 & 1 over here that's what it's going to do as its walking I actually didn't have that walk animation working in my demo game which you can play online at each dot IO which is in the description area below the link for that game which you can play this game we have a crawl animation I'm gonna make one more animation because we need a squashed pose and so I'll drag my slime blue squashed picture in okay so I'm gonna go back to my crawl animation that should be the default animation to make sure that is the case my animated sprites property for animation should be set to crawl and I will turn on playing so we can see what it looks like I think that crawl animation is too fast so I'm gonna go maybe down to two frames-per-second maybe three frames per second okay my collision shape we now need to make match the size of the sprite but we're going to actually make a little bit smaller because we want to have this object actually detect our player hitting it from the sides or from the top so the actual kind of size of the kinematic body is gonna be smaller so I'm gonna talk to Mike legend shape to D I'm gonna make a new resource rectangle shape to D and I'll make sure that I can see it it's now hiding behind the sprite so I'll drag it right below the enemy is bright and whatever is drawn on the bottom is drawn last which means it's drawn on top so now I can see it because it's in front because it's at the bottom so I'm gonna make this rectangle shape not as wide as our enemy okay so it's got some extra room on the sides and not as tall so if I make this too tall well I can just move it down and that's kind of what I want I want some room on the top and on the side so that should be good if you find the I made sprite too distracting you can turn playing off for now and then switch to whatever frame that you want to see to get an idea so I'm gonna make this a little bit shorter perhaps and move it down and I kind of like that make sure that your collision shake doesn't go below the edge of your sprite because your character your enemy will look like it's floating above the ground so right about there looks good to me my enemy needs a script so I'll select the root note I'll press the script plus button enemy dot Gigi's a name of this script file that's fine the template I'm gonna use no comments and other than that I'll press create and I'll get to scripting in just a moment but I'll just do a ctrl s to save this scene the scene will be called enemy T Sen that's fine it's in my res which is my project folder I'll press save and now I'll go to my level one and I'll go to my 2d workspace so I can see it and I'm gonna add an instance of my enemy into my level before I do that though I'm gonna create a new branch or a new folder essentially for my and so I'll select my root note of this scene and I'll press plus I'm gonna add an ode to D I can clear out my search no to D is right there this node to D will just act like a folder kind of like our folder or a branch called coins here it just kind of contains all of the clients same with enemies so I'll name this node to D enemies and then with that node selected I will press the instance scene button this little link button this want me choose a scene to create an instance of so I'll select enemy and press open and now I've got a little enemy in my level I'm gonna put the enemy above the ground right about here and so when I play this level it will just float there and it is a kinetic body which means it is a physics body which means my character will actually bump into it which is good I suppose my character can also land on it so first thing we need to do is make our enemy have some gravity and have some left and right movement so I'm going to go to my enemy script I might want to go back to my enemy scene just so I know that I'm working on the original and I'll press the script button to go into its script what I'm going to do is create the same velocity variable that we created for our character so if you recall we created above our ready function a variable called far we've gotta lhasa t and i'm gonna make it equal to a new vector2 and I'm gonna put my round brackets because you need to do that and so now we have a variable that has an x and a y property that can store left and right speed and up and down speed and of course we have a function that came with our script called ready but in order for gravity and velocity to work over time we need our main game loop which is a function I'm gonna press Enter and backspace to line up my cursor with the beginning of the line we need a function called func and then that's underscore physics process Delta that of course is built into Godot it's built into most objects and when you put that any code you put in there function will be run sixty times per second so I'm gonna make my velocity dot Y increase in value is sixty times per second so I'm gonna save velocity dot y plus equals that's a quick way of saying add to that variable so we're adding to the Y value of our velocity that means up and down we're gonna add to it I'm gonna say 20 and we can play with that value a little bit later and this will actually do nothing except for change the value of a variable until we make our enemy move using move and slide that's the same method that we use on our player to make our player actually move and move and slide is a method built into kinematic bodies that needs a velocity in it okay so if I save this enemy and then I go back to my level one I'll let you pause the video if you need that code but I'm going to in level one press the play seam button and so now our player or enemy pardon me falls down if I go and move my character down I should be able to see that at the beginning of my game good let's go ahead and make our enemy have a direction and have some side-to-side movement so I'm gonna go back to my enemy scene go back to my enemies code because my enemy is going to be detecting perhaps the sides of platforms like cliffs or it might detect a wall when it bumps into it we want to give our enemy a direction and it needs to keep track of what direction it's facing and the words is a going left or going right we're gonna use a number for that direction and I'm gonna make it be stored in a variable so up above my ready function I'm gonna make a new variable called direction and I'm gonna be a little bit tricky here I'm gonna call and just think of it this way my direction for right if my key with my enemy is moving to the right is gonna be one and if my enemy is moving to the left on the screen is gonna be negative one that's gonna help us out later on when we're doing multiplication than making our and I mean move in one direction or the other so I'm gonna say my enemy is moving left because our little sprite is actually facing his little eye is over there and we'll make him flip if he's going in the other direction so back to my script we have a direction of negative one and so now what I'm gonna do is I'm gonna say in my enemies code you know I could do a couple of if statements I could say if Direction is equal to negative one then velocity dot X is going to be equal to something like negative 50 and then I could say another if like LF and then I could I'll be lazy here copy and paste this right down here and so I'm saying here if or otherwise if direction is equal to one if my character is facing to the right then he's gonna move 50 or with a velocity of 50 on the x-axis so we could do it this way and this would work if I were to save this and then go back to my level 1 and press play scene well now my little enemy is moving to the left and if I were to change his my little enemy's script and say the direction is 1 well if I go back and play my scene now my little enemy is going to the right that would work but there is a more efficient way I'll go ahead and change this initial direction back to negative 1 because my sprite is facing to the left there is a more efficient way of writing these four lines of code into just one line of code so I'm gonna get rid of that instead I'm going to say velocity dot X it's gonna equal to we'll say 50 and then I'm gonna multiply that 50 times our direction and that's why we're using 1 and negative 1 because when you multiply a number by negative 1 it will equal it's negative equivalent okay so it'll become negative 50 or if the direction is 1 it'll just stay at 50 so I'm gonna say x yes an asterisk is x direction okay that's a little kind of a tricky way of flipping the characters speed so if my enemy's Direction is negative one then negative one times 50 is negative 50 and that'll be your side-to-side velocity so I'll go ahead and press play scene and now my enemy is moving to the left because directions negative one and if I make it one well the enemy will move to the right but I'm going to go ahead and change this back to negative one for the initial direction because of course my enemy is facing to the left but there's a problem here if I save my enemy scene and go back to my initial level and let's say I want to have many enemies in my scene so I'm gonna make this copy or this instance of my enemy called enemy 1 and then I'll right-click on it and say duplicate and so now we have an enemy - and if I move enemy 2 over and if I press play scene well now I have two enemies and that's great but you can see if I kept doing this all of my enemies would be in the same direction at the beginning of the game and yes they are stopping because they are physics objects in the air encountering basically a wall my character but what if I want to have enemies facing in different directions like some enemies facing to the left and moving to left at the beginning of my game and some going to the right well what we can do is we can actually make that property the direction property available to us when we select one of the enemy objects one of the enemy nodes over in its properties in its inspector what I'm gonna do is go back to my enemy scene and my enemy script what I can do is I can make this direction variable I can export it and when you put that keyword export before a variable at the top of your code I'll save it I'll go back to my level 1 and go back to my 2-d a workspace if I select either one of my enemies look what happens I now have that because it's exported I have that variable accessible to me up here in the inspector and that means if I select each different enemy I can change that value per enemy and that means some enemies can have a of one and some can have a direction of negative one and that's okay and as the programmer we can then take that and set an initial direction and movement walking direction for each enemy but we again have to program that so just to show you if I press play scene now one of the enemies is moving to the right and one that is moving to the left but of course it's not flipping we'll fix that in just a sec because one of them has a direction of negative one which I changed in this one to one okay let's go back to our enemy scene and I'll go back to my enemies code what we have to handle now is this variable and its initial value based on what we the programmer and the creator of this game set in each enemies Direction value box in the Godot editors interface so if this Direction variable is negative one we want to leave our enemies sprite flipped in the way that it currently is and if the direction is set to one we want to flip that's bright the whole enemy is bright around and because this is not gonna be happening at 60 times per second we're only talking about the initial direction here we'll be using this ready function this ready function again execute whenever you first launch the game in Godot and whatever this asset our enemy and or each copy of the enemy is ready to be in the game well we want to set its orientation correctly so I'm gonna say in the ready function if Direction is equal to one and I'll put my little clone at the end of that line if the direction has been changed to one we need to flip our little enemy sprite over and we can access the enemy sprite by using our little dollar sign so I'll put a shift four on my keyboard dollar sign and I'll write the name of the node which is called animated sprite we want to access this node and change its flip h property so this enemy of sprite has a flip underscore H property we did this with our character as well so you should recall that from that previous video several videos ago and I'm going to set its flip eight property to true and I don't believe that we need to worry about looking at if the direction is set to negative one when we first start the game or when the enemy first is in our game because we don't have to do anything to do with flipping when our game first starts if the direction is negative one because this fight would already be correct so if I go ahead and save this if you need to pause the video to get this code for yourself please pause the video but I'll go back to my level one and I will press play scene and so now our little right facing enemy is facing the right traction and so is the left enemy so we're good the next thing we're gonna do is make our enemy be able to detect if he hits a wall in other words if my enemy gets over to here if I don't get in the way of my enemy moving the enemy shouldn't just stop at the wall and keep trying to push through the wall forever that would be a little bit ridiculous so the enemy should actually detect that wall and turn around if you recall a kinematic body 2d has a method built into it called is on wall and so it can detect if that happens but in order for that to happen I'll go back to my enemies scene and go back to my enemies code we have to tell our enemy which direction is up and down or specifically which direction is up and put that into our moving slide method call so right here I'm going to put a comma because this moving slide has many different parameters that can take including an up Direction I'm gonna say vector 2 dot up and this is a constant that basically defines which way is up and while we're at it we might as well make our velocity update based on how our enemy is colliding with the ground and so I'll put velocity equals move and slide velocity if you're not sure why I put this year I explain that way back when you're programming the movement in the gravity of our character in a previous video so what I need to do now is above these two velocity lines and the order here will matter so don't just type this next bit below everything else put it above your velocity dot y in X lines we're gonna check to see if we are on a wall and we know what a wall now is based on which direction is up and so walls would be 90 degrees perpendicular to the floor direction so I'm gonna say if and then is on wall is a method built into a kinematic body 2d objects this is a method call so I'm gonna put right my round brackets and my colon and I'll press ENTER and if our enemy detects a wall what do we want to have happen well you want to switch this direction so I'm gonna say direction it's gonna be equal to itself times negative 1 and this will work this will actually flip the direction no matter what because if we tell the direction to equal itself times negative 1 well if the direction is currently 1 that means the enemy is moving to the right well 1 times negative 1 is negative 1 and so the direction will then become negative 1 if the direction is already negative 1 if our character or the enemy pardon me is moving to the left well negative 1 times negative 1 a negative times a negative is a positive and so it'll become positive so this is a really good method of keeping your code short and flipping the direction of your enemy by using this negative 1 and 1 to your advantage so go ahead and pause the video if you need to to get this code but I'm going to do a ctrl s to save and I'll go back to my level 1 and I will press play seen and so now if our little enemies detect a wall they should switch in to the other direction but as you can see with this one it it didn't flip around and so I'll go and test that out I'll go to B over to this one and catch up with it and make it turn around yes it does actually change direction but it it actually pumps into the other one and it's not flipping around so I'm gonna go back to my enemies code and I'm gonna say if my character encounters a wall not only should it flip it's direct but we should also toggle no matter if it's true or false this flip H property now I could put a couple of if statements here to say you know if the flip H is true then make it false else if or Elif if the H is the other way then flip it to the other way but there is a sneakier one-line version of that as well and that is I'm going to put a dollar sign and access my animated sprite just like before as well as its dot flip underscore H property and I'm gonna make it equal to not itself so I'm gonna say not dollar sign animated sprite dot flip H and so basically because this flip H is a true or a false if I say make its equal to not what it currently is well because there are only two options and because you know you're just working with true and false it will flip it it's not going to be true then that is essentially logically false and it will make flip h false if it's already false well not false is true yeah I think you get what I'm getting at here so that should flip around the sprite let's go ahead and test that out if you need to pause the video please do but I will do a ctrl s I will go back to my level one scene I'll press play a scene so now if this one bumps into me hey he flips around and if I go over here that one flips around and you know what if they bump into each other they will both change directions if you don't want the enemies to be able to detect other enemies and bump into each other even if you do we should think about our enemies collision layers and collision masks let's go ahead and deal with that we need to deal with that over in our enemies scene so in the enemies scene I'm gonna select the root node and of course we'll go over to collision we have of course collision layers which assign what layer for collisions each object type is on if I click these three little dots I'm gonna make sure my enemies are in the enemy layer and I'll uncheck player because enemies are not players and under a mask we need to think about what layer's or what kinds of other objects are enemies need to be able to detect enemies should definitely be able to detect a player's yes should they detect platforms probably yes should they detect fall zones mmm probably not items no other enemies well that depends if you want enemies to bump into one another if I check that value then yes they will bump into each other if I don't they should not should they detect fireballs yes they should at least when we make fireballs so I think we're good I will click somewhere else and I need to pause the video to get those layers set up please do and of course we're editing this on our original enemy scenes root node not any individual instance of our enemy that will get things confused don't go there okay let's go ahead and do a ctrl s to save I'll go back to my level one I want to test that out I made it so that the enemy would not detect other enemies right I left that unchecked so now hopefully in level one if I go and I make this one bump in to me and change direction if I make this enemy bump into me will they bump into each other no they will not so you can decide that behavior for yourself now I need to bring up a a mistake that I made in a previous video when we set up our collision layers and our collision masks when you're first creating I believe it was our coins I did not set the collision layer and mask up properly for our one-way platforms I know that because if I put my little enemy above my little platform and I press the placing button no it actually does work so our little enemy did fall on this one-way platform and it did just act like normal that's because each enemy if I go back to the enemy scene and go to the enemies root node it is set with a mask to detect the platform but my mistake in a previous video was still there if I select the tiles one way this is my branch for one-way platforms and I go to its child node the static body 2d this is what actually makes your player being able to land on or go through if he's jumping up through the platform this static body 2d physics object I did not set the collision layers and mask properly for so if I click on the first layer three little dots I should make sure that this object is a platform and not a player okay that's important and then it's mask what should a platform be able to detect well platforms detect players and enemies and fireballs platforms do not need to know about fall zones or items or other platforms okay so make sure you fix that if you're following along with me in this whole series so we are doing pretty well here let's talk about cliffs and our enemies falling off the edge of things very likely you will want your enemy to not always fall off the edge of a platform maybe you want some enemies to be able to detect an edge and then be able to turn around if they see an edge coming up and stay on a platform and maybe some enemies you want to be a little bit less intelligent and fall off cliffs we will set that up right now what I'm gonna do is go to my enemy scene we're gonna add a new type of node in our project it is called a ray cast 2d so I'm gonna select my enemies root node this is the enemy scene I'm gonna press + and I'm gonna search for ray cast all one word there is a 3d version and of course we're not gonna use that we're gonna use the blue 2d version so I'm gonna select it and press create a ray cast 2d is essentially like a walking stick if the walking stick is pointing in a direction if it can reach something or and detect if something is in that space well it'll detect that and you can work with that as the programmer this ray cast and Ray cast by default are not actually enabled that's why this arrow is gray if I select this node and go over to the inspector I can turn on enabled that'll make it blue and so now it'll work what we want to do here is put this ray cast judi which basically sends out a little signal again and again and again in basically something that's one pixel why even though this arrow is thicker than that and if anything comes into collision with that Ray Catholic array of light it will detect that so what we're gonna be doing here is putting this ray cast to D I need to drag it from its little origin point here you can't drag it from anywhere on it but except for up here we're gonna put this ray cast to D on either side of the enemy depending on which direction the enemy is moving because if the enemy is moving to the left well we don't need to detect cliffs that it's moving away from on its backside essentially we only have to detect cliffs that it's about to reach near its head so I'm going to dynamically change the position of this ray cast to D from being right about here to being over here depending on which direction our enemy is facing so by default though we're just gonna set its position so I'll go to its transform position and set its x value to zero and I'm gonna change its cast to value because it's going down way too far now we want it to cast down just below the ground because we want to know if the ground is still there and if the enemy keeps going and this rake has to be no longer detects the floor well that means that the character the enemy should turn around so I'm gonna cast to something like 20 I think I'm not sure if this ray cast goes to the end of the arrow here or the base of the air I'm not sure so I think 20 is good value so when the enemy first loads into a game level we're going to use that direction variable to put the raycast to D either right there or right there let's go ahead and do that I'll press ctrl-z to put this back at the origin adds 0 on the x-axis in my enemies script of course I'm going to be working with this ready function to handle things I need to get set up right when the game first loads before we do any coding though I'm gonna change this name of my ray cast to d2 more checker that's a more friendly name and so in my enemies script I'm going to say I'm gonna line my I'll press ENTER and line up my cursor with the beginning of this if so we're not saying this line of code only f Direction is equal to one I'm gonna say dollar sign floor checker and we're gonna make its initial position so I'm gonna say dot position and that's its position property and I'll put dot X to access its left and right position and we're going to assign it to the width of the collision shape to D so I'm going to be putting this arrow either rate employee edge of this collision shape on the left side or on the right side and we can actually look at the value of the width or really the extents value that means the distance from the middle of this collision shape out to the edge of it this is kind of a tricky way of doing things but it makes our code less dependent on hard-coded numbers so again we're gonna be using the width or the extent value left and right of this collision shape for our positioning of our floor checker raycast to D so I'm going to say the position the x position of the floor checker should be equal to the extents of that collision shape to D so I'm gonna use my dollar sign again I'm gonna get my collision shaped to D we can actually access that extents value from our code here so the collision shape to D we're accessing the that object I'll put a dot and now we to access its shape property and now we're gonna access the extents of the shape property and we do that using a get underscore extents method call and it doesn't know that this exists based on our code because our shape is a rectangle but not all shapes have extents so when you type dot get underscore extents it won't prompt you for it okay so all we do now is say dot X but we don't know if we should use the negative version of this value or the positive version of this value at least not by default or it's not gonna be the same all the time but it will be based on our enemy's direction and so we're gonna take this value which might be something like 20 pixels and we're gonna multiply it by our direction okay so basically what this line of code is doing and I'll let you pause the video now if you need it it's a bit longer than my screen maybe I'll zoom out once or you can get it all in your view right there what we are doing visually essentially when our game is loading I'll go back to my anime scene here is when the game first loads we are putting base our enemy's direction the raycast 2d our floor checker at essentially positive twenty or negative twenty which is based on the extents of this shape object which if I actually go and select my collision shape and look at its rectangle shape well I was pretty close it was twenty point six eight two but we're actually accessing that value in our code and we're saying hey make it equal to positive 20 point six eight two or negative twenty point six eight two because they're the same on both sides so I'll go back to my enemies code in case you need it still but we're actually gonna take this line of code that we just typed and copy it so I'll press ctrl C after I select it and we're gonna paste it somewhere else in this code and the reason why we're gonna do that is because if our enemy detects a cliff and it turns around well we need the raycast 2d our floor checker to jump from here over to here and whenever it changes direction essentially it's gonna do that so it'll be jumping back and forth the reason why we're doing it this way and not using two different raycast to T's is because these raycast objects are actually quite intensive for godot to run in your game and having too many of them will actually slow down your game so if we can have one instead of two and therefore half all the ray casts in our scene that's a good thing okay so I'll press ctrl Z to put that back where it was and I go back to my enemies code so I copied this line right here and I'll press control C we're gonna say if our player is on the wall we want to switch our direction of course we want to flip as well our sprite around but also we're going to flip that a floor checker over to the other side when we flip our direction around so I'll paste ctrl V that code right there okay if you need to pause the video please do but I'll do a ctrl s to save and I'll go back to my level 1 2d workspace let's go and see what happens when I press play of course my enemy should full and I'm gonna pay a special attention to this top enemy right here what is it gonna do it's gonna fall off the edge because we haven't programmed our enemy to if it no longer or that raycast no longer to text a floor we have to say if that is no longer detecting to a turn the enemy around so back to our enemies scene and back to our enemies code we want to a flip the enemies direction and flip the enemies sprite and flip the position of the floor checker if the floor checker is no longer detecting something that it should be colliding with in other words a platform now our floor checker if I actually select it has a collision mask we have to tell it what kinds of objects on what layers it should be looking for in this case I'll press the three little dots it should only be looking for platforms that's all that cares about I'll just check that and click somewhere else but there is a method and this is the crux of all this ray casting there is a method called is colliding and it's something that you can call on a ray cast to check to see if it is still colliding with something or attacking something in that area so we're gonna use that right now I'll go back to my enemy script we're gonna say if the raycast 2d called floor checker is no longer colliding with something we're gonna make it do all of these three lines of code so I'm gonna use an aura here because if the enemy detects a wall or if the floor checker is no longer detecting a floor than flip around so I'm gonna say or and we're actually gonna say not because work I can see if the floor checker is no longer colliding with something so if is on wall or not and now I'll access our floor checker dollar-sign floor checker dot and this is the method is underscore colliding and so I'll press enter right there to complete that line because of course this this raycast is sixty times per second checking to see if it's colliding with something and if the enemy is on a floor then the floor checker is colliding with something but as soon as it's not colliding with something that means there's a cliff rate where the enemy is heading and so we should flip our enemy around and that's what we now have so if the enemy detects a wall or a cliff it should flip around let's go ahead and see if that works I will do a ctrl s to save I will press with level one selected play seen and now we're watching that top enemy which just turned around properly as it detected a cliff let's watch it on the other side and hey it worked let's go and watch this other enemy I will speed this part of the video up and our enemy had detected the cliff properly and that's great but we have a problem now that we have this floor checker our enemy is going to change directions if it no longer detects a floor under that raycast but what happens when the enemy is in midair like if it's falling at the beginning of our game like right now when I press the play scene button it's gonna fall down or what happens if we turn that off if we don't have that behavior for you want a dumb enemy that falls off the edge let's go ahead and watch this enemy as it falls at the beginning of the game I'll press play scene did you see it I'll do it one more time I'll stop and press play scene watch as it falls down the enemy was going back and forth and back and forth as it was falling down and it was doing that because this raycast didn't detect the floor and so it used that code that we just typed to go back and look at that code it saw that we were not colliding with something and so it flipped the direction and it kept doing that 60 times per second so we're gonna add an end in here I'm gonna say if our enemy is on a wall or not checking or not plotting with something that is with the floor raycast 2d and we want to make sure we're on the ground in order for that part of it to be true so I'm gonna say and is on floor and so these two because we have this and right here that will kind of combine it in a condition or a boolean expression with the not floor checker as colliding so these two things have to be true in order for it all to kind of work together basically now we have two conditions either the enemy is hitting a wall or they have to be on the floor and the floor checker is not climbing those two things work together now okay it's a little bit logically confusing but I think you can probably see what we're getting at here so I'm gonna do a ctrl s to save I'll go back to level one and press play scene now when that enemy Falls it didn't go back and forth all do that one more time so you can see but that was the case okay but we want some enemies to stay on their platforms and detect cliffs and turn around when they see a cliff and we want some enemies to be a little bit less smart and just fall off the edge of cliffs and we want to be able to decide this on a per enemy basis so what we're gonna be doing here is adding one more variable that's gonna be an exported variable that's up here and we're gonna call it detects cliffs and it's gonna be an on or off switch so let's go ahead and add that now to our enemies code I'll go back to my enemy and go back to my enemies script we're gonna create a variable called so I'll put far and b for VAR i will put export to make sure it shows up in the inspector we're gonna call it detects cliffs and I'm gonna make it equal to true by default and so when you have a variable like this with a true or false value it is called a boolean variable or that's the data type boolean and when you have one of these variables and it's exported and you go and look at the inspector for that kind of an object you have an on/off switch now detects cliffs and it writes in lice lahir even though I have it with no capitals and an underscore it put in my scene right here in the inspector it with capitals and with a nice space so that's kind of nice so for each of our enemies we can decide to uncheck or leave checked this detects cliffs and if we uncheck it the enemy will be dumb and it'll fall off the edge of cliffs in order for that to work if we uncheck this we want to disable that floor checker raycast 2d so in our code what I'm gonna say is when of course we're gonna use this function ready here we're gonna say and I'll go down to my next line here we're going to access the floor checker so dollar sign floor checker and I'm going to turn off its enabled property in fact I'll go back to my enemy scene to show you if I go to my enemy scene and go to the floor checker it has an enabled property which we turned on at the beginning of this video so when this enemy first loads I'm gonna say enabled it's gonna be equal to and then we're just gonna say and make it equal to two text cliffs because it'll be the same true or false value so I'm gonna say detects cliffs just like that so this will be either true or false and we'll be assigning it to that same on or off so now that we have essentially disabled our floor checker by making this detects cliffs false if we do that on one of our enemies so in fact I'm gonna try that out if I go back to my level 1 scene and actually view my scene in the 2d workspace and go to let's say enemy 2 which is this one right here and I turn off detects cliffs let's go and see what happens when I press play scene well the enemy just falls down and because it's basically looking for that information from that raycast 2d it's not finding it it just gonna be always flipping around back and forth 60 times per second because again because of this if statement were saying if is on wall or not floor checker is colliding this is no longer working the way that we want we're gonna use an and right here so I'm gonna put an extra space right there and make sure you keep these in the same order that I have them I think that that's important we're gonna put an extra hand here and I'm gonna say if detects cliffs is true which we can just write by saying detects clips because that is a true or false value so I'm gonna zoom out once or twice on my code so I can get all of that hopefully in one line on my screen so now this is colliding will only really play affect on whether or not our character should change direction and flip around based on if this the text cliffs is true or false it's a little bit logically tricky because now we have so many ORS and ends and not in there as well I believe this should work if you need to pause the video go ahead and do that but I will do a ctrl s to save I'm in my level 1 I'll press play scene and so now our little enemy falls down it's not confused but it should also now fall off the edge of the cliff because we have on that enemy too we have to text cliffs turn off if I turn it on now for this enemy I press play scene and I go back and look at it let's watch it I'll see what it does yet I detected the cliff and so it would not fall off the edge if I go back to enemy 1 and put him right about there and he has the text cliffs turned on and I press play seen what happens well he had to take that edge because uh we put him right there and he would have fallen off otherwise I'll go ahead and put him right there and look at this scene again let's go back and watch and see what he does so now they're both detecting of the cliffs because they both have detects cliffs turned on if I turn them off well they should both fall off the edge let's go ahead and put them both right there and now let's go see what they do that one not landed on my head and that's fine we'll do you want that in the next video and that one fell off the edge as well so I think it's working pretty well what we do not have yet though of course is the ability to get hurt by enemies or jump on and squash enemies and do a little bounce when we land on an enemy we will be covering those things as well as disabling enemies when they go off the edge of the screen so we don't have to run their code when they're somewhere else in our level we'll deal with all that in the next video but for the end of this video I will simply go back to my enemies scene and go back to my enemies script and I'll try to put it up on the screen so you can see it but that will be it for this video of course if you like this video or flipping it please go ahead and click on that like bumbles video it really helps out me in my channel and if you wanna see more videos like this one in the cadeau game engine or in blender or other technology click on that subscribe button as well and click the bell icon to be notified whenever I upload a new tutorial check out my facebook page and my Instagram page in those two places I post sneak peeks and previews of what I'm working on next is where I communicate with you guys the most but that'll be it for this video thanks for watching and we'll see you in the next one bye bye [Music] you you
Info
Channel: BornCG
Views: 30,397
Rating: undefined out of 5
Keywords: tutorial, lesson, beginner, godot, game, engine, godotengine, platformer, code, gdscript
Id: ROYG4widsXc
Channel Id: undefined
Length: 52min 43sec (3163 seconds)
Published: Sun Jul 05 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.