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 use deltaTime to control our health loss so that our health change is not based on the frames per second, but based on how many seconds have actually elapsed. Now, this is a little bit of a subtle problem. If we hit Play and if we move into our fire, you'll notice that our health lowers very, very quickly. Now, to see how quickly this is lowering, I'm going to hit Control Shift P to pause. And then, in my game tab, let's jump down and turn on statistics. So you'll notice here I'm running this game at around 56 FPS, so frames per second. Now currently, with our current script, our health is being subtracted for every frame that we are inside of this trigger. So if you're running this game on a slower computer, and you're only getting, let's say, 10 FPS, you only lose health at a rate of 10 health per second. Conversely, if you're running this on a supercomputer and you have 120 FPS, you will be losing health faster than we intend. Now, of course, having the game play change based on the type of hardware the customer's using is a very bad decision. So let's take a look at how we can access the actual time inside of Unity to make sure that we are basing our calculations off of time instead of frames per second. So here we are, and we need to control our health with the time attribute. So let's go back to our scripting reference, and this time, let's just search for "time." Let's hit Enter. We can see that time is the interface to get time information from Unity, so let's click that. And let's just read a few of these. And here, we have one that is the time in seconds it took to complete the last frame. Hm, that looks like what we're looking for. But let's click it and see. So here, we can see that this function-- we can use this function to make the game frame-rate independent. So instead of saying 10 meters per frame or one health per frame, we can use this to say, one the health per second. So we can do that by multiplying time.deltaTime by our number. So let's add this into Unity. Go back to Unity here. Go back to mono. And now, let's do health minus equals 1 times time. And then, you'll see here I miscapitalized, so I'm going to hit down, and hit Enter. And then, dot, and then hit down or just start typing in delta, and hit Enter. So now, we are multiplying our health subtractor by the number of seconds it took to render the last frame. So what we're saying here is that no matter what your frame rate is, you will lose health at the same rate. So let's now save this, Control S, and jump back to Unity and see this working. So I'm going to turn off my stats here and un-click play, and then click play again. And again, just run into the fire here. And we can see our health lowering at a more consistent rate. However, what we're getting is the health is lowering far too quickly, and we're getting stuck at 0 for some reason. Now, if you looked at the documentation and deltaTime is supposed to be whatever is multiplied per second. So we should be losing one health per second. But in this case, we're losing health at a much, much faster rate. So to fix this, we're going to actually need to take a look at variable types, number types, and we're going to have to recast our variable and declare it as a different type of number. Now, since that's getting into something else, let's jump into that in the next lesson. So in this lesson, we took a look at how we can use the time.deltaTime variable or function to help us tie our numbers or tie our calculations to the actual number of seconds that have changed in our game as opposed to the frame rate. So this is very important to add into any kind of multiplication or addition or subtraction so that the calculations happen on a second level as opposed to a per-frame level. Now, of course, this isn't for everything. So learning when to use a multiplier by deltaTime and when not to is another important thing when we're programming Unity games. So now that we've got that done, let's begin exploring the strange behavior of our health.