How to make a 2D Platformer - Part 8 - Player Health UI

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to be adding in a simple Health UI here on our game so we're going to have it say health and then whatever our player's current health is which it is three right now and then if we take a damage you'll see it went down to two goes down to one and so on and then we also fixed our players or our enemies here so they're actually walking on the ground instead of in the air a little bit so we'll go ahead and we'll jump in and get started so we're going to start off by adding in a text game object so that the in we're going to call that one the player health so down here in UI we'll select text text mesh Pro and you might get this pop up if you haven't installed it already so we'll go ahead and import the TMP Essentials so once we have this here we can go ahead and I'm going to click this and rename mine to player Health text and then over here um if you scroll out a little bit in your scene view you'll see the the canvas here as well I'm going to move mine up as I want it to be towards the towards the top um and then I'm going to scroll down a little bit and I think that I'm going to this looks kind of big uh so I think I might adjust this down maybe to like 32 or something along those lines and we can change this a little bit later and then over here in the top left I'm going to click this this to Anchor so it's going to be anchored in the top left corner as well um and that's going to show so if we click we have this play maximized and we play the game it's always going to be up here in the top left corner for us which is what we want and then I'm also going to change the color I think changing it to to a black like this is going to make it look a little bit better and maybe I'll also set it to bold and I think I'm still going to lower this down maybe to like a 28 or so for now so the next thing that we're going to need is our UI manager so I'm going to create an empty object and I'm just going to call this one UI manager and then we're also going to need a script so we'll create a script over here and I'm going to again just call this one UI manager all right so inside our script here we're going to do a couple of things we'll remove those as we shouldn't need them and then here we're actually going to want to be using uh TMP or TM Pro I'm going to clear this up a little bit I'll get rid of that and also get rid of update as we shouldn't need those now up top we're only going to need the one variable and this one is going to be public and it's going to be a oops a text mesh Pro uh ugui here and I'm going to call mine Health text and then here in start what we're going to want to do is we're going to want to want to say player health and I'm going to call or call it player Health oops and this is going to be find object of type and then of type player health so this is going to find the player health component that is in the scene and then what we'll do is we'll say if the player health is not null so exclamation point equal null so if it does not equal null so if there is a player Health object then we're going to update um update our health so what we'll do is we'll create that first so down here we're going to say oops uh public void and then update Health we'll call it and in here this is going to take in an INT and it's going to take in the current health so here we're going to adjust the health text so the health text. text and that's going to equal uh you know this will be whatever we want it to say so we want ours to say um Health with a little with one of those and then a space and then we'll say plus um this is going to be plus current health. two string so it's going to take it our current health and it's going to take it from an in and put it into a string and then this is going to update the the health uh display text for us and so we want to do here on start so we want to make sure that this is initialized right at the beginning so what we'll do is we're going to say update health and then the uh the current health is going to be the player health. current health cuz remember down here it takes in a current health and that's going to initialize the health text uh with the player's current health whenever the game starts up so we'll go ahead and save that now back in the editor here we can take the script and if we add it onto our UI manager you'll see that this currently says new text but if we click on play this should change oops sorry so what we have to do is the UI manager uh if we stop this we never assigned our health text so we need to drag this into here and then if we click on play this should change from new text to our health and it currently says we have zero Health um however we do have uh we do have three Health not zero health so I'm going to put this back to play focused and press play one more time here just to show you uh it's saying that we have zero health even though if you look at the player uh we do actually have three as a current health that's simply because it's it's not it's not initializing properly uh and we can definitely fix that here so to fix this what we can do is we can go back to our player Health script and what we can do is right here where we're initializing or setting our current health to equal our Max Health at the beginning uh we can actually change this start method to an awake method so awake runs before start since the awake method is going to run before start uh this is going to ensure that our our health is set before before the start method runs in the other script there I can definitely pop up an image here showing the execution order that Unity uses in case anybody's interested there are a lot of different functions I will provide a link to the documentation in the description if you want to look at the rest of them now if we go back in here and if we test this out we'll see that this changes to health three so that's that's working out good for us uh one thing is I still think this is too big so I'm going to to move this down again I don't know maybe like 24 or something just feel like it's too big for for me but we'll leave it like that uh you can adjust the placeholder text as well where it says new health or new text you could change it to like health or something if you wanted so it makes more sense so now if we if we test this one more time uh one of the things so it it does say that our health is three and if we go and we get hit by the enemy uh it should update but it's not we're not actually losing any health even though you know we've died and we've lost all of our health so we'll going to go ahead and fix that now so there are several ways that we can fix this uh for us we're just going to be adding a reference in in our player Health script to our update Health method this is going to be relatively efficient and it's going to keep the the separation of um responsibilities so it's going to have the player Health script handle the health logic and then the UI manager script handle updating the UI so inside the player Health script here back up at the top what we're going to want to do is we'll put in a private and this is going to be an INT or sorry this is going to be a type of UI manager and I'm going to call it uh UI manager and then down here in our awake method uh we're going to find the the UI manager so underneath current health we're going to say UI manager and that's going to equal find object of type of type UI manager like so and then now down here in our our uh take damage scripts so whenever we're taking damage this is where we want to be updating that um the current health so it updates the health display for us so right below current health and um where we're taking the damage and then right above where we where we would die is a good place for this and what we'll do is we're going to say if the UI manager is not nin so we want to check to make sure that we have a UI manager and if it's not then what we're going to say is UI manager. update health and then we're going to pass in again that current health that we need to put in there and that should update the health display for us every time that we take damage so jumping back in here if we press play and try this out one more time uh we start at three health and then if we run into the enemy it drops down to two drops down to one drops down to zero and so on so that part is now working correctly so one thing that we can fix here is the enemy so you can see that they're not actually touching the ground which is kind of doesn't look very good uh so what we can do is we can go in here and we'll just double click to hop to one of the enemies here and they should all be at the same height so what we'll do is I'm actually just going to select all of them um and then scroll in here a bit and then just just kind of move these down so that they're all at the same the same height and so they're all actually on the ground cuz that doesn't look very good and it looks like maybe the same with our player I don't remember if our player actually falls down or not though once the game starts yeah the player actually drops down um so now they're actually on the ground which is which looks a lot a lot nicer in my opinion so that's how we set up the UI and that's how it's working for us now uh maybe in a couple later videos We'll add add in like a score and those kind of things and maybe a timer as well that will also incorporate with the UI um and then one other thing that we're going to want to fix later on in in a later video is the how the player just kind of gets stuck on the enemy there like that uh that's not something we want and then we'll also add the ability for the player to to kill the enemies as well so those are things we'll be working on in future videos but thank you for watching
Info
Channel: NIfty Yak
Views: 67
Rating: undefined out of 5
Keywords: Unity, Game dev, Beginner, Pong, Game development, Tutorial
Id: 4rg5kcE89C4
Channel Id: undefined
Length: 11min 27sec (687 seconds)
Published: Mon Jul 08 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.