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 our variables to a specific type to remove rounding errors and other things that happen when we have implicit declaration. Now, just to re-frame the problem that we're dealing with here, if we take a look, we have health of 100, and we are trying to remove one health every second. But if we come into and play our game, we can see that our health lowers much, much faster. And then, we get stuck at zero for some reason. So what is happening? Well, I always like to begin these kind of bug explorations by printing out various things. So we're already printing out our health, and we can see that. So now, let's take a look at what deltaTime is giving us because that will be important. So let's print health. Or, let's replace health with deltaTime. Save that and re-run this to see what deltaTime is actually giving us. So let's run into the fire, and let's hit Control Shift C to bring up our console. And we can see that deltaTime is giving us a value of 0.02, which is approximately 50 frames per second. So what's happening is we're getting a fraction or a decimal, and then we're multiplying 1 by this decimal. However, there are essentially two types of numbers in JavaScript or in this programming language. There are integers, and there are floats, or floating point numbers. Now, as we saw earlier with our GUI box quite a few lessons ago, health is an integer. So JavaScript has implicit typing which means it implies the type, which means that it takes a look at what we assigned to it, and then it takes an educated guess what kind of number this is. Now, in this case, it's actually guessing wrong because we want to be able to get into the decimal range of our health. For example, 99.8 is a valid health value because we do not want to change the health based on the whole numeral increments. Now, integers are always whole numbers. So if we try plugging in something like 99.8 into our health, it simply rounds it either up or down. So what's happening is we're essentially saying, health is equal to health minus this 0.02. And when that happens, the scripting language is saying, you know what? I can't put 99.98 into an integer. So I'm just going to round it down to 99. And then, on the next frame, it says, I can't put in this value because it has a decimal place. And this is a very common problem when using implicit typing or assuming that JavaScript does what it's going to do correctly and that it will read our mind in what we want to do with this. Now, there's a few ways we can change this. So one is we can implicitly force JavaScript to see this as a floating point number. And we can do that very easily by putting in a period and a 0 key, or period and any number. And this period here and the decimal place tells JavaScript that, hey, this is now a floating point variable, or this is a floating point value because it has a point that can be anywhere or float around our number. And we need to say health is going to be floating point. So again, this is method one, and this is simply tricking or telling JavaScript a little bit more about our variable information or our variable number so that it assumes it's a float. So now that we've fixed that with this method, let's come back here, hit Play, and take a look at our health. Now, we are getting the correct decimals inside of our health. Now, it doesn't look very pretty because we see the entire floating point value. But we are now successfully getting the one second health loss or the one health per second loss. So you can see here, 81, 80, 79, and so forth. So now that we've implicitly created or changed it to floating, it works. However, this is not my preferred method of declaring a variable type because sometimes you might just forget to put a period here, or you might accidentally type 1,000 instead of 100. So instead of actually tricking JavaScript into making a variable a float, I would rather implicitly tell it this variable is of type float. Now, we actually already saw how we can do this, and we saw that with our OnTriggerStay arguments. Remember, we said this object is of type collider. Now, we can do this with variables, too. So we can say health is a variable type of-- and we have decimal, double, float, integer, long, so on, and so forth. There's a lot of different types of numbers here. But in this case, let's just put in a float. And that will also force JavaScript to declare this variable as a floating point format. Now, this is going to increase memory usage a little bit. So learn about the different types of numbers and when it's appropriate to use them. But if you do not explicitly declare your variable types, you're going to run into these very difficult to debug situations when numbers are not working as you expect they are because we're assuming JavaScript is reading our minds and understanding what we want to do with this information. I always prefer typing my variables as soon as they're created so that I know, hey, this is 100, and it will be a floating point. Or, I could be an integer. But in this case, we want float. So once again, let's save that, jump back here, and hit Play. And again, this is not really going to change anything. We will still get the exact same display issue we're having. So let's now look up a way to clean up our GUI. So let's go back here. And we're currently displaying this floating point as the decimal version. Now, it would be great if we could around this up to a whole number, or convert it into an integer, or just show the whole number equivalent. So again, I'm going to jump back to the Unity scripting. And let's see here. We want to round this number up. OK, so the first result is Mathf.Round, and this will always around it up. So if our health is 10.2, it will now print 11. So let's now surround our health with this Mathf.Round function. So to do that, let's go back here and just type in capital M-A-T, and I'm just going to hit down to auto-complete, Math.f, and we want the .Round function. And this is a capital R. These functions and their function names are case sensitive. So sometimes, that's a big tripping point. A little "r" will not work. And then, opened and closed parentheses surrounding our health. And you can put a space here or have no space. It makes no difference. Now if we save this and jump back to Unity, we should see our usual display happening. And we are correctly losing one health per second. There we are. We are correctly displaying and rounding our information upwards. And we are losing one health a second regardless of our frame rate. So now, in the next lesson, let's take a look at how we can add a variable to help us control, test, and figure out what the proper health loss number actually is because I don't think losing one health per second is very scary or very good game play. If you can spend 100 seconds in the middle of a fire and still run around, that's obviously not realistic. So in the next lesson, we'll take a look at some strategies for testing and setting up variables to change things in Unity.