Godot 3.2: Let's Build a 2D Platformer!: Part 14 (Enemy Collisions, Getting Hurt, Enemy Colors!)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Thank you for your work!

👍︎︎ 1 👤︎︎ u/VRbandwagon 📅︎︎ Jul 15 2020 🗫︎ replies

vibin to this series

👍︎︎ 1 👤︎︎ u/OkQuote5 📅︎︎ Jul 16 2020 🗫︎ replies

Thank you !

👍︎︎ 1 👤︎︎ u/iustinn 📅︎︎ Aug 11 2020 🗫︎ replies
Captions
hello and welcome part 14 of let's create a 2d platformer in the godot 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 screen using keys on your keyboard of course you can walk run jump and fall you collect coins to win the game you can get hurt by enemies and lose lives you can also squash enemies find keys to unlock doors do wall jumps shoot fireballs you know how 2d platformer games work so in this tutorial in part 14 we're gonna be handling enemy collisions that means when your player comes into contact with the enemy if you're hitting the enemy from the side you will get hurt you will turn red and bounce in one direction or the other and if you jump on the enemy from the top well that's how you squash the enemy the enemy will change appearance to look squashed and then you will do a little bounce off of the enemy automatically and the enemy will disappear after a moment so we'll be handling those things in this video of course this is part 14 of this mini-series if you have not seen the first 13 parts of this mini series and how to create a 2d platformer game in the gedow game engine i'll put a link to this whole playlist containing all of my goodo3 tutorials up on the screen right now but let's go ahead and jump into the godot editor of course if you like this video or learn something in it please go ahead and click on that like button below this video it really helps out me and my channel if you want to see 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 upload a new tutorial so as it stands on our project right now of course we have our little character i've added three little blob characters into my scene and some of these blob enemy characters if i select each one of them you'll see that i might have customized each one a little bit in terms of its direction so if direction is set to one it'll go to the right if the direction is set to negative one it will go to the left so they can all be a little bit different or have a different start direction likewise i can turn detects cliffs on and off if you've not seen my last video in this mini series part 13 in which we started creating the enemy as well as the enemy behaviors i'll put a link to that tutorial up on the screen right now you can check that out by clicking the card up in the top right corner most likely so in this video we're gonna handle enemy collisions because right now if i press the play scene button well of course my game will launch and if i get in the way of my enemy well the enemy is smart enough to know to turn in the opposite direction if it hits a wall but that's all it considers my player right now is just a wall there are no enemy collisions in terms of getting hurt or squashing the enemy so these enemies don't do a whole lot except for of course change direction on a cliff or a wall the cliff if that option is set up so let's go ahead and jump into our enemy scene i have three enemies i can click on any one of these little icons to jump into and open up the enemies original scene what we're going to be doing here is we're going to be adding some area 2ds to either side of the enemy which will do the job of hurting you the player because when you hit the enemy from the side of course you get hurt as the player and then we're gonna add an area to the above the enemy uh and that will determine if the enemy should get squashed if you are jumping on and squashing the enemy so i'm gonna select my root note of the enemy scene and i'll press plus i'm going to do a search for area 2d and if i search up there for that text of course i'll find it right here i'll double click you might also have area 2d in your recents as well so i will double click this is going to be my top checker because this is going to detect if you are jumping on and squashing the enemy so this area 2d i'm going to double click on its name and i'm going to call it top underscore checker and i'll press enter and of course it gives us a little error because area 2ds need a shape in this case we'll be using of course a collision shape so i'll press ok i'll select my top checker i'll press plus i'll search for collision and i'll choose collision shape 2d the blue one not the pink one of course so i'll double click this collision shape i'm going to go over with it selected to the inspector where i cannot choose a shape so i'll click a little arrow and i will say a rectangle shape 2d and then i get this new square here i'm going to drag it up and as soon as i start dragging i will hold ctrl on my keyboard or shift i don't think it really matters control or shift will make it kind of go up and down without moving side to side once you start moving it i'm going to make this a little bit shorter and i'm going to drag it up and hold shift again i'm not going to have these two parts overlap but i am going to make this area 2d a little bit narrower than the actual shape of the kinematic body physics shape so this main area is the collision shape of the physics object and to it we're just basically adding little zones that are going to detect the player and this is going to be the one for squash so i'll make it a little bit narrower like that i'm happy with its size by the way at the end of the last video i didn't have my enemy animation turned on so right now you can see that this enemy is squashing up and down if i select the enemies bright i just forgot in the last video to turn playing on okay i just turned it on and that means that all the enemies will be doing their squash or their crawl animation which i have selected here and that crawl animation only has two frames and it's going at three frames per second so we have our top checker what i'm going to do now is use a signal to detect if a object is colliding with this top checker before i do that though i want to make sure the collision layers and masks are set up properly so this top checker i'm going to go with it selected over to the inspector to the collision section we have to set up our collision layers and masks of course this top checker is not a player so i'll uncheck that it is an enemy a category of object okay it's part of the enemy and under collision masks this is what this object can collide with of course we want to collide with players that's what we want and maybe fireballs we'll see about that later when we make fireballs in a future video okay so i'm kind of happy with that platforms no fall zones no items enemies no just players and fireballs let's go ahead now and create a signal on this top checker so i'll select the top checker go over to my node tab go to the signals kind of section of that we're going to check to see if a body enters this area 2d so i'm going to double click on body entered and not body exited or something else we want to use body entered i'll double click and i will select the enemy because we're going to put the code that this signal is going to call to run the function that we're going to be creating is going to be called on top checker body entered it's a long name but it's pretty self-explanatory if you read it out and think about it we want that code to be on the enemy itself so i'll click on enemy and press connect when i do that of course i'll zoom in on my code i get a function that is pointed to you can see this little icon here it's pointed to by the signal and this is what we want to happen if the character enters this top checker area2d what do we want to happen well we want the animated sprite of the enemy to switch to its uh squashed animation if i select it and go over to the sprite frames dock at the bottom we have a crawl animation which is its little walk we have a squash animation it's just got one frame it'll make it look like it's squashed so i'm gonna go back to my enemies code and i'm gonna say well i have to access the animated sprite so i'll type animate sprite there we go it knows what we're talking about here it knows what the child nodes are and i'm going to say dot play which is a method call so you have to put little round brackets and i'm going to put in quotes uh squash and i'm not sure why it didn't predict that for me but i know what it was called if you need to double check you can go back to the animated sprite and look for its name right there squashed all lowercase that makes it easy okay so now if i do a control s to save go back to my level 1 i will go back to my 2d workspace and i will press play because by the way we edited the code of our enemy which exists primarily inside of the original enemy scene this will now affect all of the enemies which is great it propagates out to all the instances enemies one two and three so great i'll press uh play scene and now if i jump on an enemy bam the enemy gets squashed but we have a problem it is still gonna be a physics body that means i can land on it and uh it's still moving and it's not disappearing so what i want to do here is i want to have the enemy stop and that means i need to change its speed and set at speed to zero so let's go ahead and handle that i'm gonna go back to my enemy scene and my enemies script we have our enemy always moving at a velocity of 50 or negative 50 depending on its direction and this is happening at 60 times per second because this code is in our physics process delta our game loop this is a hard-coded number we want to try to get away from those generally so i'm going to put a variable at the top of my code i'm going to type var speed i'm going to make speed equal to what we want 50 and now i'm going to use that variable speed in place of this literal hard-coded number here speed therefore it'll act the same but now we can change the value of this variable from somewhere else and that's exactly what i want so when my character enters the top body checker or the top checker i want to change the speed of this whole script file so speed is going to be equal to zero i'm going to stop the enemy in its tracks okay so if i press ctrl s to save and i go back to my level one and i press play scene well hopefully now when i squash the enemy it'll stop okay but notice how the other enemies keep going because i haven't squashed each one of them so now if i squash this one well there we go the next problem is is that we want the enemy to no longer be collidable with our player so let's think about this for a second if i squash an enemy of course now it uh stops and that's great but i can still land on it and that's not great so what i want to do here is i want to access each or the enemy that i squashed and if i select one of them in my level 1 and i go to the inspector well it has collision layers and masks set up what i want to do here is well i want to probably turn off because my players collision layers are set up and its collision masks are set up to see enemies in the scene on the enemy that i squashed i probably want to remove the enemy from its own layer so the player can't see that enemy anymore and i probably want to change the collision mask because i don't want the enemy to be able to see the player anymore so i want to turn off essentially that one and that one but don't do what i just did don't turn off and don't modify these layers and masks on the individual instances of the enemy don't do that you'll get yourself into a world of problems so what i'm going to do here is i'll go into my enemy scenes script of the root node and what i'm going to do is i'm going to when our player enters this top checker area 2d down here i'll actually go down here and i'll press enter a few times so this code isn't so low down on my screen and i'll make it bigger what i want to do is i want to play with and turn off those two layers the collision layer and the collision mask so we are right now working on the enemies code that means it'll affect the entire enemy instance that we are working on so i'm going to say set underscore collision and i'm going to say set collision layer bit and this is a method call and i'm going to turn a one of those squares one of those bit values to false which one well if i select the root node and i go over to this layer if i hover over the one that's on for enemy remember i want to make this enemy no longer an enemy or no longer in its enemy layer it is bit four okay because it starts counting from zero one two three and four so that's bit four and so i'm going to turn that to false so i will say 4 comma false i also want to remove this enemy's ability to see the player and if i go back to my enemy and go back to its masks i want to turn off this mask bit which is the player and it is bit zero okay so i'm gonna go to my enemies code and i'm going to do almost the same thing we've seen it before set collision mask bit and so that's what it looks like and we're going to be turning off bit 0 and because we're turning it off we will set it to false and i'll just confirm that it is bit 0 and because that is the player layer that is looking for okay we also have children of the enemy in other words this top checker that has its own collision layers and its collision mask so on this top checker i'm going to disable it as well by turning off its same layer bit for to false and mask bit 0 to false as well so on my enemy what i'm going to do is again when my player enters that top zone i'm going to say dollar sign and i'm going to say top checker dot set i can actually copy and paste this ctrl c and i'll paste that there instead so i'm turning off the ability for it to be in its own layer and so i will do the same thing again dollar sign top checker dot and i'll copy this because we're gonna turn off its collision mask so it doesn't see the player anymore and i think that should work let's go ahead and try that out if you need to pause the video please do get the code out for yourself i will do a ctrl s to save but i will go back to level one it's not saved i'll ctrl s and i'll press play scene with level 1 selected and so now if i go and land on an enemy you see that it no longer bumps into my player now my steve character i did this between the last video and this video my steve object is the last thing in my level one node tree in the main branch i believe as of the last video this steve was actually up here it was just below the parallax background and if that's the case for you if your enemies are lower down on this list than your steve well they will be in front and that might not be what you want so if i go into the same thing now if i walk around you can see that i go behind the enemy the enemy is on top well i can just fix that by dragging steve down there and i don't want steve to be in the enemies folder so i'll just do a little bit of reorganization here and there we go so now it works the way that we want and can i squash other ones too it's going to be hard for me to squash that upper one because it's on a platform and i'll jump just jump through the top if i try jumping from here in other words it just jumps up so it's hard for me to squash enemies that are on there i hope you understand that so i think we are doing good we are squashing the enemies the enemies are no longer going to collide with us when we squash them but what about the enemies hurting us well let's go back to the enemy scene and i'll select the root note because to it i'm going to add another area 2d so i'll select the root node i'll press plus i'll search for area area 2d i'll double click to add that the area 2d or we're going to name sides checker and i think that makes sense we're going to do both sides at the same time of course it needs a collision shape so with it selected i'll press plus i will search for collision and i'm going to select an add a collision shape so i'll double click on that this collision shape is going to go through this area and it's not going to be quite as tall as the actual physics body just because we don't want things to kind of get too close to one another so the collision shape i've got it selected for my size checker i will add a new rectangle shape 2d resource to it i'll move it down a little bit i'll hold shift or control as i do that i'll make this a bit wider i'll make this a little bit taller i think we're doing pretty good here let's drag it down a little bit and hold ctrl and maybe i'll make it a little bit shorter now if you have this too narrow like that it will it might not work properly and in fact by the time you your character is moving if it's moving fast enough your character it might actually collide with um the size checker but it also might hit the top checker so you might want to make this a little bit wider than you think you need and you might want to edit the top checkers collision shape to make it a little bit narrower okay but if you make it so narrow and then you jump on the side of your top of your enemy it might not detect you jumping on it so you kind of have to play a balancing game here so this side checker we need to set up its collision layers and masks it is a not a player it is going to be an enemy and its mask is going to be looking for players and fireballs i believe i think that's what we have on the top checker as well so yes that's what we want now we might in a moment run into a problem because right now our sides checker thinks of itself as an enemy and i believe if two things are on the same collision layer even if their masks aren't set up to do so i believe they might detect each other so because this area 2d is considered on the on the enemy layer and because our enemy itself in other words the main physics body collision shape is also on that same enemy layer the area 2d might always detect itself entering it and that might cause a problem so maybe we might have to deal with that in a moment but for now uh let's if you need to get that set of layers and masks there it is but i'm going to select my science checker now and we'll set up the signal so with it selected i'll go over to my node dock and i'll go to under signals we want to detect a body entering this area so i'll double click on body entered and it will make a new function called on sides checker body entered that's a long name but again it makes sense i'm going to select the enemy node because that's where our code is and i'll press connect it makes a new function at the bottom i don't need so many lines there so i'll get rid of that it's called again on sides checker body entered what i'm going to do here is i'm going to do a print out to my output section to see what happens uh or see how often this is going to happen so i'm going to do a print method call and this again we'll just write whatever text we want down here into the output this print method call is a good way of checking to see when a function is running or when certain code is running you can put this pretty much anywhere in your code and it should do its job it should print at some point uh depending on where you put it of course so print and again it is a method call so it needs round brackets and in here i'm just gonna say uh ouch because that's what happens when uh you hit an enemy from a side it hurts so let's go ahead and try that out i will do a control s to save if you need to pause the video please note of course that this function is being pointed to by a signal that's what these two little icons mean and if i select the size checker we can see that the body entered is pointing to that same thing with the top checker again that's pointing to that good okay let's go back to level one let's press play scene and it seems to be working okay if i run to the side of the enemy now look what happens it printed out there okay didn't do anything else but it did print out so if i go and hit the other side of the enemy see it said ouch and if i run into the side of this enemy it prints out and it seems to be working okay so i don't know yet if i have to play with those collision layers and the collision masks i think it seems to be working okay for now so what do we want to have happen if you touch the side of the enemy as the player well the easy thing to do it would be just to go to a game over screen or in our case we're not going to switch to a game of our screen because we don't have one yet but you could just reset the level back to the beginning and that's what we'll do right now so i will select my enemies scene and i will select or go into my enemies code we are not just going to print out in fact before we publish our game we'll get rid of any of these print method calls because those don't really work when you publish your game they're just for the editor but we will change our senior do you recall how to change the scene in godot to reset the scene or switch to a different scene in your game it is get underscore tree that is a method call so you need round brackets or parentheses if you're being correct about what you're calling them and when you put get tree bracket bracket you put a dot and you're going to say change oops change spelt correctly underscore scene the first one and then you're going to point to the path including res colon slash the name of the scene that you want to switch to in this case we're switching to level one if you don't get this prompt like i did you can just select the scene over in your file system doc in the words level1.tsen and you'll get the path right there you can just copy that ctrl c after you select it and put it in quotes over here double quotes of course and paste and then it should work so let's do a control s to save this if you need to pause the video please do get the code let's go ahead and go back to level one and press play scene and now as you can see hopefully when my enemy hits me well i switch scenes and if i run to the side of the enemy either from the front or the back it should work now some of you who are a little bit more experienced in godot might know that there are a ways that you can actually have a kinematic body object like the enemy so it is a kinematic body 2d object you don't have to have area 2ds in order for it to detect other areas or detect collisions right now we're kind of coding all of this enemy as if enemy objects in other words came back body 2d objects don't have the capability to detect collisions built in if i select the root note here and i go to the node signals dock you can see there is no body entered or area entered objects here but there is a way to check for collisions on a kinematic body 2d i find they don't work as well and i find that if the enemy is moving in the direction that it will hit you then the collision would work if you were to use a kinematic body's collision detection that it has but if you as the player accidentally run into the back of the enemy it doesn't work so well at least in terms of what i've found so i find that just using area 2ds that are built into your enemy object that are following along with the enemy like we have i just find this way works better okay so we are getting there we have a few more things to add if i go back to level one and i play my scene and i jump on my enemy well it's not working exactly the way that i want because uh if i land on my enemy well i as the player am falling down through the enemy because when i jump on the enemy and i enter that top zone you know we're disabling some of the things in the enemy but i'm actually falling into that side zone area too so what i want to do is go back to my enemies code and when i enter the enemy's top checker i'm turning off its collision for my player okay i'm turning off the top checkers collision for my player but because we now have another child of the enemy scene we need to turn that off as well so i'm just going to copy these two lines and i'll press enter because when we enter the top zone we want to turn off the side checker with size checker as well so i'm going to change this to a sides checker and sides checker and that should do it so if i do a ctrl s to say of course if you need to pause the video please do but i will go back to my level one and i will press play scene and now if i land on an enemy well i just fall through and that just works the way we want does sides collision still work yes science collisions uh do still work okay so that seems to work pretty well but when i squash my enemy two things should happen that are not currently happening uh number one is i should be able to bounce off of my enemy so let's go ahead and handle that in my enemy i'm going to select the root note i'm going to press a plus because we're going to add a new type of node into our scene a timer a timer node is used to do a countdown or a repeated kind of tick tick tick it can count or send out a signal on a timed increment or you can just use it as like a stop watch or a countdown timer that will sound you can have it emit a signal whenever it runs out of its time so i'm going to add a timer to my enemy we'll just leave it called timer with a capital t and when my timer runs out i want the enemy scene the whole enemy instance of the main enemy scene to delete itself now before we get to that though let's go ahead and select the timer node and go to the inspector and look at what properties it has it has a idle mode and a physics mode we're not going to mess with that we'll leave it on idle we have a wait time and this is how long the timer will count down for if you play with this number you can set how many seconds it's in seconds it will count down for i will just leave it at one if you only want a timer to run once and then once it runs out once it counts down to zero at that point if you want the timer to stop then you will turn one shot on if you don't turn one shot on it will keep looping so it will keep counting for one second at a time and then it'll just reset and automatically count again and again and again in a loop okay auto start is pretty self-explanatory we don't want that because we want the timer to only be triggered to start when our player jumps on the enemy so i've got one shot and wait time one second what i want to do here is on my enemies code when the top checker body is entered when the player jumps on the on the player i want to start this timer and then when the timer runs out after one second i want the enemy to delete itself so i'm gonna do this in two parts i'm going to start the timer when the top checker is entered by the character so i'm going to put a dollar sign because i'm going to access the timer so dollar sign timer with a capital t it's same name and i'll put dot and i'm going to put start now this start is a method call so you need to put round brackets or parentheses and if you want to specify a time in seconds you can do that i could put one here but i actually don't need to do that because the time that i'd be entering is actually the time that we set in its properties its wait time is here so i can just put start without any parameters in those round brackets i believe so the timer has started how do you make the timer do something when the timer runs out well that's a signal and so it's going to have another function that it calls when that signal is up so i'm going to select my timer and i'm going to go to its node signals and i'm going to say when it times out it has this signal built into it i'm going to double click on that and it's going to put a function on my enemy's code called on timer timeout and so it'll run this function when the timer counts down to zero so i'll just press connect after i select enemy and of course it'll make a new function on my enemies code at the bottom we're going to say when the timer times out we want the enemy to delete itself which is a call to queue free q free is the code it's a method call so you need your parentheses around brackets that should work okay so if your timer runs out the enemy will delete itself and that is triggered to start when your top checker is entered right there i think we're good i think you probably have it if you need to pause the video and catch up please do but i'll do a ctrl s to save i'll go back to level one i'll press play scene so now if i jump on my enemy the enemy disappears after a second if you don't like that time you can go back to your enemy scene select the timer go to your inspector and turn this down or up i think 0.5 is a nicer amount of time it's quicker the enemy will disappear pretty quick so if i go back to level 1 and i go back to play scene and i jump on the enemy well it'll disappear pretty quick you can decide on your own wait time is the scene restarting itself i'm not sure why that is but we will keep going and i will solve that actually let's go and see if we turn off v i'll go back to my enemy scene that might be happening because something is entering the sides checker of our enemy which triggers when you enter it prints out the screen which it did something entered the side of an enemy and it wasn't the enemy that i just squashed so it was changing the scene that's the only thing that changes the scene right now except for collecting all three coins which we obviously didn't do so what i'm gonna do is i'm gonna go to my side checker area 2d and it is on the layer the collision layer of the enemies i don't think we actually need that if we turn it off and i do a control s to save this size checker will still be found or it'll still be looking for the player so it'll still be able to collide and detect collisions with the player the player can no longer find it necessarily in its code because the enemy's sides checker is not on a layer collision layer itself but i think that'll solve any problems because i think perhaps that this side's checker was maybe colliding with the ground or something like that let's go and see what this does i'll save this uh enemy scene remember that we're working inside the enemy scene when we turn that off if i go back to level one though and press play scene well if i squash an enemy it disappears let's wait a moment and see if our scene resets i have a feeling that it was to do with this corner here and the enemy falling off and the size checker kind of colliding with that little zone there but i think it's good now i don't think we're having that problem anymore so i think that was it and i think i confirmed that when i practiced uh this tutorial last but not least actually have a few more things to do we want to make our player do a little bounce when we land on the enemy we should do a little hop so let's handle that back into my enemy i will go into my enemies code when the top checker is entered by my player i should call a function on my player to tell my players do a little bounce so we have access to whatever body enters the top checker of the enemy and so if i put that body code if i put that body reference right here i can call a function on the body by putting a dot and then if that body has a method called or a function called balance which we're about to make it will run the bounce method of course i can access any property of the body by putting a dot and then putting the property name in this case we're calling the function bounce which we don't have yet so if i were to save the enemy scene if you need to pause the video please do and go back to level one we would actually cause our game to crash so now if i jump on the enemy well again the program will crash because it says invalid call non-existent function bounce in base kinematic body 2d steve dot gd so it didn't find a bounce method or function in steve's code so let's go ahead and add that i'll close this window i'll press the stop button that's important to do and then i will go to the code of steve and add the bounce function so i'll go to steve's script here when you edit the script of this instance of steve it'll just edit the steve.gd original code file so that's what we want that's good so i have my physics process delta function here which is my main game loop on steve if i scroll down we have one other function it is the fall zone checker so if our player enters the fall zone well we are resetting the game but we want to make our own custom bounce function down here as well below either of those two functions so i'll go to the end of this line i'll press enter a few times and backspace so i'm lining up my code or my cursor with the beginning of the line same as the func above it and i will type func because we're making our own custom bounce b-o-u-n-c-e uh function here when you write a function you always need round brackets even if you don't use them and then you need a colon and then whatever you put below that line that is indented it will be a part of that function it'll run it'll be the body of that function's code essentially in this case if i land on the top of enemy i want my player to do a little jump automatically without me pressing any key on my keyboard how do you jump well you're going to be affecting your player's y velocity in other words if you do a jump if you press the jump action button and you're on the floor you make your velocity dot y equal to your player's jump force and jump force here is a constant it is negative eleven hundred now if i do a bounce by jumping on an enemy at the top of an enemy i don't wanna do a full force jump force jump in other words i don't wanna go quite as high up but what i will do is i will copy that line ctrl c i'll paste it here indented of course tabbed once in but i will divide or multiply jump force by a number that's less than one so i will type 0.7 or something like that so the jump won't be quite as high it'll be 70 percent of its normal height so if i do a control s to save i think i should have it i will do a control s and play my level 1 scene again so now if i jump on the enemy hey i do a little hop is that the right height of hop i'm not entirely sure and i can adjust that 0.7 to something else if i don't like it but i think 0.7 is okay for me likewise if you hit an enemy right now i'll go back to my level 1 scene and i'll press play scene it'll hit the side of an enemy i don't just want to reset my scene necessarily this is a little bit optional if you run into the side of an enemy i'm going to make my player turn in a translucent red and my player is also going to jump in the direction based on i think where you are in relation to the enemy so if you are to the left of the enemy you should jump to the left if you're to the right of the enemy hitting enemy you should jump back and up to the right so in other words if i go to my enemies script if you enter the side checker where is that if i zoom out on my code top checker size checker body entered right now we are printing out the screen i don't mind that um we are changing our scene back to level one i'm gonna get rid of that and instead on my body this is the body that enters the sides checker i'm going to call a method core function called not bounce this time we're going to make one called out it's a good name i can't think of a better one but there we go so now that should work assuming that we actually make that ouch function on our steve character so i'll do a ctrl s to save my enemy code i'll go back to steve's code we're going to make a function called bounce again lined up here funk uh ouch not bounce ouch and ouch is going to need of course um some body code so i'll press enter and make sure i'm indented once like before when i am hit when my steve character i'm editing steve's code right now when i'm hit by an enemy on the side i want to change my color in other words i want to make steve look a little bit more red and a little bit more transparent we can do that using steve's i'll go back to my level one and actually go to steve's scene steve is right here i'll click on his little scene icon if i look at steve's root node i can edit the visibility modulate setting in code and this is actually pretty easy to do right now if i just do this in the interface if i click on this little color picker and pick a kind of a ready color and then maybe turn down this a slider which means red green blue a is for alpha you can make him transparent so i'll turn these all back up to 255 that means they're all their natural color of that sprite so i'll go back to steve's script back to my script workspace what i can do is set underscore modulate and this is a method call so it needs parentheses or round brackets and in here we have to tell the set modulate what this color should be now if you want to describe a color in code it requires that you make a new color uh variable essentially so we put color with a capital c and if you are british or canadian you are spelling color with no u you're spelling it the american way with a capital c and i'm gonna put some parentheses here because color needs an rgb and a red green and blue value as well as optionally an alpha value so rgb and a so these all go in these round brackets with commas between them so if i just put 1 comma 1 comma 1 comma 1 that would not change the color at all because this just all means white in here when you have all these rgb turned up to 1 that's their fullest value when you make a new color object it just means all the colors are there which means we're not going to be affecting the modulation instead what i'm going to do is i'm going to leave red turned up but i'm going to take away some of the green and the blue so i'm going to turn these down to 0.3 and 0.3 when you leave the red turned up and when you turn down green and blue it'll look more red and if i turn this alpha value down to 0.3 or 0.4 you can choose you will be making the sprite or the entire steve character look more transparent so if i do a control s to save and if you need to pause the video please do but if i go to level one and i press play scene well if i run into the side of my enemy i will turn transparent red next we need our steve to do a little bounce and do a bounce backwards so i'm going to make my steve do a jump up and then we'll decide how to make it go side to side so on my steve's script i'm going to make steve do a little bounce and we'll use the same code here i'll do a ctrl c to copy i'll paste that indented of course right there i might want the bounce to be a little bit less dramatic so maybe half of the normal jump force bounce height and so if i do a control s to save and i go back to level one i will go and play my scene so now if i hit the side i uh well i jump up but then of course when i land i enter that top zone so that's not what i want so now we have a problem because we have this steve's ouch function that knows how to edit properties and see properties of steve because this code and this function is on steve but now i want to say you know if steve is to the left of the blob enemy i'll go back to my level one if steve is to the left of the blob and it hits that side i want steve to jump to the left and up of course and if he's over here to the right of the blob i want him to jump that way or get thrown kind of that way right now though inside of steve's code it has no idea what object hit it or therefore where that object is in relation to steve's coordinates so really steve needs to know the coordinate of the enemy or at least the x-coordinate of the enemy that hit it in order for it to jump in the right direction so i'm gonna go back to my code i'll go back to my script workspace and go back to my enemy right now when my enemy detects or its sides checker detects that my character entered it it calls this body dot ouch function but it does not send this body.ouch function any information in other words a parameter in the round brackets i'm going to fix that now i'm actually going to send steve through the parameters of the ouch function this own objects this enemy's x coordinate which i can send by just saying position dot x you can send a parameter to a function that function will then have access to even if that code is somewhere else on some other object by putting that information here okay so we're essentially sending the body ouch function a number okay and it happens to be the number that is the current position at the time of collision of the enemies x position okay what are we going to do with that well i have to first implement it in my ouch function so i'll save that code if you need to pause the video and get that code please do but i will go back to steve because on this steve we have an ouch function but it's not expecting right now any parameters and we need to program that so i'm going to have to put here a new variable declaration to accept the parameter that's being sent to it so i'm going to write var that's how you create a variable of course and i'm going to call it uh pose x all one word you cannot put spaces in a variable name so that's uh what we want maybe i'll say enemy pause x that's kind of long but i think it does the job it describes it pretty well so now we're gonna say because we in our steve code have access to the exposition of the enemy that just hit it we're gonna say well we're not just going to jump up and down i'm going to go below here if position this is my steve's position now because i'm just typing position and not enemy pause x dot x is less than enemy paws x in other words am i to the left of the enemy because when your position dot x is less than the enemy is position x that means you are to the left of the enemy i'm going to put a colon right there and i'll press enter if i'm to the left of the enemy when i hit the enemy i want to jump to the left so i'm going to say i'm going to make my velocity dot x this is me now telling my velocity dot x to do something i'm going to make it equal to and this is where i'm going to play around with numbers i'm going to try negative 800 and uh that's going to be quite like a sideways jump essentially but i'm going to press enter and backspace to line up again with my if because i'm going to say otherwise if or else if or elif for short in gd script i'm going to say position dot x is going to be greater than if it's greater than enemy plus x then i want to jump in the other direction so i'm going to say velocity dot x is going to be equal to 800. okay so jump in the opposite direction now we can try this out in fact let's do that but we're going to run into a problem here and you'll see it in just a moment i'll do a control s to save of course if you need to pause the video out please do if you need to get the code but i will go back to my level 1 scene and press play scene and so now if i run into the side of an enemy i jump and that looks pretty good if i jump into the other side of an enemy well i'm still that red color so maybe i should stop the game and go back and uh try getting hit by there that's that works pretty well it works on the right side going in the right direction but if i restart my game uh what i'll find is that if i'm already walking and i walk into the enemy because i'm pressing that arrow key down and i'm moving to the right that me pressing an arrow key to the left of the to the right will override me being thrown back in the right direction so if i'm already i'm standing still i'll get thrown but if i'm walking i'll just keep kind of going in that direction because my speed is going to override that throw back direction or throw forward direction so what i want to do here is actually disable the player the person who's playing the game their player's input so if they're pressing a key on the keyboard to make the player move we're gonna actually stop that from working until they let go and press the button again so i'm going to go back to my steve script and what i'm going to do here is i'll press enter in backspace to line up my cursor again what i'm going to do is i'm going to type input with a capital i this is how you can affect input things just like we did to make input check for an action pressed earlier on so what i'm going to do here is say action underscore release and this will release any actions that are currently being pressed i want to release either left or right depending on which one is being pressed if either of them are being pressed so left and i'll press enter input dot action release and i'm going to say right in uh double quotes of course so that should work for that if i do a control s to save of course of course if you need to pause the video please do to get this code out for yourself but i will go back to my level 1 scene and i will press play scene and now if i get hit by the enemy i get thrown back and if i run into the enemy well i can no longer move until i let go and i press left or right again okay that isn't necessarily totally ideal because once i get thrown back i'm still holding the right uh arrow key and it's not letting me move until i let go and that is a little bit weird maybe i'll change that in a future video but i think we're doing pretty well except i want to die or reset my scene once i get thrown back the problem is when i get hit like i currently am i want to turn red i want to get thrown back and i want to wait a second before i switch back to my game level again to reset my game or go to a game over screen so i don't want to just die automatically so what i'm going to do is go to my steve scene and i'm going to add a timer just like before okay that's how you make a delay in your game so in my steve scene i'll select the root node i'll press plus i'm going to add a timer hey i've already searched for it so there it is i'll double click on timer what i'm going to do is after one second i'll leave it at one and i can adjust it later and we're going to make it a one shot timer because you can only die once then the game will restart itself at least for now so we have a timer and once it runs out we want to have the game reset to level one so with my timer selected i'll go to my node dock i'll go to the timeout signal and i'll double click we're going to select the root note of steve this is what we have our code on in our steve scene and it's going to make a new receiver method a function called on timer timeout with steve selected i'll press connect i'm not going to change that name now steve has a method called on timer timeout and when the timer does time out in other words we've been hit we were are gonna start this timer we haven't done that yet we then want to change the scene which of course is get underscore tree parentheses dot change scene and of course we want to reset our game to level 1.tscn this timer is not going to start though until we tell it to start and we have not done that yet just a second though let's go ahead and check to see our timer has the timeout calling and running this function which is if i double click on it it'll take me right there on line 60 of my steve dot gd script okay so if i am hurt as steve i will and eventually run this ouch function which in which i will be thrown in one direction i will disable the users left and right actions at this point i'm going to start the timer so i'm going to say dollar sign timer dot and i'm going to type a start and that is a method call so we're going to put our parentheses there that should work okay so i'll press ctrl s to save and i will go back to level one if you need to of course pause the video to get this code for yourself please do but i will press play scene and now if i get hit by an enemy well i'm thrown back it waits for a second and then it resets the game level and you might want to play with that timer's countdown value or the the timer value i can't quite recall what it's called this timer's wait time so if i change this to 0.7 or something like that in the steve scene and i save it and i go back to level 1 and i press play scene now i probably won't land or just as i land it will uh oh that didn't quite work as expected so it seems to work 99 of the time or so at least for me okay i was in this video going to add a bottom checker to my enemy because if you have an enemy that falls down on top of you you don't want to just land on you like a kinematic body 2d you want it to basically have the enemy maybe do a little bounce if it lands on you and so you might want to have the enemy have a bottom checker and if that bottom checker hits you in the head you should still die and that means you should probably have that ouch method called as well but i'm going to leave that up to you i might add that between this and the next video just to save on this video's length so that will be it for this video as it stands we have enemies now that you can customize um to go in one direction or the other initially we now have the ability to squash enemies and they wait a second and then they disappear and if you hit the enemy from the side you will get thrown back and you will turn red transparent and then it'll wait and then it'll reset the scene one last thing if i want to make enemies a different color depending on if they have a different behavior and this is something that i thought about around the time that i was planning for the last video some of your enemies depending if i select the enemy have the ability to detect cliffs and so this enemy one right here does not have detects cliffs turned on and therefore it'll fall off of a cliff whereas these ones if i select enemy 2 which is this one it will detect a cliff let's see about enemy 3 this one will as well and that's not good video game design because you can't tell the difference as the player between these two things and so you might expect once you see this one fall off an edge you might expect this one to also fall off the cliff once it gets to over here and it won't it'll turn around so ideally this type of enemy would be different in appearance than this one so we're gonna actually set up in our enemy very quickly if i go back to my enemies scene and i go back to my enemies code if that variable called detects cliffs is either not true or true we're going to use that color modulation to change the color of that enemy so some enemies will be a different color based on if they can detect cliffs or not this is going to happen in our ready function right when the game first starts um i'm going to make an if statement i'm going to say if detects cliffs is true and you can just put if the text clips i'm going to put my colon there and i'll press enter we're going to call that set underscore modulate modulate uh method which needs uh parentheses of course and when you call set modulate you have to put a color in here so i'm going to type color with a capital c c-o-l-o-r and i'll put my parentheses here and just like before on our player we need four values uh or you can just use three for red green and blue if you don't need to change the alpha as well so if i put one comma one comma one that will modulate this player's look with white which will not change it at all so if i do a ctrl s to save if you need to pause the video please do i will go back to my level one and press play scene you know they're all the same color still now if the enemy i'll go back to my enemy scene and go back to my enemies code if the enemy can detect a cliff i'm going to change these three numbers to 1.2 comma 0.5 comma 1. so we are turning up the red turning down the green and leaving the blue at its normal level typically you leave these where you change these numbers with a value between zero and one so we're actually turning red up too high we're adding more red than there actually would be but uh i think that's okay let's go ahead and see what this looks like if i do a control s to save i will go to my level one and press play scene and so now some of the enemies are purple and some are their original color and of course you could put your own code change uh to change the color for both types if you want so now i i can tell that these two enemies might do something different the ones that are purple will not fall off of a cliff and the ones that are blue aren't as smart and they will fall off the cliff okay so that will be it for this video lots of code in setting things up in this video but i'll leave my enemies code up on the screen right now and i will switch over to steve.gd in which we have more code so there's the top of steve's code and there's the bottom of steve's code but that will be it for this video of course if you like this video or learn something in it please go ahead and click on that like button this video it really helps me in my channel and if you want to see 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 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 that's where i communicate with you guys the most other than on youtube but that'll be it for this video thanks for watching we'll see you next time bye you
Info
Channel: BornCG
Views: 26,324
Rating: undefined out of 5
Keywords: tutorial, lesson, beginner
Id: eyRNGKVbWm0
Channel Id: undefined
Length: 59min 24sec (3564 seconds)
Published: Wed Jul 15 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.