STATS - Making an RPG in Unity (E09)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we'll be making a system for character stats I'll make it easy to define stuff like health armor and damage and to have our equipment modify our stats also implement a method for taking damage that take these stats into consideration also if you didn't know so best in the previous video in the series so you can go to this channel to check that out let's get into it so as we can see it's better to set up some of the graphics for equipment we now start with some default wear and if we pick up some of the items here on the ground they will now appear in our inventory with the correct sprites and we can then equip them on to our character but currently the items have no effect on gameplay if we look under the items folder we can go under objects and here we have all the scriptable objects for our armor we'll see that each one has a armor and a damage modifier and the same thing for our default where I'm actually gonna select all of our default wear and set both of those to zero but we're currently not using these modifiers in any way to do that let's go under our scripts folder let's right-click go create folder and let's create one for all of our stats in here we'll create a script called character stats this will be responsible for keeping track of all the stats on all characters in the game and we'll make it in such a way that we can derive from it in order to create custom logic for our player and our enemies we'll also create another c-sharp script and this is just going to be called stat this will be afraid the symbol object representing a single stat such as armor or damage also just notice that I want to go out of debug mode in my inspector here let's begin by opening up our stat script let's the leader two methods and we don't want to derive from monobehaviour instead we want to go ahead and mark this class a system dot serializable remember we do this whenever we want unity to serialize a custom class which means that the fields inside of this class will show up in the inspector will then create a public integer called base value as the name suggests this will be the starting value for the given stat we do want this to show in the inspector but that's actually no reason for this to be public because we don't want to be able to access this directly this should only be set inside of the inspector and then when we want to get the value for us that we want to do that using a method so that we make sure to always apply modifiers before returning the value so we'll create a simple method called get value that returns an int to do that we go public int get value and right now we don't have any modifiers so we can simply return base value but we'll add those in a sec and now we don't need this to be public anymore so we can mark this as private and then in order to still make this editable in the inspector will mark it as serialize field if we now save this and go into our character stats here let's remove the two namespaces at the top and the two methods and now we can create a public stat called say damage let's also create one for armor if we then save this go into unity select our player and drag our character stats onto here we can see that we now have these two stats under which we can edit the base value awesome so each character now has a damage and armor value but we also want each character to have some health amount you could have this be a stat as well but I actually don't want to be able to add modifiers to health so simply keep this as an integer we call it max health and we'll just default it to say 100 we'll also create a public int which we'll call the current health and we'll make this a property of type get and then private set what this means is that any other class will be able to get this value but we can only set this value from within this class so for now create and awake method and in here we set current health equal to max health that's totally fine however if we try to do this from another class it wouldn't let us the next thing that we want all characters to be able to do is take damage so we'll create a public void called take damage and this will take in a damage and mount the simplest way to apply damage to her character is by simply subtracting it from our current health so we'll say current health minus equals damage and we can maybe also print out a message saying what happened in the console so we'll go deeper deadlock he will print out transform name then we'll say takes and then our damage amount of damage then we want to check if our current health has reached or gone below zero if it has we want the character to die to the go if current health is less than or equal to zero well then we want to call some kind of die method and how the character dies is completely going to depend on what character we're talking about we probably want enemies to either play a death animation or turn into physics ragdolls we might also want them to drop loot or trigger a cutscene but for the player we probably want some kind of game over screen or some way to respawn so the best way to do this is actually just make a public virtual void called die that we can then override for both the enemy and the player so here we want to die in some way this method is meant to be overwritten and then we can maybe just throw in a debug the LARC statement saying that transform that name died but our damage calculation is currently super simple let's utilize the fact that we have an armor stat and use this to lessen the damage in other words we can simply take our damage and subtract our armor stat remember to get the value of this type we go dot get value of course we might get a case here where we have a large amount of armor than incoming damage and that means that we simply want nothing to happen but right now this would actually make our damage negative and when subtracting with a negative number the result is positive which means that if our armor value is currently greater than the damage we would actually be healed and we don't want that to avoid that we'll make sure to clamp our damage between zero and int max value so now our damage should never go below zero so now we have a structure in place for taking damage but we currently don't have any combat let's go in here and add an update method and we're gonna add a quick test in here where we check if we press the T key T for tests and if we do well then we want to take some damage and we'll just set the damage amount here to say ten now if we save that go into unity and hit play will also switch into debug mode we can see that our current health is currently 100 if we then press the T key it says player takes 10 damage and the current health is now reduced to 90 if we bump up the base value for our armor let's just set it to 5 and then try and do the same thing we can see that it says player takes 5 damage and now the health is only reduced to 85 so already our system is working but our damage and armor values are still unaffected by equipment to change that let's get rid of our character stats and instead create a new C sharp script called player stats we can then derive player stats from character stats and here we can use the fact that in our equipment manager equipment manager dot instance we put in a callback method for whenever a new item was equipped we call this on equipment changed we can now create our own method inside of this class and subscribe it to this callback let's call this method on equipment changed let's go ahead and create it down here void on equipment changed and this is going to take in a piece of equipment which is the new item and a piece of equipment called the old item just as we defined it inside of the equipment manager in order to apply these modifiers let's go into our stat and here we can create a list of all of the modifiers currently on this stat let's create a private list of integers and we'll call it modifiers a by default we'll set it equal to a new list of integers then we can create a public void add modifier which takes in an integer called modifier here we want to check if the modifier is not equal to zero in which case we want to add this to the modifiers list we do that by calling modifier stud ad and feeding in the modifier that we want to add and we'll do the exact same thing for removing modifiers let's create a public void called remove modifier this also takes in an integer called modifier here we also check if modifier is not equal to null in which case we'll call modifiers dot remove and feed it the modifier passed in if we save this now and go into our player stats we then go armored dot add modifier new item dot our my modifier and the same thing for damaged damaged stud add modifier new item dot damage modifier and of course we only want to do this if our new item is not equal to null and we can do the exact same thing for our old item but in Reverse so if old item is not equal to null with we want to go armored remove modifier old item dot armor modifier and damage don't remove modifier old item dot damage modifier so now every time an item gets equipped this method gets called and we make sure to add and remove the appropriate modifiers if we now save this go into unity make sure that we're in debug mode in the inspector select our player drag in our new player stat script we can give the damage here base value of say 5 and just leave the armor at a base value of zero we should then be able to hit play see that we currently have no modifiers for the damage or armor if we then pick up our equipment here and we then start to equip it let's equip the helmet first we can see now that a modifier of 1 gets added to our damage and a modifier of 2 gets added to our armor and it will keep on adding more modifiers as we equip more items so now with all three items equipped you can see the different modifiers that are now added to our armor and damage and just to make sure this is working we can see that our armor has a base value of zero but it has two modifiers one of +2 and one of +3 so all our incoming damage should now be reduced by 5 so if we take 10 damage we should only subtract 5 health but if I press T it still says player takes 10 damage the reason for this is that inside of our stat when getting the value we are currently only returning the base value in here we need to create an integer called final value and we'll set this equal to base value will then need to loop through all of the values in our modifiers list and add them to our final value a quick way to do this is using modifiers dot for each so for each element in our modifiers list and we'll call the given element x will then want to add X onto the final values of final value plus equals X and then instead of returning the base value we want to return final value let's say that going to unity hit play we'll make sure to equip all of our items so again we have an armor of 5 and when I now press T the player only takes 5 damage and that should be it for player stats let's pretty much it for this video if you enjoyed it make sure to subscribe so don't miss the future one also if you haven't checked out the Mayan temple pack that we recently released on dev assets I definitely recommend you do sir I would love to see an RPG with that theme and in general I would like to see more of what you are doing so if you've been making a game using these tutorials or maybe one of the packs on dev assets definitely treat me some pictures at brackets we are really excited to see what you guys are doing on that thanks for watching and I will see you in the next video thanks to all the awesome patreon supporters who donated in August and a special thanks to hence off-tune yes pamekasan Thomas Wally stone gamer Sybok Nami chase nate'd Oh Derek heaps Kirk face Tamara with some casa kudamon Erin Robert punt and Peter lock if your name's not on the list I will make sure to include it in videos later this month and the next month as well you guys Rock
Info
Channel: Brackeys
Views: 148,049
Rating: undefined out of 5
Keywords: brackeys, unity, unity3d, asset, assets, beginner, easy, how, to, howto, learn, course, series, tutorial, tutorials, game, development, develop, games, programming, coding, basic, basics, C#, character, characters, player, stats, equipment, modifiers, code, RPG, collab, Sebastian Lague, role playing game, characteristics, values, stat, changing, armor, damage, health, hp, life, protection
Id: e8GmfoaOB4Y
Channel Id: undefined
Length: 11min 42sec (702 seconds)
Published: Wed Sep 06 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.