Unity Game Creator Tutorial - Reducing Enemy Health - 26

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome to indie game hustle my name is charles and in this video we're going to continue with of course the platforming uh game and what we want to do here is reduce the health of both our little enemy here on the left and the one on the right now let's just take a look at where we currently were let's see here so if they hit us we basically lose health and so we want to basically do the same thing because now there is no health for these guys and uh they need to also lose health so um it's actually pretty easy and let me go ahead and see if i can get us through that um but before that you might notice that i'm using a darker themed unity here and so in order to change that you're going to need the version of unity 2019.4.8 f1 and so that's a new feature so in order to change it just head over to edit preferences general and head over to editor theme and change it from personal to professional all right cool pretty simple all right so let's go ahead and get this going here so it's kind of three things that we want to do the first thing we want to set up our health bar for the enemies the second we want to make sure that the health bar is facing the camera at all times and then third we want to trigger the health bar to be visible only when we're close to the enemy okay all right and we're gonna just do this really simple so all right so the first thing we need is a canvas created for the health of the enemies all right so the first thing i'm going to do is go to my mechanics i'm going to right click on mechanics i'm going to head over to create empty and then on this empty i'm going to go ahead and name this health okay and then in this health i'm going to name it i'm going to create a a ui canvas and then when i do the canvas i'm going to leave that just like that and i'm going to go over to canvas and i'm going to change this to world space and then i'll change this pixel reference to one i guess all right cool all right so if you were to zoom all the way out here you'll see that that's our canvas there so i'm gonna take this here the rec tool and i'm gonna take this just like this and i'm gonna bring it all the way down just like so and i'm gonna hit the f key so i can zoom in and i'm gonna get out of iso mode into perspective and i'm just gonna continue to bring this puppy all the way down until it's the size that we need all right so i'm gonna go ahead and hit the w key and move this here in place right over our enemies head cool all right perfect all right so i'm going to go ahead and do that and move that right kind of where we need it right over the head of the enemy and it looks pretty good so far so the next thing i want to do is put in a panel so i'm going to right click on canvas i'm going to go down to ui i'm going to go to panel and this panel is going to represent our health bar very similar just like what we did with our player so with this panel i'm going to select the wreck tool and let me change something actually i'm not going to roll i don't want to rotate the canvas so that was my mistake so i don't want to rotate that so let me just go ahead and move it into a general space over the player over the enemy and then again i'm going to go ahead and create that panel all right cool and then i'll just go ahead and move that panel in place just like that now i have the panel selected i'm gonna head over here where it says rec transform and i'm gonna select this here and then i'm gonna hit in the center and then i'm gonna do an alt select so to center it as well all right cool all right and so i'm going to also make it smaller and i think that's probably about the width that is fine for me and i'm going to select the canvas and just kind of move it up just a little bit and another thing i think i'll do is go ahead and just make the canvas universally like a little smaller just like that cool and go ahead and move that just like so all right cool all right so we have um where our enemies health is going to be and this is just going to be a representation of uh the health for this enemy now we're going to have health for the enemy but what we can do um for some enemies maybe they don't have a health bar maybe they only require like one hit so you just hit them and they they get hurt and they die um but for these enemies i think we're just going to go with a health bar because i think it's just good to know how to make health bars and whatnot so all right so the next thing we want to do is mess with the panel so the first thing i'm going to do is change my source image now right now it's selected as some type of background image that's probably built in but that's okay we don't want to change that what i'm going to do is select this square but if you don't have that square which i'm sure if i cover that with you before but basically i'm going to select this scor i'm going to select this square and this square is just a texture type and it's a sprite and it's a 2d or ui sprite and so to change it you would just select it because i think when you import it it's going to come in as like default or something like that so just change that to do 2d ui and then go down here and select apply all right and we'll probably come across a lot of this type of stuff in the future when we're messing with like textures and all types of stuff but for now let's just keep it real basic all right cool so that's our panel i'm gonna make my panel green and i'm gonna make it completely solid just like so all right so that's gonna be that there all right and i'm going to change the image type to field and i'm going to change the radial to horizontal and i think i'll leave the fill origin to be left for now we'll we'll change it if we need to i have a feeling we might need to change it but we'll see all right cool all right so so far so good so let me just go ahead and name this panel and i'm going to call it the h h bar right and so that's going to be our health bar and so what i'm going to do is click on the health right here and i'm going to add a attribute ui component and just like before we're going to change this so the first thing we want to change is the target so we're going to change it to game object and we're going to move the character right here the parent into that slot and then we're going to select the attribute that we want to affect so it's going to be the health all right and so now what we're going to do is we're not going to be using any icons or anything like that here so the only thing we really need to put in here is the fill value okay so i'm going to change this and move this panel h bar i'm going to move that to the fill image okay and then i'll leave the smooth transition all this stuff i'm going to leave this kind of default let's just make sure everything is working properly all right cool all right so now that we have the enemy set up we need to reduce the enemy's health and to do that our player needs to be set up all right so to do that we need to ensure that the player's weapon is going to be these particular ones here so if you select your weapon on your player so i'm doing that by going to the player and i'm going to draw a weapon and then to actions and then you can just see what weapon your player has and then you just select it and of course when you select something here it shows up in the project folder now if you have a different weapon that's fine but i would suggest moving your weapon into the folder that you can get to i kind of was messing with this a little bit already so i have these listed here but if you don't just go ahead and create your weapons or duplicate your weapons and move them into like your character's folder all right so i'm going to select this base weapon temp and just make sure it's the one that we're going to be utilizing in the base weapon shield and then in terms of the attacks so if i select the weapon and you can see here what we need to do is change the clips the melee clips okay so what i've already done actually for my weapon is get rid of all of those attacks we previously had and so what you would want to do is select one of the clips and you can actually just reduce it by removing it from the selection here just like so so for instance if i want to add one i can just hit the add button and if i want to subtract it you can select it and then hit minus for me what i want to do is just make sure that i only have three attacks for this player now in the future we can have different attacks where maybe if he's in the air he can you know hit down if you hit forward those types of things but i think right now i'm thinking just more simple so i want to just make sure they have at least the three basic attacks now if you're using the previous attacks the ones that were built in with the game creator examples what you're probably going to want to do is duplicate those now if you remember the only way to duplicate a melee clip is to actually select the melee clip and go to where it says duplicate when you hit duplicate it's going to wait a second and then it's going to create that duplicate that's going to be the proper way to do it okay so go ahead and do that and then eventually move those clips into a separate folder so we can have our own unique clips all right i'm going to go ahead and delete this one all right cool all right so we have our weapon we have our clips here so let's go ahead and actually change the attack all right so my attack one on hit that's where we're going to reduce the enemy's health so what i'm gonna do is go ahead and hit add action and i'm going to go to stats change attribute and what i'm going to do is change this from player to evoker and i'm going to change this to health and of course the operation is going to be subtract and now we want to subtract health from this enemy cool so what we're going to do is make this go to [Music] i don't know we can do something like 40 i don't know maybe like 30. now let's let's go like 20. all right cool we do 20 all right so for the first hit we can do 20 and then we'll select the next attack and if you want you can actually select an action and hit this button here and you can actually copy it temporarily we're going to the next attack and we'll just hit paste and so it have that information there cool and what can make this attack to be maybe a little less so the first attack if you hit is going to be 20 maybe the second one can be 10 and the third one can be five or even higher it just depends all right so i'm gonna copy this one and then i'm gonna paste it and then i'll just do five all right cool all right so first tack is gonna be worth 20 10 5. all right cool all right so far so good so now let's actually test to see if this works let me hit play alright so right off the bat there's something wrong we're missing the health bar so let's verify what's going on all right so we're missing our health bar and that's okay so let's take a look at the actual enemy and see if we can troubleshoot this right now so i'm going to select the health of the enemy here all right and i'm looking and seeing what's going on so everything looks fine here let's select our enemy and see if there's anything missing ah yes there is so our enemy the reason why the health is not showing is because the enemy or the all these different attributes they don't know where to pull the health from the enemy currently doesn't have any stats so we have to add some stats to our enemy for that to work so we simply gotta go to add component directly on the character enemy here the game object go to add component we're gonna type in the word stat it's going to be stats um ui i believe actually no it won't be stats ui it's going to be simply stats so right below it great and so these are going to be the stats here now if we want to change the health of this particular enemy we can do that we can change the max health right now it's base it's a base value but we can change that and say we want this guy to be 75 instead of 100 or 50 or something else so for now let's just say it's 150 right so i'll put it 150 and let's verify that this is working all right so you can see the green bars there so i'm going to walk up and hit this guy all right and i think it's reducing it looks like it is a little bit i'm just going to keep hitting them until it's gone down and as you can see the green bar is definitely reducing and that's nice all right all right until it's gone right all right so and as you can see here as well the stats is reducing here as well all right great so that's working properly but what i want to do with this is i want to add a trail to it because i want to kind of see it kind of reduce as it's going down so what i'll do is go ahead and go to the panel um h bar i'm going to do a control um let's do a control d and then i'm just going to go hbar trail and then i'm going to move it above it and then we're going to change the color to red just like so all right and then what we want to do is go actually on this hbar trail we want to add another component and what we want to do is add a attribute component and it's going to be the attribute ui it's pretty much the same one we're using for health but um all we're doing is using this to kind of display like an effect when like when you lose health it's going to kind of reducing so um what i'm going to do is drag this into the fill image area and then i'm going to do a smooth transition down okay and let's just see how that looks so i'm going to go ahead and hit play actually before i hit play is there anything that i'm missing yep missing this right here so we need to make sure the target is set so we're going to select the game object and we're going to move the enemy into there and we're going to set it to health perfect so that should work all right so let's take a look all right so i'm going to go ahead and hit the enemy all right so it looks like it is reducing um it's just not reducing the direction that we want but that's okay because i think i have a pretty good fix for that here in a moment all right so but it's definitely reducing and if you notice that there's a red delay if i get the camera straight we'll definitely be looking at switching our camera up soon as you can see there's a red delay and it's a nice little effect so i kind of like that alright cool all right perfect all right so the next thing that i want to do is make sure that this health is facing the camera at all times and there is a way to do that it's called billboard so billboard i don't know all the technical ways or how it works specifically but it's basically getting the camera it's facing whatever the direction the camera is facing at all times so to do that what i'm going to do is show you a website which is game creators website and they have something called the hub game creator has this site and it's called the hub and it's really awesome because a lot of different game creator users like myself they generally will create additional actions and additional things like additional triggers and all types of just very useful things that game creator itself doesn't already come with so people will create them and they'll put them on this hub place for you to download now there's all types of things here you can search for it but as you can see there's something called like this updates and you can explore the different categories and these ones here are just the most recently updated or at least the most recently uh uploaded to the website and you can explore the categories this way so as you can see there's different categories for actions conditions modules and triggers all right and i have a couple up here but the one that we're going to use is going to be called simple billboard ui now if for any reason of course i recently put this up here but um if it's not gonna i'm sure if you're looking at this a year from now or whatever it may not be listed right here on the front so what you can do is hit up the search area here and type in billboard and as soon as you even type in the word bill it's going to start putting it right here so you just select it and as you can see we have billboard here so all you would need to do is actually just hit the download button and once you download it to your computer what we would do is put that in our game project now you can put it in any particular project that you actually want or any particular folder that you want so i would recommend creating a folder called scripts and putting those directly in that folder there okay all right so let's take a look back at our project so now in my project i already have this set so what i'm going to do is go to my scripts folder and as you can see i have it listed here all right so all you need to do is leave it here there's nothing else for you to do all right so at that point now what we need to do is head over to our actual health here this particular um health here and we can select canvas i believe we can either select canvas or health i'm going to select canvas and i'm going to add a trigger component and then what i'm going to do is select change trigger and then i'm going to select at the bottom where it says custom ui and it's going to have billboard script all right great so now all we need to do is select this for the main camera we're going to move our main camera and put it in here just like so perfect all right so there's our main camera all right and so now we should it should automatically face whatever camera you put here now you don't have to use any actions or anything this is actually a pretty simple script it could have been used as just a simple script without it connecting and using like triggers but i wanted to do it this way so people who are using game creator maybe for the first time and they're not familiar with how scripts work but this is an easy way to approach it now let's go ahead and hit play and let's see if it's actually working so if i turn the camera as you can see the the health bar is facing me no matter what so that's cool and that's kind of the effect that i wanted to go for um with this all right great so now if i were to attack him you see all right cool and so now we're good to go perfect all right so now that that's working the way we want let's go ahead and do one more thing so with this we have created a health bar for our enemy we've created a or added a billboard script so that it faces the camera now what i don't want however is the health to always be displayed no matter where they are in the game we don't want a bunch of little health bars kind of just floating all over the place so what if we kind of thought it through and said well we want the player to when he gets close to the enemy for the health bar to turn on and when he gets away from the enemy it turns off and we can do that we can use some simple logic to do that so just think it through so we need something to trigger with the player is going to be the the object that triggers that so for the enemy itself so we're going to select our enemy and what we're going to do is do this as a kind of like a mechanic i'm going to right click inside on this folder i'm going to go to game creator and i'm going to go trigger and i'm going to rename this to display health okay all right all right so now we have a trigger called display health and so what i want to do with this is i'm going to go here where it says trigger and i'm going to go change trigger and we're going to go object and we're going to do on player enter alright so when the player enters this trigger it's going to do something all right and then i'm going to add another component called trigger and we're going to do this one but we're going to do when the player exits it's going to do something all right cool all right so on the first one i'm going to hit this here where it says it's a little icon for actions i'm going to hit that and of course it's going to create automatically um it's going to create a thing for us here and where is it actually located and i think i renamed it so let's make sure that this is named properly we wanted this called display health okay so display not to be confused with the actual health all right and so we have our actions and i typically like to get rid of these numbers so i'll just leave that as actions all right cool all right so um what i'll do now is display health so i'm gonna go add action i'm gonna go set active all right and we're going to select the health here and we're going to move that into there all right so we need that to be active um when he enters when the player enters into the trigger of the enemy all right cool all right so i'm gonna go back to my top trigger and i'm gonna do the same thing here i'm gonna create another action and what i'm gonna name this actions is on and off so on and then this one will be called off all right and then this one i'm just going to again set active we're going to take the health from the dim here move that there and we're going to make it not active all right cool all right so the only thing we need to do now is test to see that it works now in order for us to make sure it does work i'm gonna go ahead and actually make the health um turn off so i'm gonna go health just like that all right great all right so i'm gonna go play and let's see what happens all right and so nothing happened and you're probably wondering why and the reason is because i forgot to increase the trigger size so if you remember i created a trigger here and we need something to trigger the enemy and it's too small for the enemy to actually come inside of that so what i'm going to do is increase the radius of this to something like like three right um we can do something like three and then i'll go ahead and hit play and see if that is actually working all right so we walk over as you can see it actually is displaying the enemy's health when we come close and when we get out of it great and if we attack it's still attacking our enemy and if we get out everything is good to go perfect all right so that's not bad at all all right so that is actually pretty good so what we've able to do is we have our health bar we set that up for our enemy to actually reduce the health we set up the billboard for the ui of that health bar so it's always facing the camera and then we have the trigger for the displaying of the health bar so next we just need to do that for the spider and since we did a lot of the work already let's go ahead and take a look at that so what i'm going to go ahead and do is copy ctrl d this so we're going to copy the health and we're going to copy this actual display health trigger so i'm going to do a ctrl d for those and i'm going to go over to my spider and put that in the same location all right so i'm going to put that maybe like here and i'll move this below though so it's consistent all right cool all right so we have our spider so the first thing i'm going to do is move it over to where the spider is basically reposition everything so i'm going to select both of those and i'm going to move that over here like so all right great next i'm going to go ahead and change the health location because the spider is a bigger creature so i'm going to move that upwards just like that awesome all right cool all right and then next of course i'm going to [Music] change that to health and change this get rid of the one on that as well perfect now what we want to do is go into health and actually change a few things so the first one we want to select health we need to change out the target because remember this is going to pull the stats from a particular object and we don't want it to pull the stats from the little red guy here so um let's do that so what we want to do is move the spider into that slot we also want to change i think the h the bar because we duplicated i think that is going to carry over so we can test that and then also for the p trail we want to make sure that that is set as well so i'm going to move the spider into there perfect and then we need to give the spider some stats okay so i'm just going to type in stats and we're just going to select our stats go to go and then we can also change the health max for the spider bigger object so let's just give it 200 for now great and i think that's good to go because we do have the canvas we do have the billboard everything is still there um i think we're good to go so let's go ahead and take a look and see if that is working as expected so i'm going to go ahead play and of course there's one thing i want to do is change the health so remember when we turned the health on we need to have that turned off from the initial state there and there is a little trick we can actually do for that so but we can play with that later alright so as you can see we come into that space of them all right and now since the spider is bigger we can actually change the radius for the spider because the spider is so much bigger but i don't want to get into those details just yet so i just want to leave that as it is so let's go ahead and get close to the spider and reduce the health i missed him completely all right let's see and we're going to deal with this camera soon so all right so we are definitely reducing the health of the spider great and we're also our health is reducing so that's great all right so hope that was easy enough basically we spent the time in health bar change we made the health bar work for our enemies we got the billboard working for the ui for the enemies and the trigger for the enemies as well now some of these can change where we don't know but this is just some basic functions i think that are super useful all right all right so i'll see you guys in the next video we'll probably touch on um some other aspects of of the enemy maybe we're gonna work on maybe the the dying mechanic for them or maybe we can start thinking about cameras and things like that all right all right i'll see you guys in the next video hey guys thanks for tuning in to stay up to date on the latest 3d platforming tutorial feel free to subscribe to the channel if you'd like to support you can find me on patreon or of course you can hit me up on discord i like to talk about whatever project you guys are working on of course thanks for hanging with me your support is always appreciated as always remember never give up and keep moving forward peace
Info
Channel: Indie Game Hustle
Views: 973
Rating: undefined out of 5
Keywords: Unity, Game Creator, Tutorial, Tutorials
Id: I4gS8vT5feo
Channel Id: undefined
Length: 32min 23sec (1943 seconds)
Published: Mon Sep 07 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.