Godot 3 - Platformer Tutorial - Part 14 - Destroying the Player (Let's be fair)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up everybody welcome to my pixel as always it's awesome to have you here if you're new to the channel then nice to meet you today we're gonna work on making our player a little less overpowered up until now the player could destroy enemies with fireballs but the enemies couldn't do anything to fight back so to make things fair we're gonna go ahead and add in the classic platformer behavior where the player dies if coming into contact with an enemy so let's get into it we're going to start off by programming in what we want the player to do when he dies so let's get into our player and the player script in this area where we declare our variables we're going to create a new one we're gonna say var is dead and we're gonna start that off equal to false right our players alive at the start of the game okay and here we're going to say if is dead is equal to false whoops is equal to false then we want to be able to do everything in here all of so take everything that exists inside of the physics process if you followed along exactly as I've done it if you've done it by yourself you've changed some stuff it might be a little bit different but this is all the movement code basically anything you want your character to be able to do while he's alive so we're gonna indent that so that it becomes part of this if statement so if is dead equals to false so if the characters alive I want to be able to do everything that I used to do before so then what this does is if the character is dead then we effectively disable all the player controls if not even after the character died you still be able to move and jump around the screen which doesn't make too much sense next we're going to go ahead and create a new function that specifies what to do when the player dies for that we're gonna go ahead and create a new function so over here under R let's see animation finish what why don't we put it right here so we'll say func and we'll just call this one dead and then we'll say when we run this function we're gonna set is dead equal to true the LA City equals we're going to zero out our velocity we're going to say that we want our animated sprite dot play and we're going to want it to play a dead animation and we're gonna say cool in shape 2d dot disabled equals true so with that we're gonna disable our collision shapes so that nothing else can or so that the dead body doesn't interact with the rest of the environment anymore and then we're also gonna say timer that start okay so with that we've created our function here now we don't have the dead animation yet and we don't have a timer yet so let's go ahead and create those first we'll add in a timer for the player so go back into this scene tree here and on there player we're gonna just want to add a timer as a child okay we're gonna set or well process mole we'll leave it at idle wait time can leave it at one so that's one second we're gonna set it to be a one-shot timer and we do not want it to auto start right we want to tell this timer when it should start then for this time would it be any bit useful we going to go to this node over here and then we're going to connect the timeout signal to the player so when this timer times out we're going to want to do something so we'll just connect connect to the player the kinematic body connect and then we have our new function here that we can put our code in the code we're going to add in here is first let's get rid of this pass and then we're going to say get tree change scene title screen dot t SC n so when this timer times out it's going to take us back to the or well then what happens here is all right when the player dies then we run the timer when the timer runs out we're going to change back to the title screen because our characters did and we can't play anymore now we need to go ahead and make our dead animation so let's go back into our animated sprite get this inspector get into the sprite frames we don't have a dead animation yet so we're gonna need to add one just call it dead let's see and then we're gonna add in our sprites today I'm just gonna make the player blink or flash a little bit so we have one sprite and one semi transparent or translucent sprite right be the alphas not 255 anymore and then we're just going to set the FPS we usually set it to 12 I'll set it to 24 this time we're going to want it to loop between these these two so make sure loop is on and then that will do it for our animated sprite that we need to add now we're done with the setup of what to do when the player dies but we still have to figure out what we're going to do to actually trigger the player's dead function that we need to make the player die for that we're gonna use a get slide count method that goes hand-in-hand with the moving slide method that we're currently using to move our player so let's do a quick save on our scene and then let's jump back into our player script so looking back into our physics process that we're in here if we go all the way to the bottom of the code that we have and we're gonna make a couple additions here we're going to say if get if get slide count is greater than zero for I in range get slide count if enemy in get slide collision I thought Collider name dead alright that was a lot of code there so let's go ahead and explain some of that so this gets light count what that does is it gets every after a moving slide function this counts the collisions that occur so collision that might occur with the ground the wall or a or an enemy so if that slide count is greater than zero then that means that something has been collided with so we use this for I in range this is just a for loop so what it does it for every collision right so get slide count zero gets slide count one gets like two for every collision that has occurred we're gonna put the information for that collision inside of variable I then we're gonna say if enemy or if the string enemy is in get slide collision I so this is the collision index number so for the collision at this index number so let's just say that's collision one for collision one I want to know the collider what it collided with and then I want to know the name of that Collider so it says write get what I collided with that's the collider if it's name has the string enemy in it then I want to do my dead function this this way if you do something like collide with the tile map or a wall or some platform it shouldn't have enemy in the name right so then you're not gonna go ahead and kill off your player so only if enemy is the name of what you collided with then we run our dead function now let's run the game and see what we've got okay there we go we we hit an enemy and we die we can do that all over a place let's see which one was my jump key here jump on them takes us back to our title screen that's great looks like everything is working but we actually have a problem for that let's go ahead and show you that that problem here when I'm moving I die but if I just stand here right that that's not supposed to happen right anytime I touch the enemy I should die but if I'm not moving then nothing happens if I'm moving and I touch him then I die so that's a problem but what's more than the problem is understanding why it happens right I can tell you the solution but we need to know why it's happening well why that happens is because we're currently calling this dead function after we do a get slide count that pulls information from our move and slide function this moving slide function only happens when we're actually moving so if we're not moving there's no possible way for us to get a slide town and get a collision and for all this code this this will never occur but what happens is the enemy is moving right now so since the enemy is moving he'll be using his moving slide function so we can simply put this code or a similar code into the enemy so that if we're standing still and the enemy hits us then we die also so let's go ahead and do that jump back into our it jump into our enemy and then go into our script here so we'll go down after all the other code in the physics process and then we'll try something like this if get slide count is greater than zero for I in range get slide account if player right in the player script we were looking for the enemy now we're in the enemy script and we need to look for the player so if player in get slide collision I dot Collider dots name then we're going to get slide collision i dot collider dot dad okay so these first three lines over here are exactly the same as what we have in here right it's just this last line that's different so in the player script if all of this happens we bang into an enemy then we run our dad function for the enemy if all of this happens and we bang into a player then we use this gets like a lit excuse me get slide collision that Collider this this is going to return the object for our player once we have the object for a player we can say it dot dead to run the players dead function right so yeah again almost all the same but over here the player runs its own dead function in the enemy script the enemy is basically telling the player to run it's dead function all right so now that we have that we should be able to run our game and everything should work so cross your fingers okay we run in here and we jump jump on these guys that that still works it's always good to test to make sure that something previously every working still does work after you modify your code and then if we stand still and he hits us we die great so it works in any case we can run into these guys everything else still works we can shoot them but now they have a little bit of shrank to fight back okay great today we went ahead and made our game a little bit more fair we found that using kinematic bodies and collisions is not quite as straightforward as when we used area to DS with our fireballs so it was a little bit tough it took a little more work but because of that we got to learn about the for-loop which is used quite often and will surely come in handy in the future that's it for today ladies and gentlemen I'd like to thank you all for watching and I hope you found this helpful if you liked today's video please give it a like and hit that subscribe button if you haven't already as always the sprite source cone and everything else that I've used in this tutorial today is available on my patreon page so if you want to check that out and also support the channel the link is in the description and with that we're gonna call it a day so thanks again to everybody for watching and I'll see you in the next one real soon take it easy
Info
Channel: UmaiPixel
Views: 24,455
Rating: undefined out of 5
Keywords: Godot, tutorial, platformer, pixel art, game engine, indie
Id: 1_dqnDxP6lQ
Channel Id: undefined
Length: 15min 15sec (915 seconds)
Published: Fri Nov 02 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.