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 create a custom function to run several commands at once whenever we want. Now, so far, we've just accepted the fact that our player health can go below zero. But in a real game, we would want something to happen when our health hits zero. We would want the character to die. Now, we could put all of that quote unquote die code such as resetting the player health, resetting its position wherever we want. For example, in the update function we could have something that says if the health-- so let's just type this in here-- if and then the health is less than zero, less than is above the comma key, close parentheses, open bracket, close bracket, so if health is less than zero, let's do something. Let's just start out by printing you're dead. So let's put print, open, open parenthesis, quote mark, player is dead. And then, let's remove our other print here just so we don't have 1,000 things appearing inside of our console. So I'm just going to delete that. And let's hit Ctrl+S to save. And so our console will say our player has died. So I'm going to hit Ctrl+Shift+C to bring up my console, Hit Play. And let's come over here and die. So we'll sit in the fire until our player health is less than zero. And now we are saying the player is dead. Now, of course, we actually need to do quite a few things when the player dies. We need to reset the health. We need to reset its position. We need to reset the orientation. We might need to take away something from a player if that's the part of our game. So we need many lines of code inside of this if statement. Now imagine if we had many different types of these commands or many different instances when the player could die. For example, if the player ever leaves the level or falls through the level, we would probably want them to die too. Now the result of that is many of these if statements with a lot of very copied code. So instead of actually copying and pasting the code for every single instance of when the player needs to die, let's actually create a new function that handles all of that for us. Now just to see this in play, let's actually test if the player is outside of the level. So the code for finding out a position, we first need to find out what game object we're going to access. Now since we know that this character state is our controller, we can type in the object this which simply gathers the game object that this script is connected to. So now let's hit period, and we defined the transform of this. So I'm going to type in transform. And then, we need to hit dot. And the transformations are all the positions, rotations, and scales. So we need to find the position. So let's type in position. I'm just going to hit down twice. And then, we want the position.y. So this is saying whatever object this is connected to, get the transform, the position, and the y-value. If this is less than zero, let's do something. So again, we'll just copy/paste our code, which, again, we really do not want to do. So we'll close that bracket there. Open that and close that. Hit Save. And let's come back to Unity and test this out. So we'll go back here. And just to make this a little easier, let's actually take one of our walls and turn off the collider. So let's just select one of these walls, turn off its collider, and hit Play so that we can actually just run through a local wall and fall. And we can see as soon as our x-value for our player, and we can actually see this if we select our player while we're falling, if we select our first person control, you could see the y-value is negative. So the player is dead. So as you can see, there could be a lot of different conditions when we need to kill or reset the player. So instead of copying and pasting this code whenever we need to run it, we need to put it in our own function. So here is how we can create our own function that will run some code. Of course, we need to begin by typing in function. After that, we need to type in what this function is going to be called. Now since we're going to be killing our player or resetting our player, let's call it reset. And now, we have to tell the script whether or not it's going to take in an argument. You can see here in function update has no arguments. However, other functions like OnTriggerStay has an argument that is passed into it. Now for a character reset, we don't really need any arguments. So let's do an open and close parentheses. After that, we need to say what part of the code this function is covering. So like we do with our if statements. We need open curly bracket and a closed curly bracket. So now, we can take a repetitive code, such as this print player is dead, and hit Ctrl+X, paste this in here, and now we can replace that with just calling function reset. So let's just type reset and so that our script knows it's a function. We need to do the open and close bracket and a semicolon just as if we were running something else, like this rectangle here is a function that has four arguments, these four numbers. So our reset has zero arguments. So we can just copy and paste this wherever we want this to run. So with that done, let's hit Ctrl+S to double check that our reset function is working correctly. I don't see any console errors, so that's usually a good sign. And now, let's just run out of the level again. And we can see our console is printing that out. Now, of course, when we reset our player, we don't really want to set the console to be saying something. We actually just want to reset the player. So let's now take a look at some code that we can put inside this function that will reset the player. So to begin with, let's reset the health. And let's just say health equals 180. This will be the assumption that we make. Actually, instead of this, let's create a new variable that we initialize or set as soon as the script starts to run. So let's just add a new new variable. So variable, let's call this our start health, because remember we can change it inside of our inspector. So we want to make sure that we gather whatever the inspector value is as opposed to hard coding this value in here. So let's say our start health, colon, which is a float. And then, let's drop in a new awake function, awake. So function, awake, open closed parentheses, awake with an E, open curly bracket, closed curly bracket. And now, in here, so as soon as the script starts to run, let's say start health is equal to health. So that way we know before any damaging happens or anything gets set, we're going to go in and take the inspector value and plug it into this new variable. So now, let's reset our health to start health. And then, we can reset our position. So again, let's set the transform.position. So instead of using this, we could also just do transform, and use the little transform, and then the .position. And then, we need to reset this to a new value. So let's set this equal to, and then, we can just copy the values from our inspector. So let's see up here, transform.position. So this is going to be three numbers. So in this case, position-- let's take a look at this, so we can know a little bit more about what position is actually doing. So here, we are at our script reference. So let's see. We're looking for a transform.position. And you'll see that this takes in a Vector3. Now, we don't really need to know much about what a Vector3 is, but we need to write the position is equal to Vector3 and then the x, y, z-values. Now, I usually look up these sorts of things before I write any code or after it begins giving me errors. Because many of these take a very specific type of information, such as a Vector3. So transform.position equals new Vector3. And let's jump back to Unity. And this is going to be-- let's see-- 27.44 comma. I'll just copy that value Ctrl+C, Ctrl+V comma, Ctrl+C, Ctrl+V comma and parentheses. Now, as always, we need to put a semicolon at the end here. And this should reset our player to the spawn point. So let's hit Ctrl+S to save that. And if we jump back into Unity, let's let this script recompile, hit Play, and I'm just going to move this console out of the way, and let's see if our script reset us successfully. And there we are. So if we run into the fire and let the player health go to zero, it will reset and set it to the initial position. Now you'll notice that it did set it to the initial time, but we also have a little bit of a timing issue where the player health is still inside of that trigger before it gets reset. Now that is an ordering issue. Because if you recall in our script, we reset the health and only then do we move it. So instead of moving it afterwards, so let's actually move it and then reset the health. So we're no longer in the trigger that was damaging us. So let's just switch these two lines up, save it, and jump back to Unity, and hit Play. What we should see is a successful implementation of a reset function. So again, we'll stand in the fire waiting for our health to disappear. And it'll move us back. And we're still getting a little bit of a timing error. But that is to be expected when doing these kinds of events. So now that we've got our reset function working, we can put this reset function wherever we need it to do a hard player reset. And creating new functions and creating the code inside them is the primary way we create functionality or usefulness inside of our scripts. Because we do not want to just stick everything in the update function and put all of our code in here. We want to be able to bundle up our code into functions so that we can have very plain and understandable names for our operations. So now that we've got that done, let's just do a little clean up in the next lesson with some comments, and we'll be finished with this scripting part of this course.