Game Developer Makes a Mobile Game (3/4)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up everyone renaissance coders here welcome back to the third episode in the tap shift series in the last episode we created our obstacles and pickups so check that out if you need to but today we're going to be moving forward today we're going to learn how to enforce our pick up rules for the invincibility and score pickups will understand how to determine when the player loses we'll add a game over screen and then we'll start keeping track of players score we have a lot to get into today so let's go ahead and get started I think one of the first things we should do in this tutorial so that everything else makes sense is to handle the lose state so you want to be able to determine whom the player loses and then reset the game when that happens okay so to enforce some of those rules we want to start off in our game controller so here I have the game controller open and let's just start off with one of the easiest detections which is going to be detecting when the player falls off of the screen so if the player stops tapping and they fall below the bounds of the screen we want to make sure that they lose in that situation so let's create a new method inside of our game controller the name of our method is going to be private void detect player fall and in here we're just going to detect if the player is outside of the bounds of our screen in the negative direction so what we'll actually do is make sure that we called detect player fall in the update method so we'll come up here to our on update method and under check player progress we will call detect player fall so we know that every update cycle we are going to be determining if the player has fallen off the screen in order to detect this we want to check a relative position between the players position the players Y position and the cameras Y position so how do we do that well it'll be a simple conditional statement so we'll say if the camera let's see see if we have a reference to our camera in the game controller yet doesn't look like we do we do okay so we have camera so we'll use that as a reference here if camera dot transform.position dot Y and then of course we're looking for the player the the cameras position relative to the player so what we'll do is we'll save if the cameras position dot y - the players position dot y so - player dot transformed that position dot Y is greater than some value right and that value is going to be the distance between the the middle of the screen and the bottom of the screen where we want the player to lose up so we'll start off by just saying 5 and see what that looks like and then if this condition evaluates to true we want to call some function called end game so let's create a new function called in the game down here we'll have a boolean called a game over and if game over is true we just want to return we want to make sure that we're not doing anything else if it's not true we just make sure that we set it to true so we say game over is equal to true and then here is where we would pause the player we would play some animation for the player and we would turn on the game over screen so we can add these comments here as a reminder so that we can come back to this and add all of these rules for now what we could do is call a reset function which is simply going to reset all of the games resources so let's add another function down here called reset so inside reset is where we might reset the score to zero we would ask the obstacles to reset so reset obstacles we would also tell the camera to reset and of course we would reset the player so for the objects that we have access to right now the obstacle is the camera in the player we can go ahead and add some code here for the obstacles well ideally have some method inside that controller called a reset so we can call that here our camera will also have a function called reset within it and so a lot of player so these are functions that we have yet to create but those are going to be implemented very shortly in addition to resetting the obstacles we want to make sure that we add add a new obstacle when we reset the game we want to make sure that we clear the obstacles but we also want to make sure that we're spawning a new one just like we did whenever we handled the initial initialization of the game controller so we can take this and drop it down here of course this would indicate that we would want to reset our progress to the proper value let's see what we initialize that value to appear so we initialized our progress to negative one we want to make sure that we reset that to the proper value so maybe we can do that right here so we can say the progress is equal to negative one and that would complete our reset function for now eventually we'll come back and make sure that we're setting the score to zero as well so I think in the last tutorial we actually did implement the reset function for obstacles let's just take a look at that really quick okay so we did add a reset function for obstacles let's check the camera and the player so camera controller here so we need to add a new public member for resetting the camera let's check the playercontroller and it looks like we need to add a reset function here as well okay so for our playercontroller what we'll do is initialize our target position to vector three dot up times negative four this will be zero on the x axis negative 4 on the y axis and zero on the z axis and then what we'll do is we'll set the transform position equal to that target position so it snaps back to the starting position remember that our player does start off at this position it doesn't start off at a vector 3.0 of course this could be different based on how you place your camera and your player in the beginning but if you follow it along up to this point this is the value that you would want let's go to the camera controller and see what we do there so this is going to be a pretty similar implementation of the reset function essentially we just want to reset the position of the camera but for the camera we'll set the target position equal to the initial position which I suppose we could have setup for the player but we didn't and then we'll set the position D equal to that target position okay with that we can recap here what we've got we have the detect player fall which is going to see if the players fallen off the bottom of the screen if that happens we'll end the game inside in game or check to see if we've actually transitioned to this lost state and if we have not yet then we go ahead and make that transition inside trends and inside this lost state we'll make sure that we set game over to true which is a boolean we still need to create before we test this we'll go ahead and pause the player and we'll play some sort of animation for the player this is also where we turn on our game over screen and then we call the reset function the reset function is temporary until we actually get a game over screen but what reset does is it just snaps everything back to their initial positions so let's create this boolean for game over and then we'll test this to see if it works so up here we'll just have a bull call it game over now let's go back to unity and see if this actually works okay so here we are in the starting position I'll just jump up a little bit and then if I fall off the screen we can see that it restarts so it looks like it's working okay it definitely didn't work there okay so I already know the reason why this is happening let's go back to our game controller and see what's going on remember we're checking to see if game over is true and if it is then we're not doing anything so all we did was we forgot to set game over to false we would want to do that inside the reset function so let's go and make sure we say game over is now equal or false and then we can go back and we should be able to see this working properly okay so I'll jump up a little bit we lost there we should be able to lose again okay so it looks like it's actually working now you can see that the player snaps back to the correct position of negative for the main camera comes back to its initial position as well so it looks like this is working now what we need to do is implement the same thing whenever we actually run into some of these obstacles okay so to detect the lose state for the obstacles what we need to do is create a new script that goes on our obstacle so let's take this for example so any any moving object on the obstacle will have a script on it that's going to check for trigger events that trigger event is going to tell the game controller that the player just hit something and that will trigger the reset function or the game over function so let's let's start off with this obstacle 2 and let's start off by adding a box Collider to each of the obstacle so let's see here we should be able to see a green outline I can barely see a green outline there just to verify that that trigger is the correct size and then don't forget to click the is trigger boolean and then we'll just do the same thing here okay once we do that we want to make sure we click on the parent of the prefab click on this overrides to drop down if you're using this version of unity and then click apply all so that applies to the prefab we'll do this for obstacle 0 and obstacle 1 so let's just add box colliders to these make sure you're adding box Collider 2d otherwise this won't work there we go so I'll click apply all and then finally for the last obstacle here make sure that that is trigger and then we'll apply this prefab as well okay so we need to maybe I shouldn't have applied those yet we actually need to create a script that goes on each one of these so let's go to our scripts in our game directory and we'll create a new C sharp script called obstacle let's open that up okay so I'll start off with just a clean script here get rid of some of this boilerplate and I'll add my region of course for unity functions this is gonna be a very simple script but it's just good to get into the habit of creating structured code here so we're going to have let's see we could have a reference to the game controller as a member variable or we could just access that instance on the fly regardless our unity function is going to our region is going to define the ontriggerenter to D so say private void on trigger enter to D will be accepting a Collider 2d as a parameter to this function and inside of here what we want to do is just check to see if this Collider is the player to do that we can say if Co l gave object tag equals and then pass in this string constant player so if this is the player this is how we know this is the player so if this is the player then what we want to do is just say the just we want to access the game controller and tell it to reset or something like that so we'll come back to our game controller and make sure that we actually do have that public instance we do and all we're doing is when that happens when the player actually hits that object we're just calling this reset function here actually because this reset function is private we might want to keep that private instead what we could have is a public function here called on player hit obstacle and then here we would just call reset like that then we can call this public method from the obstacle class okay let's go back to unity and make sure this obstacle script is on all of our obstacle elements and then we'll make sure we save those prefabs and test to see if this works okay so we want to test this for each one of the obstacles we only have three so it shouldn't take long to run through them all so here's our first one so we hit that and it actually reset the game for us let's hit this one okay and it reset the game for us and see if we can get past this that one's a little bit tricky we might have to scale that one up okay so it looks like all three of our obstacles are causing us to lose so we're off to a good start now instead of just jolting the player and the camera back to the start position ideally we would want to have sort of game over screen so let's go ahead and implement that right now let's start off by creating a canvas for our game over screen our canvas can just be called UI that's good enough we want to make sure it's overlay which is default I usually scale with screen size on the canvas scaler and then I just set the match to 0.5 and other than that everything is OK we'll just start off by creating an empty object which is going to be the page itself we'll call this game over page now on our UI element on our canvas we want to make sure we have the page controller object which comes from our unity core scripts we'll make sure that we're debugging that and you can see here the goal is to add a game / page inside the pages array so here is eventually where we'll also have the we'll also have the main menu page and the next tutorial we'll add that here as well but for now let's just design our game over screen so I'll have some sort of UI you can either take up the whole screen you're kind of in charge of the design at this point I think maybe what I'll do is only take out part of the screen something like this so maybe we can say 400 width and 400 height something like that doesn't really matter at this point we're just trying to get some basic functionality and some of this stuff that we want to add to our game over screen would be the player score a button that restarts the game so let's just start off with those two elements I'll also end up having a title as well so the title would you know tell us that the game is over so let's add this text element here say game over I'll use best-fit on the text and then make sure that I Center it like this yeah I'll name this title okay so we'll also have a text element for the players score some other things that you might add to the game over screen would be the player's best score you might also give them options to watch ads to continue going or you might give them options to use sort of in-game currency to continue moving on you'd also give them the option to go back to the main menu here but all of that is up to you you can add whatever information you need to in the game over screen obviously for now we'll just have the player score as one of the text elements and this will be something that we end up modifying later I want to set the size to be a little bit smaller so maybe I'll just reduce the size of the RET something like that and then of course we want to add the the button that actually restarts the game so I'll add a button down here this will be our reset button we can say try again here of course the design again is all up to you guys so these are the bare minimum specifications for our game over screen and the only other thing that I would want to do is move my anchors up to make sure that this would conform to any sort of stretching caused by different resolutions I'm not sure that would happen with the current canvas killer settings but I like to just make sure so our game over page is going to have a custom page script which is going to derive from our unity core page class let's go into our game directory and add a new directory called UI just like that we're going to have this called game over page and again this is going to derive from our unity core page script let's open this up to do that we want to make sure that we derive from or we want to make sure that we first import unity core down menu and then we want to make sure we derive from page okay so let's open up our page class and we are going to have to add a virtual member to this so we want to know essentially when the the page is enabled when the page is enabled we want to capture the players current score and display that and we also want to store a reference to the game controller so let's just create a virtual method inside this this is something that I may end up updating and the unity core script so if you already see this function inside your page class then don't worry about adding this but right now it's not actually in there yet this is going to be a protected virtual void method that just means that it's over aidable and we're going to say this is on page and Mable on page enabled gets called in the on enable function and then we can override this function inside our derived class so we'll go back to the GAMEOVER page will have a new region for override functions and then we'll override that function protected override void on the page enabled and so here we can do any sort of page initialization while we derive the page class so again what we want to do here is capture the player score and display it and then maybe even just store a reference to the game although we could just access the instance on the fly so let's not worry about that we'll have a public function for the button to access that's going to be called try again so public void try again and this is where we would we would ask the game to reset so tell the game to reset and it will also close this menu page so we'll end up creating a function in the game controller class I'll try again and then to close the page we'll say page controller dot instance dot turn page off and then we'll just pass type in so type is a member in the parent class that refers to the type of this page let's go to the page and make sure that that is accessible right now so that is a public type so that is accessible ok and that actually reminds me that we need to open up our page type and make sure we have a game over type inside our page type and raishin so make sure you add that as well okay let's add this try again function in the game controller so that will essentially be duplicating this function will just call it try again we could have one function for any sort of like restart we could just make reset a public function of course that is totally up to you guys if you want to do that let's go back to unity and see how to set all of this up okay to hook this up we want to click on our game over page add the game of our page script make sure that the type is the new enumerated type that we created game over and up to you if you want to use animation that's an optional feature but for now we're not going to set that up we also want to make sure that the reset button is calling the correct function here so we'll drag in the game over page and then find the try again function so anytime we click the reset button it'll call that try again function which tells the game controller to reset okay let's see what else we need to do that might be it of course we do need to go to the page controller and make sure that we add this page reference let's do that and I think we may have forgotten in the game controller to turn on the game of our page whenever we lose so let's go back to the game controller really quick and make sure we do that so back in the game controller we want to say that when we end the game we turn on the game over screen so we can say page controller dot instance turn page on and the page type that we want to turn on is the game over page because we are referencing the game controller here we want to make sure that we have the correct using statement for the menu so we'll say using unity core menu now let's go back to unity and see if all this works okay so when we actually lose we should see the game over screen pop up and we do need - stop resetting forcefully whenever the game is over let's go back to the game controller really quick so what we need to do is come down to our end game function and get rid of this reset function so the only time that we reset should actually be based on should be triggered by the game over controller so that means that when we hit the obstacle we don't want to reset but we do want to call into game so let's follow this logic really quick when we hit the obstacle we call end game and the game turns on the game over page so this should work let's go back to unity and try it out okay so we'll press play and then if I hit the obstacle the game over screen comes on and if I hit try again then it resets of course the problem here is that when the game is over we can still see everything moving the player falls down ideally you know it's fine if everything else is moving but we definitely want the player to stay where he's at so we want to make sure that we pause the player movement and we can create a new function in the player controller called lose or die or whatever you guys want and inside there we would have some control over the movement of the player so let's let's try to figure that out really quick okay so we actually do have a comment here for pausing the player let's say player dot pause just like that and we'll go to the player controller and add that function for pause of course if we are pausing the player then we want to make sure that at some point we run pausing so that could be in the reset function let's go back to the game controller so yeah when we call reset we we tell the player to reset so we could unpause the player and the reset function inside the player controller will want to have a boolean called pause and inside update will say if the game is paused or if the players pause rather then we just want to return so this would stop all movement and all events on the player so inside pause we'll just say pause is equal to true and inside reset will say pause is equal to false and then let's go back to unity and see if this works okay so I'll run into something here to end the game and we don't see the player falling off the screen so that's a good sign but when I hit try again the player can now move and let's try this where we just fall off the screen and we don't actually hit anything okay so we can see here that the player stopped whenever they reached this point this is actually good for us too because we can see exactly where the player needs to be for the game to be over we might want this to be more off-screen so it doesn't feel as jumpy but this might also be fine as well it might not actually be an issue what we're going to end up doing is triggering a particle effect and and hiding the actual player object so that might not be an issue now currently we have an invincibility pickup and a score pickup that aren't actually being used at the moment right now we have player death being tracked and we have the game over screen so we can actually test the invincibility pickup right now we don't have score tracking yet so we can't really test the score pickup but because we are tracking the lost state we can actually test the invincibility pickup let's find the invincibility pickup in the game so we're looking for a blue pickup here and we just want to see if that's going to we just want to prove right now that that's not going to actually do anything so this is a score pickup here that's obviously not going to do anything okay so here we have an invincibility pickup so when I hit that you can see that I can still run into objects and die so we want to make sure that when we pick up that blue pickup that we can remain invincible for an extended period of time so to do this we would add a new function called enforce pickup rules and inside this function what we would do is have some sort of timer that would be incrementing our invincibility timer detecting if the timer is greater than the expected duration and if it is then we would cancel out the effect so let's see what this looks like we'll keep a reference to eight Delta time for all other pickup effects and then we'll set that equal to time Delta time then what we'll end up doing is incrementing the invincibility duration so we'll say the or I'm starting decrementing the duration so we'll say invincibility duration is going to be reduced 5 Delta time so remember we are reducing this value because we're initializing the duration to the the constant up here we can see or to the duration so so let's recap what's going on here because in the last episode we created these functions and we may have forgotten I definitely did and we can see here that in the handle and invincibility pickup function we have the duration parameter that's coming in that's what we set invincibility duration to so the point is this is a non zero value and what we're doing is we're decrementing the the value of duration over time when that duration reaches a value of zero we know it can turn off the invincibility boolean okay so let's find our function here so what we do is we say if the so if the invincibility duration is less than or equal to zero then what we want to do is just say the that invincible is equal to false and in fact we would only want this condition to evaluate to true if invincible is true so we can also say inside this condition if the duration is less than or equal to zero and and the invincible bool is true and then what we could do is also just make sure that the miscibility duration is equal to zero but that's not necessarily required okay so with that we should be able to pick up the blue pickup which is the invincibility pickup and we should be able to run through all of the obstacles without losing let's go back to unity and verify the number of seconds that we have that pickup set to okay so our pickups here we have the invincibility pickup let's click on that let's see let's look at the actual child object and we can see that the duration of the invincibility pickup is four second so we can count down from four and after four seconds is up then we would obviously be losing when we run into stuff so let's play this until we find an invincibility pickup ok so here's an invincibility pickup I'll click that ok so we definitely hit it but we weren't invincible so we have the invincible bool but apparently we're not using that to invalidate any sort of player collision let's go back to the game controller and see what we need to do for that ok so we have this on player hit obstacle function so it actually is a good thing that this is a separate function because what we do here is just say if invincible then return let's go back to unity and test this okay so during my last testing period I realized that one of our obstacles is way too difficult so I'm just going to remove that one I think it was obstacle number two I'm just gonna remove that for now because that that is just annoying to play with right now okay and again I'm gonna be looking for that blue pickup and now we should definitely not be losing what do we run into stuff so let's see and I'm passing right through these I'll pick up another one here and after about four seconds the effect will wear off so we're getting pretty lucky here we're picking up all the invincibility pickups no score pickups being dropped that's just how it goes sometimes okay so we should be getting to the point now or if I run into one I should be losing it's definitely been more than four seconds and I'm not losing or maybe well we can go back and check the timer but looks like eventually I did lose it seemed a little bit slower but that's just something that I'll have to keep an eye on so we've tested the invincibility rules and now we want to actually start tracking score so that we can finally test the score pickup okay and then throughout that process we'll be posting the player score on the game over screen okay so probably the easiest way to do this would just be to create a public integer so that that's accessible by the game controller so we'll have a public int score but we do want to have control over who can set the score we only really want the game controller to be able to set the score so we can say score with a private getter and I'm sorry with a public getter so that would just be this in a private setter so this means that only the game controller can modify score but anybody who has access to the instance can get the score okay so now the question is when do we increment the score and basically any time we increment the progress for the player we want to increment the score for the player so where we have the check player progress function and we're incrementing the player progress here is where we can set the score to be incremented by the score multiplier which would be one unless we pick up the score pick up in which case it would be whatever that multiple is so we also want to make sure that we're resetting the score value whenever the game restarts so inside the reset function where we have our comment to reset the score 2:0 will just simply say score is equal to zero there and with that we should be about ready to test but I do want to double check that our well right now our score pickup actually isn't working so let's let's go ahead and add some coats the enforced pickup rules it's going to be very similar to what we did for the invincibility rules basically what we'll be doing is saying that the score multiplier duration is going to be reduced by a value of Delta time as well and then we'll be checking to see if the score multiplier duration is less than or equal to zero and also if the score multiplier is not already equal to 1 which is the preset value so we can say and score multiplier is not already equal to 1 in this case what we will do is have the score multiplier equal to 1 and that's it so this indicates the reset value for our school multi score multiplier and with that we should be able to go back to unity and test this so the invincibility pick up and the score pickups should both be working at this point so at this point it's going to be pretty difficult to know what the actual score is because we're not visualizing the score anywhere so one thing that we need to do before we move on is create some new UI that's going to tell us what the score actually is so let's just add some score text into our existing UI here I'll just add a text element and I'll double click that so I can get a good view I'll put this into my top left corner and then give it some sort of padding of about 12 the design here isn't super important of course we just want to visualize this score text and I'll make this a little bit bigger so it's easier to see okay so I think that's pretty good for now and now that we have this new score text we want to have a reference to this in our game controller and then every time that we increment the score or change the score we want to make sure that the score text is changing appropriately as well so let's go back to the script in the game controller and see what we need to add so we're going to want a public reference to the text component will call the score text because we're using the text component or the text type we want to make sure that we're using unity engine WI so we can access that object type so now any place that we change the value of score we also want to change the value of score text so I'll look for all the instances of score here and any time that we're incrementing it or changing it we want to make sure that we set the score text text equal to score to string so I'll find other instances of what we're setting this and it looks like we're only setting it here so we're only setting it in two places right now and at this point we should be able to go back to unity and watch our score as it gets updated okay so before we start you'll want to remember to go to the game controller object and fill in the empty field called score text with the newly created UI text element then with you press play and start visualizing our score in the top left corner so you can see it's incrementing by one now we will need to actually set the score in the game over screen but for now what we're doing is just testing the score pickup so let's make sure that score pickup works and when we pick up the pink pickup we would expect to see the score incrementing by a value of two okay so here's a score pickup and now every time I go through or incrementing by two went from thirteen to fifteen fifteen to seventeen and then we're just going to try to keep playing until that effect wears off so now we're incrementing by one again because that effect is borne off so it looks like the pickups are definitely working now we need to make sure that when we hit the game over screen we are telling the player what their score was whenever they lost and then every time the page is enabled again it'll be checking for the new current score so let's go back to the game controller and see what we need to do okay so remember every time the game ends we are turning on the game over page and every time we turn on the game over page this function on page enabled gets called so that means that all we need to do is get a reference from our game over page we need to get a reference of our score text just like we did just like we did in the game controller and because we're using that object type we want to make sure that we're using Unity engine dot UI okay so every time on page enabled is called we would simply say score text dot text is equal to of course we want whatever whatever specific text and our whatever specific verbage in our game over page to be prepended by the score so in our case we're saying player score with the colon and then we're appending the game controller dot instance score value and we'll say that that's two string okay so let's go back to unity and play this and see if this works so because we added a new public parameter to our game over page we want to make sure that we're filling that with the appropriate component so we have the score text here we just want to drag that into the score text and now we should be able to play and see our score displayed on the game over screen so I'll go ahead and lose now we can see player score is equal to one but if I play again and I get a difference for you can see that that score changes okay so having this having this this sort of view controller the page controller where our individual pages inherit from the base page class makes it very easy to update individual UI pages and hopefully this demonstrates that effectively all right now the final thing that I want to cover in this tutorial is actually playing some sort of animation when the player loses and that animation is going to be a particle effect so let's go to the player object here and add a particle effect that'll be just this particle system that is added as a new component so say particle system and let's just sort of go through some of these settings and decide how we want this to look alright so we basically want this to look like some sort of explosion so we do want the duration to be fairly short we'll set duration to 0.25 I'll turn off the looping here because we only want this to play once we'll set the start delay to zero we'll leave that will set the start life time to a value of two we'll we'll set the speed to a well let's see this is only the start speed so we can just say the start speed is a constant value I suppose five is fine the start size is pretty important so we do want this to be a fairly small size say zero point one and at any time that you are modifying the particle system you can go to the particle effect window here in the scene view just press play and you can see sort of what it looks like okay so we don't have that many particles coming off of the player yet and they are moving a little bit slow but I think the size is okay let's let's go to the speed I think there is a start speed this increases to ten and maybe we'll try 20 okay I think that's good there and let's see what else we can do let's go to a mission and increase the rate over time to something like 200 and then restart okay so now we have a lot of particles sort of exploding off of the player which is what we want I'll reduce that okay we can also change the the shape so how do we want these coming off I would change the radius I think the shape is fine I'll change the radius to 0.1 that'll make it look like the particles are coming close there they're spawning closer to the player so it looks like the players actually the source of the explosion so this is starting to look ok for now I don't want to go too in depth with the particle system because this is something that you can spend hours tweaking so the last thing that I want to do for this is simply change the color in the shape of the particles themselves because right now they're just pink squares so let's go to the renderer and we want to add a material for our our particles and we could just choose sprites default will restart and now we can see that these are white squares but maybe they should be circles instead so let's let's see if we can get one of our circles circular sprites here maybe just this default particle okay and I think that's fine for now for the purpose of this tutorial at this point we want to figure out how to play the animation once whenever the player loses and then get rid of the actual circle that is the player so what we'll end up doing is accessing the particle system API to play the animation and then we'll have an access to the sprite renderer from the player controller that's going to tell us to toggle this on and off at the appropriate time okay so we'll need two references in our playercontroller one would be for the sprite renderer so I have sprite renderer and then we'll also have one for the particles so that'll be particle system and we'll call this particles and what we want to do is every time we we pause the player which would indicate that the player has lost then we would actually spawn the particles and hide the renderer so we could say something like sprites dot enabled is equal to false and particles dot play so uh there's a play function on the particles class that we can call that will actually play that animation for us and then every time we reset we want to make sure that the sprite becomes visible again so we'll say sprite not enabled is equal to true now so let's get back to unity and see if this works well actually before we do that we want to go ahead and assign a value to each one of these so on an it I'm going to say the sprite is equal to get component sprite renderer and similar for the particles will say that is equal to get component particle system because the assumption here is that the particle system and the sprite renderer components are both on the same object as the player controller but if we do want to enforce that so that we can ensure that these values are never no we can enforce that by adding the require component class attribute so for our component and we can say type of sprite render and the same thing for the particle system so again what this does is it enforces these components on to the same object as the player controller which means that these values should never be null now we can go back to unity and test if this works okay so we'll press play and we can see when we press play the particles actually play we want to make sure that the play on awake boolean is not true so let's go to our particle system there should be a play on awake bullet here and what does it disable that so we'll go ahead and play again and that looks better and now whenever we run into something we can see the particle effect plays the player is hidden if we take a look at the scene view here and then if we hit try again the player is visible again okay so a couple of things whenever the player actually dies the game over screen shows right over the player so we never really appreciate the particle effect that's playing one thing that we can do to improve that is wait for about a second whenever the player dies before actually showing the game over screen so let's go back into the game controller so I can show you guys how to do that okay so in the game controller we'll want to come over here and first at the top of your script ad system dot threading tasks and the reason we're doing that is because we want to have some control over how long we wait before opening up the game over screen so for this end game function will add the async keyword which just Flags this signature as a function is that is asynchronous which means we can actually put some a weight logic in here okay so before we actually show the game over screen what we'll do is we'll just say a weight task dot delay and then we'll just wait for one second so this is milliseconds that we're passing into the delay function and again what we're saying is after we actually pause the player which in turn plays that then we want to wait for a second and then open up the game over screen so let's go back to unity and check if this works ok so I'll play the game here and if I run into something you can see that the player we can actually see the player animation and then the game over screen pops up a few moments later so that's exactly what we want and at this point I would say just play the game that you have so far and add whatever new obstacles you want to because in the next episode we're gonna be wrapping things up with a splash screen a main menu screen and some audio effects that we're gonna be sprinkling throughout the game so it's going to be a fairly straightforward tutorial coming up but that's going to conclude this tutorial we went over a lot today we covered score tracking game over screen we also covered player death and enforcing some of our pick up rules so yes we did cover a lot in this tutorial but I hope you guys enjoy and if you did go ahead and drop a like subscribe and add notifications so that you can catch all of our new tutorials coming up throughout the year but as always you guys thanks for watching and I'll see you in the next tutorial
Info
Channel: Renaissance Coders
Views: 2,349
Rating: undefined out of 5
Keywords: unity3d, unity-3d, unity 3d, unity, unity-games, unity games, unity-game-engine, unity game engine, programming, coding, code, coders, programmers, software, how to, tutorial, beginner, basic, simple, easy
Id: wdojQNQhDtc
Channel Id: undefined
Length: 51min 55sec (3115 seconds)
Published: Sat Mar 14 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.