What you will learn
In this Unity tutorial, we will learn how to effectively utilize the game engine. Over the tutorial, we will learn how to use the Unity editor interface as well as how to navigate in the scene view. From there, we will learn how to import assets and how to create prefabs. Then we will shape our level even more by adding character controllers, colliders, lights, materials and physics. We will then finish the tutorial by learning how to use scripting in order to make our level interactive.
In this lesson we're going to learn how to declare another variable to help us control the rate of health lost. So we've got our health removal system working correctly. However we don't know what the multiplier should be. Now of course we can come in here, change the value, save this file, to jump back to Unity. Wait for it to reload the script. Hit Play. Wait until we run into the fire. See how that goes. Now if it's still too slow. Go back, we change it again. Say and so forth and so on. So this is obviously a very slow and tedious pipeline. What would be better is if we created a new variable to store this change so that we could change it inside of Unity while it's running. So let's just declare a new variable. So let's call it var, and we can call it damage rate, or health change, or we could really name this anything. Of course, we want to give it some name that is descriptive of what the number is doing. So let's say damage rate. And we might even say fire if we have different damage rates for different things. And let's cast this into a flow. So I'm going to put colon flow equals-- I don't know what it should be. Let's just put in, let's say, three for now. And end it with a semicolon. Now we have a new variable that we can modify inside of Unity. Of course we need to use this inside of our expression, or our equation here. So let's copy damage rate. And replace three with damage rate. Hit Save. And now we can jump back into Unity and actually change it inside of Unity. And this is something that we haven't actually taken a look at yet. Which is one of the best features of scripting in Unity. Let's go to where our script lives. And that would be our first person controller. Sorry, I just want to clear my hierarchy a little bit. And if we scroll down, we can see we actually have access to or variables here inside of the Inspector. So we can change the amount of health we start with and we can change our damage rate on the fly. So let's say we want to start with 110 health, and hey, well let's just try damage rate of 14. See what happens. So lets hit Play. And now our health will appear as whatever is stored in the Inspector. And now let's test this damage rate of 14. It's a little slow, so let's actually just Control Shift P to pause. And we can now modify this on the fly, un-pause, and then test that out. And that seems a little bit better. OK so let's try 20. so I'm going to un-pause. And of course our damage rate will reset. But now we can try 20. And you can continue to do this as long as you need to test this. So maybe even this isn't fast enough. So we can hit Pause again, Control Shift P. And let's try something like 30. And so you can see here we can modify the variables inside of our script while we're actually playing the game. And this is an incredibly powerful feature of Unity, being able to dial into the exact value that you want while you're playing the game completely removes that traditional pipeline of changing the script, re-compiling the game. Redoing everything and then testing your value. By pausing the game and just coming in and changing this, we can come in and make many, many tests very quickly and easily until finally deciding on the best number. So I'm going to a damage rate of 30 looks pretty good here. And of course, depending on your game, this might be a different rate. So lets just try that one last time. We see here that we can spend a little bit of time in the fire. We can probably spend, let's see, 30-- I think we can spend around four seconds in fire with 110 health. Now there's one more take away from this lesson before we're going to jump ahead. Now you'll notice the health and damage rate values in my Inspector. 110 and 30. Now if we jump back to our mono, you can see those are still 103. Now this is so that we can change these values on a specific game object. So this component of this character's state script has been modified to these values. If we copy our character's state onto another object, it will start with whatever values are inside of the script. So if we always want our objects to start with 110 and a 30 damage rate, we change that in the script. However, if we already have an object-- or if we already have the script component on a game object, no matter what we type here will not be reflected. Now it'll be reflected the first time we create a variable. But you'll see here we've updated the script. But all of our script, or all of our Inspector values have not changed. That's because the script never tells the Inspector what to do. It only tells the Inspector what the initial state is when we first copy that script onto a new object. So if we copy it now, you'll see the new default values. I'm just going to go in and remove those scripts from those various other components here. We don't really need them on our doors and our floors. But that's a very important part of scripting in Unity is that we can come in and modify our default values here inside of Unity without actually editing the script. And that is exactly what we can see with all of these script sensitivity, the character motor, all of these are variables that we have access to to change how our script works in Unity. OK great. So now we have our health loss correct. So now let's take a look at how we can create a custom function to do a few lines of code that happens whenever our character needs to die.