Check out our latest Unity courses in the Unity Game Development Mini-Degree.
Transcript Part 1
The transfer component allows you to specify a game objects position, rotation, and scale. We’ve looked at how to position objects. Something that was missing that needed to be mentioned was that the unit of our positioning is called Unity unit, and it’s equivalent to one meter, ^when it comes to physic calculations or virtual reality. So assume for all matters, that one unity unit is one meter. What we’ll do in this lecture is look at rotation, the second, of these transform properties here. So, what I’m gonna do is select my cube and click on the rotation button here. That will activate the rotation gizmo.
Remember that a gizmo is simply a tool widget that appears in your unity later on that you can use to modify different things in your games. So in this case, I see the rotation gizmo here and what I can do is drag any of these lines to produce a rotation about a particular axis. For example, if I want to rotate this cube about the z axis, the blue axis, I, what I’ve done is selected the blue line which turns then yellow when selected and I can rotate my cube in this manner. You can see in the transform component, how the values for that rotation changes. You can do the same thing with your other axis, with x and with y. And what I want to mention is that this rotation here, these values are in Euler angles. Now, what are Euler angles?
Let’s take a look at this concept of angles. An angle can be found by joining two segments by a vertex, like this. The separation of these two segments can be described in degrees, so it is a measurement unit. Where if you have one segment like this and this is vertex that joins it to the other vertex, you can describe all the way to 360 degrees, in this manner. For example, if the other segment is here, that is called 90 degree angle and that is called a right angle. If, for instance, you have, something like this, that is a 180 degrees, that is called straight angle.
So these are all useful math concepts, that you need to know when working with unity and transforms. Now how does this actually relate to rotation, because so far what we’re describing, is just simply the separation between two different segments. Well, Leonhard Euler came up with the concept of Euler angles to describe the rotation of object in 3D by giving the rotation in angles of all three axis.
So that you can have a rotation in y, for example, so you could rotate your cube like that. That would be a rotation on y or about y, is the correct way to say it as well. You could have a rotation about x, like this, and a rotation about z like this. And unity provides us exactly with those tools. Now how do you know when the rotation is positive or negative? Because in this case we are sort of going that way but what if we want to go the other way?
Well, because of Unity’s left-handed coordinates system. That can be easily determined by using your left hand. So with your left hand, point your thumb towards the direction of the axis, towards the positive direction of the axis, And then simply close your fingers like shown in the figure. That will give you a positive rotation. So if you have, let’s say, some sort of game object, like some sort of car or something and you want to rotate it about this y axis. The positive way of rotating, if you specify rotation in a positive number it will go this way. And if you specify a rotation in a negative number it will go this way. So now, if we go back to Unity, we can easily see this in action. So, I’m gonna reset the rotation here, set it back to zero, and I’m gonna click outside and click on it again.
And you can see for example that the y axis has it’s green rotation handle here, and if I rotate it, in this positive way that I was describing before. Imagine that your thumb points up here in your left hand. See how the numbers is positive, if you on the other hand, rotate it the other way, that is negative. So that is how you can rotate the elements. Something else that I wanted to show you in this lesson, is how we can move our object considering their rotation that was applied to it.
Let me explain it in a more clear way. You can see here, we have our cube that doesn’t have any rotation applied to it. It has zero in rotation in x, y, and z. The blue axis, the z axis, as you can see, matches this face of the cube, So, I can move it along that face. But once I rotate my cube, so if I apply a rotation and if I unselect it and want to move it again.
You can see that the blue axis no longer matches that face. Because it still matches the global coordinates. However, if you click here, where it says global it will toggle into local, so it will go from global to local, and what that does. What local does, it allows you to move them with the movement tool. Your game object in the local coordinate system which was modified by the rotations. So we can now then move it along this axis here, or move it this way. Even though that doesn’t match the x and z values of our scene. Now, the best way to really get a good grasp of rotation in the editor, either by moving things around, and also by adjusting numbers here manually, which you can do as well. Is to actually go for a little challenge.
So, I’ve actually prepared a challenge for you, which consists on starting with something like that and putting together, that simple bridge. So that will combine a few of the things we’ve covered, because you’ll be rotating things but you will also be moving things around. I will show you, then, the solution of the challenge. So have a try, and if you are unable to solve it, that’s fine, because we are going to cover it step-by-step.
Now, a couple of things. Don’t try to make it exact and precise. As in, the bridge doesn’t need to be in the exact half of this area and also this distance here doesn’t have to be the same as the distance there.
When it comes to the platforms, I’ve given them a 45 degree inclination, so that you know, but you can try with a different numbers as well. So have a try and then come back for the solution, but before you do that, actually, a quick tip. If you have multiple game objects in your scene, you can easily select many of them at the same time by pressing ctrl or cmd on a mac and then clicking on them. So you can select many of them and then once you’ve done that you can move them all at once. So, keep that in mind.
Now, have a try at the challenge and come back for the solution. Alright, so you’ve had a try and hopefully you were able to put this together. But if you didn’t that’s fine, at least you had some fun, you had a try and you’re getting better and better at using the unity editor. So let’s go for the solution, so I’m going to find that folder, I’m going to select the file and everything is loaded for us to start solving this challenge.
So, I want to mention that there is not a single way of solving this challenge. It’s really up to you and don’t worry too much about particular details. Just try and see what works best. So I’m going to start by rotating this about the y axis in 90 degrees, so that the bridge is in the correct orientation. So I’m gonna drag it here and I’m going to look from above, just to make sure that the elements are kind of in the right positioning. Whoops. And what I’m going to do now is rotate the stairs to the bridge. This one for example, I really want to do is rotate it about the z axis, and I want to rotate it in a positive way. So that it looks that up.
So I’m, literally, using my left hand to try and show you how to do it. So, that’s 45 degrees positive and with this one, if you also use your left hand, you realize you need to do a negative rotation. It needs to be in z. So minus 45 as well. Now let’s join these things. Let’s put them together by using vertex select, type v and then pick this vertex there and, yup, it needs to snap in the right location. Whenever you have that stuff that you just saw, just press f and that will bring the elements into focus. Okay, so that seems good to me.
So I’m going to do the same thing with the other part. I’m going to grab that, type v, grab the top vertex and make sure that it is in the right one, I think it is, and yeah, it seems that it’s fine. Now grab all of them, select them. Press ctrl or cmd, so that we are selecting all of them. You can also select them from here, so you can go like that, and now we just need to bring it down to the floor. And It doesn’t need to be a precise, it can be something like that.
Transcript Part 2
The transform component of a game object includes the position, rotation, and scale. What we’ll do in this lesson is talk about scaling. When you select a game object, you’ll notice that the default scale is one. If I go here and click on the Scale button, the scale gizmo will appear in my game object, and that allows me to change the size of my game object very easily by simply dragging the handles that appear in the gizmo. You can see that I can scale my game object in all different directions, and I can scale it either up or down as well. So I can make it bigger or smaller.
How does this exactly work? What is a scale? If we have a cube that has a size of one in all sides, and let’s say that this is x, so that’s the x direction. If I want to change the scale of x and set that to two, or to any number, what I’m doing is multiplying that number by the size of the object in that direction.
So if we set this at two, we’re now effectively doubling the size on x, so the final object will look something like that, and this will have a size of two. When you have a scale that is greater than one, you’re increasing its size. And when you set a scale that is less than one, you are effectively reducing the size of the game object. If we set a scale of say, 0.5, what we are doing is reducing the size in half. So if we had our cube like this, and we went and reduced the scale on x and set it to 0.5, this is the x direction, then the cube will be reduced to something like this.
So the size will be cut in half, basically. You can easily see that here, for example, if I’m increasing zed here to a number that’s greater than one, it’s bigger. And if I reduce it, eventually, you set it to something smaller and you are reducing the size. We have now covered the transform, so you know about position, rotation, scale, and in order to access these buttons here quickly, if you don’t wanna be clicking the buttons, you can use the keyboard as well. By using the keys Q, W, E, and R, that allows you to easily go through all of these tools.
Remember that the first one allows you just to move the scene. It’s the same thing that you can do with the middle button of your mouse. And regarding this last button here, it is used to resize two-dimensional objects, so we’re not using that for now. We’re working with three-dimensional objects here. Another topic I wanted to cover in this lesson is that of parent-child relationship between game objects in Unity. So far, all the game objects that we’ve worked with don’t have a parent. They are top objects here in the hierarchy.
But, you can have that kind of relationship between two objects. So, what I’m gonna do is create another object. I’m right-clicking and going to 3D Object, and I can select from the different primitives, so I’m gonna pick a cylinder. So I have a cylinder now. Let me change its position. I’m pressing the letter Q just to start using these shortcuts. And if I want this cylinder to be children of the cube, all I have to do is to drag it in here in the hierarchy view. You can drag the cylinder onto the cube, and now that relationship has been established.
What does that exactly mean? What it means is that the transform of the children is relative to the parent. For example, if I move the parent, you can see that the child object moves along. The position of the cylinder is no longer in relation to the origin in the world space, but it’s in relation to the origin of the parent. The origin of the parent is this point that we see here. That is the parent’s pivot point. The position of the cylinder is thus established in relation to that point.
For example, if I set that cylinder to a position of zero, that will actually put it in its origin, in the origin of the parent. If I move the parent around, the parent, as you can see, it’s not on the origin, so it’s in a different position. And yet, if I go to the children, the children is in the origin because that is in relation to the parent. I’m gonna move that away. I can also change, for example, the scale, and the scale of the children will adapt accordingly. This parent-child relationship will be used all over the place, and you’ll be using it all the time when working with Unity. Some situations, for example, imagine that the parent object is a player. So, I’m gonna rename this. You can right-click and click on Rename. You can also press F2.
I’m gonna call this Player, and let’s imagine that this is some sort of tool or weapon, and imagine that your player needs to be carrying this around. So this parent-child relationship allows you to easily do that. The parent, imagine, needs to be carrying a gun, or some tool, or some item. So that is one case. Another example is, if you recall the challenge of the Position lecture, there was some sort of humanoid figure that was made of different cubes, so that is another example where you might wanna put all of that inside of one parent object, and then you just move that parent object. But, the parent object contains the different parts of the game character, or whatever item, or thing that you wanna have in your level.
Another example, imagine that your scene has, I don’t know, 500 trees, and you don’t wanna have all those 500 elements here because it’s gonna be very hard to navigate this hierarchy view. What you can do in those cases when you want to group elements, you can create an empty object and put them inside of that empty object.
So, I’m gonna create an empty object. You can go to create here, Create Empty, or you can do a right-click and then Create Empty. You can call this, for example, Environment, and then you could have a lot of different elements. For example, different trees. You can add elements like that directly inside of another object. We’ve covered a lot of ground, and what I want you guys to do now is have some hands-on practice, so we’re gonna go to challenge mode. So let’s open the challenge. This challenge consists on opening this scene that’s gonna have a nice looking tree, and a very distorted tree.
What you have to do in this challenge is go into this element, which is gonna be a parent with some children, and I want you to change the scale. Only the scale, don’t change the position or anything else. And you have to change the scale of the parent and also of the children so that it has the same scale values that this other tree. So, start by going to this other tree and see what values are present for scaling, and then go to this tree and try to change those values so that it matches the other tree. So have a try and post your video, and then I’ll show you a solution.
All right, so assuming you’ve had a try, I’m gonna show you how you can easily solve this challenge. I’m gonna go and open the challenge file. And, no, I don’t wanna save this scene. This is located in another project, yes, I want to open another project. All right, so my scene is ready and I’m ready to solve that challenge. Let’s go and see how we have this environment object, which contains the ground, nice looking tree, and ugly looking tree. Nice looking tree has a scale of one in here, and this one has a scale of five. Let’s start by changing that to one so that they match.
Now, if we go inside both of them, there’s a trunk that has a scale of 0.2, 1, 0.2, so I wanna have that same thing here. And now, on top, I have one, two, one, so I’m gonna go here and make sure that I also have two in here, that’s looking a little bit better, and one in here. So there we go. We’ve got now all trees looking the same. Get ready for the next lesson where we will cover the basics of materials so you can see how all of these things look good because they have colors, they have materials. So, that is what we’re doing next.
Transcript Part 3
There are three important concepts that we have to cover. The first one is that of shaders. Shaders in Unity are scripts that describe the mathematical calculations required to determine what colors should be shown on the screen, what color should each pixel be. So when you see your game, it is the shader what determines what exactly you see according to the light and to other settings.
A material on the other hand, is what defines how a surface will be rendered or drawn on the screen. And a material has a shader associated with it. So a material has a shader, and depending on the shader, there will be certain options, certain parameters, that you have to enter. Some of these parameters might be values of different colors. It could be other numbers, or it could be different textures.
A texture is simply an image, a bitmap image. And depending on the shader, the shader might require a few images. So, basically, to summarize, a shader is a script that is written in a language called HLSL and that shader would require certain parameters, certain textures, and it is the material where we specify all of those things. When we create a material, we assign a shader to it and then we enter all these things and then we can give this material to a 3D model. For example, you can create a cube, create a material and then you can assign the material to the cube. Let’s go to Unity and create our first material.
So, in Unity I’m going to start by creating a cube, to which we will assign a material. In my project view, I’m going to create another folder here, which I’m going to call Materials. I like to organize my projects in this way. So I’m going to enter that folder. And I’m going to right-click and go to Create, and select here Material. So I’m going to create a new material. And we can call it something like Basic Material. So if I select this material here, you will see all the properties of this material, in the Inspector.
The first thing to select is the shader that’s going to be used. Unity comes with a shader called the Standard Shader, which is a very flexible and robust, and efficient multi-purpose shader. So that is the one that we are going to be using. There are other shaders for other purposes but we will be using and covering this Standard Shader here. And there are a few parameters here that we have to enter. The albedo is a very important concept.
So let me explain what that is. You might have heard the word albedo if you’ve studied Astronomy. So the albedo is the proportion of the light that reflects back. So different materials have different albedo values. But in Unity, albedo actually means something different. It doesn’t mean the percentage of the light that is reflected. It actually sets a base color for the surface.
So, in simple terms, it is the base color of the material. So let’s go and select a color. I click on the color there and I get this color picker so I can pick any color I want. And once I’ve selected a color you can see that your material now has that albedo color. And I can drag my material to the cube to assign that material. If I click on the cube, you will see that the cube and all elements that you create that are obviously visible, like a cube or all the primitive cylinder spheres, they all have a mesh render which is a component that determines how that particular game object is show, is drawn on the screen, is rendered on the screen.
So you can see here that in materials we actually see our material. If we create a new cube, and I’m going to move it away, you can see that by default the cube has something called a Default Material. So, our first cube has the material that we’ve created for it.
There are two other parameters here that are very important. The metallic and the smoothness value. The metallic parameter, specifies how much the material will reflect its environment as opposed to just showing its albedo color. The smoothness simulates “micro-surface” on the material and that affects how it reflects light. So the more smooth it is, the more even the reflection is going to be. And we’re going to talk about emission in a little bit.
The best way to get a feel of what these parameters do is actually to try and experiment. But if you need some help, there is a very useful material chart in the Unity documentation that explains how you can easily achieve different effects, what sort of color you’ll need on your albedo, and then what parameters you can set on your metallic and smoothness.
Now let’s go to Unity and have a play with these things. Before modifying those things, what I would like to do, is actually add a couple more things here. So I’m going to add a plane that we can position underneath these elements. And I also want to add a second light. As you know, we have a directional light, which is light that comes from this direction as shown here. And I would like to add a point light, which is light that comes from a single point.
So I’m going to right-click and go to Light, Point Light. It created a light here. As you can see the light has a transform component that we’re already familiar with and a light component, which is what makes that light a point light. I’m going to draw that here and I’m going to change the color of that light to something else, something like green so that it gets something a bit more interesting. And there should obviously be some reflection on the ground. I’ve created another material here called Ground Material so I’m going to select a color for that material, let’s make it brown, and I’ll just position that in there.
Okay, so let’s go back to our basic material and play with these metallic and smoothness parameters. Out of both parameters I find smoothness the one that’s the easiest to understand. Because you can easily visualize surfaces that have a micro-surface on them, for example, the ground or blanket and those surfaces will usually reflect light in a way that is not so concentrated.
For example, if we said this, we can compare that when it is very smooth it looks like a billiard ball pretty much, and also the light tends to be more concentrated. Whereas, when it’s set to a low number, it can resemble more the kind of surfaces that have this characteristic. The metallic value, the more metallic it is, the more that it reflects the environment. When you can really appreciate the metallic value is when you have, for example, a sky box.
So, if we look up we can see that this sky, it’s pretty boring right? It’s very plain. But if we had a sky box with clouds and more elements, by using the metallic value, you could reflect some of the stuff. It would become a bit more intuitive. But, as you play with these parameters and also keep that chart in mind you can achieve a lot of very good looking materials. The other property here that I want to cover is emissions. A material that has emissions is a material that emits its own light, basically. So let me create a new material. So I’m going to go and add a new material. I’m going to call this Emissive Material. And by the way, I think the ground should have a low value of smoothness and then it looks a bit better when you set a bit of a higher metallic value here.
So that’s my opinion when it comes to grounds like this. Anyway, back to emission. We have to activate the emission here. And then we can set the value of the light that will be emitted, the color. So, we’ve set that to green and we can also give it an albedo color. And as you can see, that looks kind of radioactive. So, we can assign it to this one here. This is actually emitting some light.
If you played with more advanced lighting options that will come later in the course you can actually get this to show this light and to really illuminate its environment. So that happens when you do something called baking light. Another property here that’s interesting to discuss is transparency. So I’m going to go and create a sphere, move that somewhere here, and create a material that will have transparency. So I’m going to go Create, create a material, and I’m going to call this Glass Material.
So, for our glass material, we can give it an albedo color, for example, five. And then you can play with this alpha color, but it’s not going to work unless you go here, in rendering mode, and check on transparent. We check on transparent and then you can set the alpha channel, and that will give it the transparency. So if we drag this onto here, we can easily see how our sphere is transparent or semi-transparent. So in that way you can achieve that effect. And the fifth type of material that I want to build here is a material that uses a texture, an external image file. So let’s call this Sand Material.
And what I need to do is import an external image file. I’m going to create a new folder here called Textures. And, if I go to my file explorer, I already have a few textures that we can use. These are all open source things. I think I got this one just from the GIMP program. But if you search there are a lot of open source and free textures that you can use. And also, premium textures. So, the way to import this is simple as simply dragging the file onto Unity. If you click on it you will see that this is a texture and there are some options here. We will leave all these in its default. So let’s go to our material and I want to give that material to a floor, so that we make it sand, a sandy floor. By clicking on that circle next to albedo, we can bring up this menu with available textures.
So I’m going to select the sand. And, at first you can see that it doesn’t look too good, because it’s expanding the image to cover all the whole area and this sand texture is meant to be repeated, multiple times in such an area. So the way to work with that, the way to fix that, is to go to Tiling, underneath Emission. Tiling has nothing to do with Emission by the way, it’s just located very very near. And this is the number of times that the tile or the texture will be repeated. So if I change, for example, to an X we are repeating on the X-axis we’re repeating it twice, but just once on the Y. So as you can see it will stretch. It looks horrible.
So, what if we do that on the Y as well? that looks a little bit better. We have now four times, the texture. And if we do three times, that looks much much better. And let’s do four, and just to show you, if you have too many it kind of looks weird, because it’s just repeated too much.
I found that four looked okay in this case, so I’m going to leave it at four. And, we could also play obviously with these parameters if we don’t like the way that the light is being reflected or anything like that you can just find the sweet spot, find whatever works for you. And, you can also set an albedo color, if we wanted this to have a bit of a martian aspect, we could change it to like red and that would give us our martian soil.
So we have all different types of material here. We have this sandy material, that you can be like “Yeah, in real life, sand is not smooth,” but sometimes in Unity you just have go with whatever kind of looks best. So, I guess it could be more like that, that’s a bit more realistic, yeah. And then we’ve got this other ground material that basically had like a very low smoothness. Then we have the transparent material and we also have an emissive material like a radioactive cube.
And this original material is the one that was made kind of like a billiard ball, very metallic, very smooth. So, what we’re going to do now is go to the challenge. And, the challenge will bit a bit different in this case. So, the challenge will basically replicate everything we’ve done in this lesson but without you obviously looking at the lesson. So trying to do it on your own. You can always just look at the lesson again to find out how any of these things are done and of course do check the documentation that I pointed out. So, create a new project in Unity. Create five cubes or five primitive shapes, it can be spheres or cylinders, a plane. Create five materials and start with the basics. So, start by simply setting a simple color, just change the albedo.
Then start playing with the metallic, with smoothness, to create something that’s looking like metallic, kind of like that billiard ball that we created. Then create your own emissive material, your own radioactive material. And, create a semi-transparent material, something that looks like a glass. And, end up by creating a material with a texture. So use any image that you want, and you can of course download the solution that is basically the files, the project that I just put together so you can use that sand it’s open source as well. (groovy music)
Transcript Part 4
The Unity Editor can help you with many, many things. But there will come a time when you will want to implement your own custom logic or the logic for your game. And for that you need to learn scripting. What we’ll do in this lesson is get started with scripting, which is the same as programming or coding, or however you want to call it, using the C# programming language. I’ve created a new project here, and the very first thing that I’m gonna do is create a folder for my scripts.
So I’m gonna right-click and go to Create, Folder, and I’m gonna call this folder Scripts. Inside of that folder I’m gonna create my very first script. So I’m gonna go to Create and select C# Script, that is C and the hash sign Script. I have to enter the name of my script now, and the convention is to start with a capital letter.
So I’m gonna call this Hello, and then World, also W will be in a capital letter. That’s just how people write their names of their scripts. So I’m gonna press Enter, and that has created a new script for us. If you select that script here, you can already see the source code of the script in the Inspector window. To open your script, double click on it. In my case that will open the script in Visual Studio, which is the officially recommended editor for Windows.
If you are on a Mac, the script will be open in a program called MonoDevelop that works in a very similar way. Maybe, by the time you watch this, Visual Studio for Mac will be out, and you will be able to use Visual Studio as well. So we’ve opened our script, and if you have never done any programming before, this can seem like a very strange thing. What does this all mean? And even if you have done programming, but you haven’t done any C#, this might also look pretty strange.
So, I’m gonna start by deleting this whole part here, this whole part that says void Update and those brackets, I’m gonna remove all of that and just keep this stuff here. And what I’m gonna do next, I’m gonna explain what this is in very simple terms with no technical term usage, and then we can move up from there and start adding some code. And now we can explain what this is.
So the best way to understand programming, and this is programming in general, is to think of a cooking recipe. So, in a cooking recipe you have different steps. For example, if you want to make pasta, the first step is to boil the water, and then you need to add the pasta, and then let’s say that you need to stir. So you have different instructions, and they can all be understood in human language in basic terms, and they all need to be performed at a certain order. So it goes from top to bottom.
The same thing happens with programming. You’re basically issuing statements that the computer needs to execute, and they are also executed from top to bottom. Let’s say that we have the full recipe of pasta here, and we might want to use that recipe in different meals or in different events. So, for example, you could have some sort of dinner, a dinner party or something, where you need to make the pasta. And let’s say that you also need to make a dessert. So that is another recipe. So this recipe can be put in what would be called a method, and that method could be called something like MakePasta. So every time that you want to make pasta, you just use the method MakePasta, and that method has all these instructions.
So when you’re preparing your dinner, you don’t have to type each one of them. Write this down, you just say, just make the pasta, and you already have the instructions to make the pasta. So if we now go to this code here, we are gonna start from the inside to the outside, making sense of everything. All of these instructions, they are very clear steps, would go, let’s say, here.
This Start text, it is a method. So it would be something like, for example, MakePasta could be Start. And this green text here that says, Use this for initialization is a comment. That is a comment for humans to read. So whatever is preceded by these two signs is a comment for the programmer to read. The computer is gonna ignore this. So what we have here, we will have instructions here, and those instructions are put inside of a method, in this case the Start method.
Now, what do these brackets mean? The brackets, these curly brackets are used to put everything together like the same thing that you have a burger, and you have a bum on the top and a bum on the bottom, and then you have your burger here and the lettuce and all the other things. So, think of the brackets as the bum that goes on the top and at the bottom. Things need to be within a certain space scope. So that is what these brackets mean.
What if we go one step even further. So at this point we kind of have an idea what this is. Basically, start some method, but what about this Class? This is a class and it’s called Helloworld. We gave it that name. Think of a class as like a Lego block, think of the class as a Lego block. So, you have a block that is almost like a Lego piece that you can use in different parts. So you could have, for example, one character in your game that is gonna use this Helloworld, and it’s gonna do something. And then you could have some other character in the same game that uses that same component. So that is what a class is. Think of it as some sort of block that can be reused.
And again, the brackets here are doing the same function as the burger bums on the top and at the bottom.
Then, what about this top part here? Since we said that this was like a piece of Lego, these would be other pieces of Lego that other people created that we are using in our script. In particular, this one that says UnityEngine. It gives us access to the whole Unity API. And these other two are part of the C# programming language. It’s created by Microsoft, and this is part of the, it’s called the .NET Framework. So those are just tools that come with the programming language that we can use, and when we create, so let’s go back to Visual Studio, when we create a new script in Unity in this way, this is created for us automatically.
The instructions that we put inside here, for example, boil the water or things like that. Like it says here, they will be used at the beginning for initialization. When our game starts, and the very first frame is shown on the screen, that is when the Start method is executed. And the last part that hasn’t been mentioned is this MonoBehaviour thing here.
So, we will go over this in later lessons, but for now, just think that all this does, it just gives us the fact that we can use Start like that, it comes from here. So this gives us access to Unity’s way of working with things and connect with the Unity game itself. So, that would be a very, very general, basic way of looking at programming and a script. Now, what can we do in our Start method? When people learn how to code, usually, they like to do what’s called a Helloworld, which is when you just say hello to the world. So, what we will do is just show the text Hello World. For that, if we type print, we can show something on the screen, and what is it that we want to print, we need to write the message. So what I want to write is a message that says Hello World.
But this kind of feels weird because see how it’s kind of underlining the words, and that is because when you are going to write a text what’s called a string, you need to use these quotes. So we are going to print Hello World on the screen. And see how it’s still showing us this red line, it means that there’s something it doesn’t like, and it is because we are missing a semicolon.
Whenever you type this type of statements, you have to put the semicolon at the end. So, we have now a little script, which all it does, it will say Hello World at the beginning of the game. So let’s go back to Unity and see how we can make this actually happen. When a game begins, that is when you press play. So I’m gonna press play, and I’m not seeing any Hello World anywhere.
So why is that? It’s because in order for a script to actually do something, you need to put it in your game. You need to assign it to a game object. So you need to create a game object or use an existing game object, and give it the script, so that then the script has life and actually runs when the game runs. So, what I’m gonna do now is create an empty game object because we don’t really wanna show anything. I just need a game object here.
An empty game object only has the transform component. So it’s not shown, it doesn’t have a render component, it doesn’t have a material. And I’m gonna rename this and call it just Hello space World. That’s just gonna be its name. And there are different ways of assigning a script to a game object.
One of them is by simply dragging the script all the way to the game object, and that will make it appear here as a component. I can remove that component by going here, Remove Component. So, another way would be to click Add Component and then find Scripts and find that Hello World. So that’s another way of adding it. So now that the script has been assigned in our game, we can actually play our game, and you will see the Hello World. So let’s play the game. And you can see down here Hello World. If you go here next to Project to Console, the console is an area that we use to communicate with the script. In the sense that we can send ourselves messages such as this this, and also, if there’s any error in your code, it will be displayed here.
See that Hello World entry, if I click on it, and then I go down here, it will tell me the exact location of that message. In this case the file is called HelloWorld.cs, and the line is number nine. So if you go to that file, which is by the way called HelloWorld.cs, that is the extension for C# scripts, and if you go all the way to line number nine, we can see our message. So, that is how you can create a script and assign it to an object. And what we’ll do in the next lesson is make this a bit more interesting and introduce the concept of variables, so that we can actually pass on things to our scripts. So before you go to the next lesson, I have a very simple challenge for you. The challenge consists on creating a new script and assigning that script to a game object.
The script, all it needs to do is say hello to yourself on the console. So you can write anything you want on the script. Once you complete the challenge, you can join us on the next lesson. (upbeat music)
Transcript Part 5
Variables are a very important concept in computer programming. They allow you to store and retrieve information from your computer’s memory. You can, for example, store the name of the player and a variable, and then you can show that later to the user. You can do all sorts of things with variables. You can do mathematical operations. And what we are gonna do now is create our fist variable. There is one important step when you create a variable, which is the variable declaration. And then there’s also another step, which is the variable assignment.
Just so you know, both things can be done at the same time. So we can also have variable declaration plus the assignment at the same time. So let’s declare a variable. As I said, something that will describe the name of our player. In C#, when you create a variable you need to specify the data type of the variable. That means, is our variable a number? Is our variable a string, which means text? Is our variable a true or false value, or is there a decimal point number?
Well, in this case, it’s a text, so it’s a string. So we’re gonna type string. That is the data type. Now, we need to give our variable a name. I’m gonna call it playerName. That’s gonna be the name of my variable. Now, because we finished our statement, we declared our variable, I’m gonna type semicolon. As you can see, the editor is gonna highlight that because we’ve declared the variable, but we’re not using it. So that is obviously not optimal. That is why the editor is telling us, please use that variable. Let’s do an assignment where we give this variable a value. We assign by using the equal sign. And because it’s text what we want to give, it needs to be inside of double quotes. So this is gonna be, for example, Mr Pink. We finish our statement, so semicolon.
So we have declared a variable, and we have assigned a variable. And it’s still complaining because we haven’t really used that variable. So let’s show that to the user. And by the way, notice how I’m using comments just to keep track of what we’re doing. So it’s always a good practice to comment your code so that when you come back to it later it’s easy to know what is going on and what each thing actually does. As we saw before, we can use the print method. So I’m gonna open brackets so that we can pass in what it is that we want to print. And what it is that we want to print is the player name.
Now, see how asset type, the name of the variable that we’ve previously declared, I can see that in here. It is being suggested to me that that variable has been declared, and then I can just use that. So if I click on that suggestion, or press Enter, we will actually save ourselves the time of typing. So that is called order completion. And it’s common in code editors.
We can now save. And if we go to Unity. And I need to start by clearing the console because I was doing other things here. Now, I’m gonna play my game. And you can see here, Mr Pink. So we are getting that shown. We are basically declaring a variable assigning, and also showing it on the screen. Now, what if we want to do both things at the same time? String, let’s call this enemyName, and we’re gonna call this Miss Orange. Close that statement, and that’s it.
So we are declaring and we are assigning. By the way, before you assign, so in between these two lines, the actual value of this variable is null, which is a special key word in C# to determine that something is basically nothing, or it hasn’t been declared yet. So that’s null. And we can also show this to the user. So we can show that, print. And I’m gonna make use, again, of this order completion. And there we go. Now, what other types of variables can we create in Unity in C#? We’ve seen string.
Well, there’s quite a few of built in types. These are all called data types. There are a few built in data types, or types that we can use. And as we create our own classes, we can also use those to declare variables. But for now, let me show you some of the basic ones, some of the ones that we’ll be using the most. So we have integers. For that we use the int keyword that gives this big range of numbers, probably not gonna use all the way to the extreme, who knows. An example would be just to type 100.
Then we have float, which are numbers that have decimal points, and you specify the decimal point just with a point, and you have to type the letter f at the end of the float. Now, strings that we saw, we use the string keyword, and it’s just for text. So like, Hello World, for example. Booleans can have a value of true or false, for example, false or true. And see how we’re not using quotes here. We just type true or type false.
For a full list of the built in data types just go to .URL, which will take you basically here. And then you can see some of the other types that are available. This documentation is the part of the language API of the things that come with the C# language. So let’s go back to our code, and let’s declare another variable, so the float, for example. Let’s say the energy of our player. We can declare float energy, and, in this case, it can be something like 1.5. That would be the value of the energy. Or you can have an integer in the number of lives, for example. Number of lives int n, and you can have 10 lives. So that would be another example.
Also, you can check whether the player is alive or not. So is alive. And you can have a Boolean, and then something like that, isAlive can be true, or it can be false. So in that manner, you can declare variables in these other data types. Now, regarding floats, there’s something interesting here, which is that, let me show you in a note.
Now, something interesting here is that when we declare a float, for example, 1. If the number is 1 we can type 1f, right? But we can also just type 1. And 1 will always convert to 1.000. This is an integer. This would be a float, 0000, all the way to infinity, really. So that works. If you don’t wanna type the f when you are writing integer numbers, you can do it like that. C# under the hood is really doing this transformation for you.
Now, if you try to type, for example, 1.5f, that’s fine, but if you type 1.5 and you are missing the f, you will get an error. And the reason for that is, if we go to that original list of built in data types, besides having float, you also have double. And double has a much higher range of numbers than float. So it goes all the way to 10 to the minus 325. So that’s really, really, obviously, a really big or small number, and that is obviously more than this, or a bigger range than this. So if you type 1.5, that is actually a double, and a double cannot convert it to float, because you are losing part of the number, part of the information of the number. And C# is not gonna know how to transform that.
The same thing happens if you try to go from float to int. That 1.5, for example, how does that translate to int? Is it 1 or is it 2? We don’t know, because ints don’t have this part. So avoid that, and also, avoid that. So to summarize regarding this, when you are using float and it has a decimal number, decimal point, just always use the f, and if it doesn’t, you can avoid using the f if you don’t to type the f each time, which I have to confess, is something that happens to me. So let’s go back to our editor and look at that. So you can see how energy is 1.5f, but if we don’t type f we get that error thing. It says here that something from type double cannot be converted to float. Same if I type 1, we won’t get that error because an integer can be converted to float very easily.
So that was a bit of clarification that I wanted to make, which might be a bit too early, but it’s just something to keep in mind. Now, the other thing I wanted to cover in this lesson are Arithmetic operations, which can be performed with integers and floats as well. So the Arithmetic operations that we have here are addition, subtraction, multiplication, and division.
For example, let’s say that you want to calculate the total score of a user. And let’s say that you have the total score is they completed 10 levels, and each level gives you 10 points. So you’ll have 100. You could have this saying the levels completed. She completed 10 levels, so she completed 10 levels. And then your total score is the levels, oops, the levels completed times 100. So you can get that. And in that case, we’re using the multiplication. Or you can easily total, for example, total could be 10 plus 1.5f. As I mentioned, you can convert from integer to float, but be careful that if you try the other way around, for total2, if you try that, you’re actually gonna get an error, because, as I said, you cannot go the other way around in this implicit manner.
You can explicitly convert them, and you can define how it’s gonna be converted, but in this case, try to avoid it. Now, the last thing is, well, we still have subtraction. It’s quite easy. So the number of lives could be a number minus another number, like that. That will be 4. And division, of course, the parts that you get out of something, could be, for example, 100 divided by 50. In that manner, you can divide.
You can also combine operations. So you can have a combination. You can have, for example, 100 plus 1.5f times 10 divided by 100. And just like in algebra, multiplication and division go first, and then addition and subtraction follow. So that is the same that with algebra. And you can also use brackets, and by using brackets everything that’s inside the brackets will be executed first. I can easily show this on the screen like that. Print combination, and that will show the total to the user. Something else that I can do is concatenate two different strings and including a number.
So let’s show something like the result for my name. By using the plus sign we can join two strings. We can concatenate two strings. So result for player names. So I’m gonna copy that, and I’m gonna go here. That will say the result for Mr Pink. And then I wanna have some more text, so another concatenation is. And let’s now, actually put this total here. So that will show the result for Mr Pink is, and then the result of this operation. So let’s go to Unity and see what we get with that. So let’s press play. And the result for Mr Pink is 10.15. What is really happening here under the hood is that combination is actually being transformed to string by C#. And then the two strings are joined together.
So we’ve covered a lot of ground, and basically, I’m gonna do a very quick recap. So we are using variables to store information, and to manipulate, and resend that information. We declare variables like that, specifying a data type. We have all these different data types to use, and obviously, a lot more here. And then there’s the part of the assignment where you actually give the variable that value, or that can be changed later as well.
We can easily work with numbers in this way, but keep in mind that you cannot go in an implicit way from a number that has more detail to a number that has less details. So you cannot go from float to int, but you can go from int to float. You can use the same operations as in normal algebra. This is what you use for division, multiplication. And you can concatenate different strings, join different strings in this way by using the plus sign. And when you join a string with a number that is not a string, what is really happening is that that number is being converted to a string.
And, well, in this case, combination is being converted to a string, and that is how we are able to put this together. So before you move on, let’s do a challenge so that you can get some hands on experience. Create a script that converts Celsius to Fahrenheit degrees. This is the formula. If you have a number of Celsius, you multiply that by 9 divided by 5 and then up 32. And that will give you the Fahrenheit as a result.
So create a script where you declare a variable, and you declare a variable, which is the temperature in Celsius. And then you perform the calculation. And then you show that to the user. So have a try, and then I will show you a solution. So pause the video now and have a try. All right, so let’s solve this problem. We’re gonna create a variable that’s gonna be Celsius. And so, we have, for example, 20 Celsius. And for the result, we’re gonna create this Fahrenheit value, variable. The calculation is gonna go like that. We’re gonna do C times, what was the formula, 9 divided by 5 plus 32. And now, we can show that to the user.
So print the result is + f. And if we run that on Unity, we execute that, we get our result, 68 degrees Fahrenheit. So let’s move on now to the next lecture. (upbeat music)
Transcript Part 6
Imagine you want to create a character in your game. And to describe that character you want to have a variable for its name, for its energy, the amount of coins and to keep track whether the character is alive or not. So if we’re just using variables we create a variable for each one of these things and we could define it’s name, energy, coins, if it’s alive or not. But what happens when you want to create a second character?
You’re going to run into an issue that you’ll have to create another four variables and call them something different like second name or second energy. And what if then you want to create ten characters? All of these variable names are going to start to get very confusing and you’re going to have all of these variables floating around. It’s going to be hard to work like that. So there is a better way. And that better approach, that better ways is to use a class.
Think of a class as a blueprint that we can create to define an object, in this case a character. This class of us can have member variables which are variables that belong to the class and they can be accessed anywhere inside this class. In this manner we can create a variable to keep track of the character name, its energy, its coins and whether it’s alive or not. We can create then instances of that character and what that means is after we define the blueprint we can use that blueprint to actually create multiple characters for our game. So, by using the name of the class as a data type we can create variables that consist of new characters that we are creating.
A class is basically a blueprint. A class definition doesn’t create any object. What actually makes things happen is when you create instances of that class. The same analogy with cooking recipe. You have a recipe that doesn’t mean you have a cake. You actually have to make a cake, so that is how you should see classes. That is the easiest way to understand the concept.
Now how can we get access to these member variables? We saw how we could create new characters but we haven’t seen how we can access them. And the truth is that the way they are defined here, we can not access them outside the class. We need to add something to that. What we need to add is the public key word which will make these classes and these member variables public. That means they can be accessed from outside the class. And when you have created public variables like that, after you initiate a new class, you can easily access those variables and change their values, just like you would do with normal variables.
And that brings us to the concept of encapsulation. In C sharp, if you want to have a public variable, you need to write the word public. You can also be explicit and say that a variable is going to be private and that means that that variable can not be accessed from the outside. You cannot access that variable from outside your class. If you don’t write anything which is was I had done before, that by default makes variables private in C sharp. So having these public and private approach which is called encapsulation in object oriented programming, means that sometimes you’ll be able to access variables right away without any intermediary if they’re public. But if they are private, the only way to access them will be through a method.
In Unity, when you create a public variable, so see how this is the script that we created, HelloWorld, when we create a public variable like that, it actually shows in the Inspector. And then we can assign a variable, a value to that variable. So that’s how it works in Unity. If you create a private variable, it’s not going to show like that in the Inspector. And we are now due to really talk about methods, which I’ve mentioned before.
The best way to think about methods is the best analogy, we start up a factory. Methods can have input and they can also have output. The difference with a factory though is that some methods don’t need to have an input. They can not have any input at all. And also some methods are not going to have any output. So that is the main difference with a factory. For everything else it is a very good analogy. This is how we can declare a method in a class.
So see how we have a class here that has our properties. Public member variables of our character, name, energy, coins, and we’re also declaring a public method. That means a method that we can access from outside the class. See how we’re calling our method PrintName. And inside that method all that happens is we’re printing the name to the user. I will talk about void in just a little bit. So if we wanted to execute that method to call that method, we need to create an instance for a character, we can assign values to those public variable.
In this case, there shouldn’t be a is alive, because I didn’t add that here. So, that actually shouldn’t be there. And we can print the hero’s name in this way. So that is how we can call this method here. Returning a value, the output of the factory. I’m creating here another method which is called GetScore. That method, what it does, it creates a variable, it’s a local variable inside that method of tight interjer. And the value of that variable will be coins multiplied by 100.
Let’s say that we want to give this character 100 points for every coin that they collected. And we want to now return that, send that as an output outside of our method. So that, a few things here to notice is that, we have typed here int instead of void like before. So, that means that we are returning our variable of tight int. And that is exactly what we’re doing.
We return that value with the return keyword. Also it’s important to mention one more time that methods can access all member variables. If you had public or private member variables, you would be able to access them in your methods like that. And now when we actually call the method, see how we are declaring a variable here called playerScore and that is equal to hero.GetScore. And when we do hero.GetScore, what is happening is that we’re calling the GetScore method and that will return an integer and that integer is placed in our player score.
And then we can show that to the user, and do anything we want with it. Void means that there is no value returned. So we’ve seen void a few times. It basically means this is a factory that doesn’t produce anything, that doesn’t give any output. If you could do things like showing something to the user, many other things, but it doesn’t have a return value.
And now the other part of the factory, how do you get things in the factory? How do you pass in parameters? We’ve been doing that all along so, if you look at the print method that we’ve used a few times, it actually already has parameters. So, you simply pass these parameters inside the brackets when you execute the method. So let’s look at an example.
Let’s create another method called GetScore but this time our GetScore method will have two parameters. The character, the score of the character will be a base score, so it will have dock value as a minimum. And then we’re going to define how many points we want to give the player or the character, per coin that they’ve collected.
So, as you can see the operation here, the calculation of the score is simply the base score plus how many coins you got multiplied by the points per coin that we are giving and that is returned, that is passed back. And the way to pass these parameters would be the same thing we’ve down with print. In this case we have two parameters so we are passing two values. And see how these two values are intejers. So, they need to match the type that is specified in the method signature, the method definition.
We can also have default values for our member variables. If we don’t specify the value of energy in this case once we create characters, they will have an energy of 100 by default. If we don’t specify a default and we don’t enter any number, the value will be nul.
And now it is challenge time so we’re going to have a challenge that is a two stage challenge. And after you have a try, I will show you the solutions. So let me explain what this challenge consists in. You’re going to go to your HelloWorld script which make sure it’s empty. It only has that start method. And the first thing you’re going to do is create a new method called PrintGame. And this method, all it’s going to do is going to print in the console the type of game that you want to make the most.
So for example if your dream game idea is a VR RPG, just print VR RPG or any game idea that you have in mind. Then the second step is to actually, we’re going to execute that in start. So, you create your method and then you execute it in start. And by the way you create it here, so you create your method here. And then you’re going to execute it. And then you’re going to run the game so that we can actually see that in the console.
So you’re going to run the game. And after you’ve done that you completed part one. And now part two consists on creating a public variable of type string called myGameIdea and you’re going to place that here. So you’re going to place your public variable here. And after that, you are going to go to inspector and enter a value for that game idea. As I mentioned, when you create a public variable that will appear here and you can enter a value so it will appear under the game object that has the script and it will appear on the script component. And then you’re going to modify your PrintGame method.
Instead of just printing the game that you want to make the most, it’s just going to print whatever you’ve entered in this myGameidea variable. So that is the challenge and if you get stuck at any point, we are going to go through all the steps right now basically. So, pause the video, try your best and then join us to see the solution.
Alright, welcome back. I had coffee and it was amazing. No kidding, I’ve been here all along. So let’s actually solve this challenge. I’m going to create a new method here, void PrintGame. This is not going to have any parameters, so I’m just going to type the bracket. And as you saw, I just type one bracket and the other one is created for me. So it does make life easy by doing those things. I’m opening now my curly brackets with are the barrier buns that contain everything that goes in this method. And now we’re going to print a message to the user. And I’m going to go with that VR RPG idea.
So I want to make that kind of game. And now in my start method, I’m going to execute this PrintGame method. So, I’m just going to type PrintGame and close the brackets so that we’re executing that. Now let’s go to Unity and play the game and see if that works. So I press play and yep, we can see that the RV RPG in here. So, we got part one of the challenge solved.
Now second part of the challenge was to create a public variable of type string called myGameidea. So I’m going to go here and I’m going to type public string myGameIdea and I’m going to save. You have to save in order for this to work. So I’ve saved and now I go back to Unity and I have to click on Unity somewhere. And what is really happening under the hood is that the script is being compiled. That means it’s being transformed to zeros and ones and that’s something that Unity, if the script is compiled it means that there’s no errors and therefore we can see this here. If there was any error, we would see in the console.
So now I see myGameIdea, and I’m going to type a different game idea. So, let’s type VR adventure game like Zelda. So I’ve type dot, but I’ve actually, I’m not really doing anything with this yet so that was the other part of the challenge was to modify your PrintGame method to show the value of myGameIdea. So I’m going to copy that. And I’m going to go here and I’m going to delete that hard coded string and I’m going to paste the name of my variable which is this one here.
So let’s go back now to Unity, run our game, and we can see our message. So we completed the challenge. So thanks for watching this lesson and I will see you on the next one.
Transcript Part 7
The demo we’ll be building in the next videos is a very simple balloon popper game or experience, however you wanna call it. I have this balloon here, and if I click on that balloon, I’m increasing its size. Up to a certain point, because if I go too far, it will disappear. We’ve popped the balloon. There are many concepts that we’ll cover by building this little demo.
Some of those concepts are inheritance, we’re gonna talk about accessing the balloon scale, about detecting clicks on a game object, about destroying a game object, and also we are gonna use what’s called a conditional operator, to check whether the size of the balloon, the scale of the balloon, is greater than a certain number. So, let’s dive right in and build this little demo. I’ve created a new project, which I’ve named Balloon Popper. Inside of that project, I created the Scenes folder. Saved my scene inside of that. And what we’re gonna do now is create our balloon. So I’m gonna go here and create a new 3D object. It’s gonna be a sphere. And I’m gonna rename that, and call it Balloon.
I want my sphere to look a bit nicer than that, so I’m gonna go and create a new folder called Materials. Inside of that folder, I’m gonna go and add a new material. So let’s call this Balloon Material. And I’m gonna give it a color of red, and just strike that in there. And, maybe change this a little bit. Lower the smoothness value. Okay. So we have our balloon. And now I wanna create the script for our balloon. So I’m gonna go create a new folder, called Scripts. And inside of that folder, I’m gonna go and create a new script. Create C-Sharp script. I’m gonna call this BalloonController.
And I’m gonna open that in my code editor. We are not gonna be using the Update method here, so I’m gonna remove it. And, there is something that I want to mention. So, what I would like to do is be able to detect when the user clicks on the balloon, and when that happens I’m gonna increase the size of the balloon. So I’m gonna increase the scale of the balloon. Each time the user clicks, I’m gonna be increasing it in a certain number, which I’m gonna call scale factor. And once it reaches a maximum scale, it’s gonna pop. As I mentioned before, we need then, we have these two properties that we are interested in. We need the scale factor, so how much the scale is gonna increment each time you click, and also what the maximum scale is gonna be.
So, for that I’m gonna create two public variables. One of them is gonna be the, how much it grows each time. So I’m gonna put it public float, and let’s call this scaleFactor. And I’m also gonna create another one, which is gonna be maximum scale. So public float, actually this, yeah this will be a float as well, so it will be maximumScale. I can give this default values, so I can say that when the scale reaches three, it’s gonna pop, and each time we’re gonna multiply the scale by 1.2. So it increases 20 percent each time.
And now we need to figure out how we can detect that click input in the balloon. And that brings us to this MonoBehaviour thing here on the colon. This is forcing us to have that awkward inheritance conversation, so we’re gonna have that conversation, and then we are gonna detect the user clicks.
So, let me show you two very similar classes. You have a Player and an Enemy. So as you can see, there’s a lot of repetition here, and in programming, repetition is always bad. We are repeating these two public variables, and we’re also, we have this exact same method called damage, which basically takes energy from the player or the enemy. And by the way, if you want to reduce a variable, energy in this case, I’m writing energy equals energy minus the amount of energy that we want to take to damage our character. But when you write this, that is the same as writing energy minus equal amount, that is the same as that. So, anyway, we have this thing, and a repetition is bad, so, a better approach for this, is that of inheritance.
And, inheritance, what it is, is you create an object, and then other objects inherit all of the variables of the member variables, and all the methods from that parent object. We create a parent that is called character, and that character has everything that is common to all characters. That is the energy, name, and that damage method that we have created. And see how we’re creating two classes that are much smaller than the ones we had before, because they inherit from Character, and that is expressed with this colon symbol.
So the Player character has everything that the- sorry, the Player class has everything the Character class has, that is all of this here, except that it also has its own lives. And the Enemy has the same things that Character has, but it also has this hasClaws member variable. And that brings us to MonoBehaviour, which is what we’ve seen already in our Unity scripts. MonoBehaviour is the base class of all Unity scripts.
So whenever you create a script in Unity, it inherits from MonoBehaviour. And, MonoBehaviour brings in a lot of functionality that we can use out of the box. Even the very start method that we’ve been using, is part of MonoBehaviour, and the print method that we’ve been using to show things in the console, is part of MonoBehaviour too. And, MonoBehaviour has very good documentation, in the Documentation API. And in here we can find all of these methods that are available for us to use. For example, the Start method is presented here. And there is the Update method as well that we will use later. And even the Print method is up in here as well.
So, let’s have a bit of a challenge at this point. And I want you to go through the documentation of MonoBehaviour that I just showed, and try to find a method that maybe can help us detect that the mouse has been pressed on our game object. So, you have to go to the documentation, find the method that you think could help us, and then implement that method in our code. And print in the console when the game object has been clicked.
So, in our Unity code, we are gonna implement this other method. So, a method that will help us that will detect a mouse press will be added here, and inside of that method you will print in the console. Keep in mind that for that to work, don’t forget to add this balloon to our game object. So we haven’t done that. So I’ll do that now. So we’re gonna add it here. And now, we can see it in … Let’s see if there was some sort of error. Wait, it’s not showing, so let’s go balloon. Oh yeah, there it is. So it was showing it twice actually, so I’m gonna remove it once. And we have it there. So, make sure that the script is added here, and then play your game, and that is when you should be able to click the balloon.
So, have a try. It’s okay if you can’t do it. These are all things we hadn’t really covered, so I want you to try to get your hands dirty and explore the documentation a little bit, because that is the main thing here. You need to become familiar with the documentation, and the only way to do it is just to practice. So, have a try, pause the video, and then I will show you a solution.
Alright, so you had a try, and if we go to this documentation here, you might have come across this OnMouseDown method. So it says, OnMouseDown is called when the user has pressed the mouse button while over the GUIElement or Collider. It’s okay if you’re not sure what these things are. Collider is a component actually that detects that a collision has occurred with that game object. So if you see here, we have that Sphere Collider component. And we will cover that later in detail.
So that is okay, if it all sounds very new to you. So, this method then, it looks like it could be useful. And, in here we see an example on how it’s used. So, I’m gonna go to VisualStudio, and I’m gonna type void OnMouseDown, and you can see that it’s already anticipating that that method is here, because we are inheriting from MonoBehaviour. I’m gonna press enter. VisualStudio creates this nicely for me. When it comes to method, you can leave that private if it’s created automatically. I personally like not to have it, so that is just my preference here. So I’m gonna delete it, so that it looks the same as the other ones. And a lot of like the documentation of Unity as well.
So, when the user presses the mouse button, what was the other part of the challenge? We wanted to print something in the console. So let’s go and do that. Print, and let’s call this mouse pressed. Now, let’s go to Unity. Our script is assigned, and we can play our game. We’re playing our game. We’re now on the game view. And if we click on our balloon, you will see in the console that we can now detect that.
So we are finally adding some interactivity here, and this is very exciting, because on the next lecture, we’re actually gonna make this balloon grow, and eventually disappear once it’s reached its maximum size.
Transcript Part 8
Let’s now finish up our balloon experience or interactive demo and to do our quick recap we have our balloon script and we defined two public variables. We have the scale factor and we want to multiply the scale by that number each time that the user clicks and we have the maximum scale. Once the scale reaches top value, the balloon will be destroyed.
So, if we go to our code, we’re actually not going to use the start method here so I’m gonna delete that. And what we need to do here is be able to access the scale and if we can access the scale, we need to be able to increase this value and also we need to be able to check if we’ve passed the maximum value, the maximum scale value. So, I’m writing all this in what’s called psuedocode, basically it’s describing an algorithm in plain human language, in english in this case. So, we want to access the scale and we want to increase the value of the scale and we want to check if we’ve passed that number and if so, we want to destroy the balloon. And I’ve used the word destroy for a reason that you will see later.
So, if that’s not the case, we’re not going to do anything. So, first step, how can we access the scale? If we go to Unity, you will see that the scale of an object is part of its transform component. So, we need to find a way to access our transform component and, lucky for us, in Unity there’s a quick way to access the transform component simply using MonoBehavior we have direct access to the transform component by using just simply that in transform and that gives us the transform component.
And what exactly is the transform component? If I hover the mouse, you’ll see that it is of class transform and if we go to documentation, for transform you will see that there is obviously a lot of information and understanding the transform well is one of the most important things when you’re developing with Unity. We want to be able to access the scale so if I go here through the different variables that are available, I will run into this localScale variable and this is what we need to use access and to modify the scale of our balloon. Now, the scale itself is a value of, it is of type Vector3. In the next lecture, we are covering vectors in a lot of detail, so, for now, let’s just assume that Vector3 is simply a type of object that gives us access to values X, Y, and zed.
So, that is all we need to know for now about vectors and also that if we multiply a Vector3 by a number, we’ll be multiplying X, Y, and zed. As I mentioned in the next lecture we’re gonna talk about vectors in a lot of detail. So, we have here an example of how to access this and we are actually going to combine these two operations, we are gonna increase the scale. Increase the scale.
So, for that we’re gonna type transform dot localScale using autocompletion and we want to multiply this localScale, the new localScale will be the old localScale multiplied by scaleFactor. And that will work but when you have something like this, when you’re incrementing or multiplying something by assigning the result as itself times something or the value by something, you can just type a short version, which is, I’m gonna copy and paste, I’m gonna keep that there. Simply, the operation sign equal. That is the same as the above and this is just a better way to write it. So, that should already allow us to increase the size of our balloon. Let’s go and play our game and see what happens.
I’m gonna press play and if I click, you can see that the scale is being increased, and it’s being increased all the way to any number I want because we haven’t really set that maximum yet. And this also allows me to show you the game view and if we go to the scene view, we’ll select our balloon and press F to focus on that, we can actually see how it’s showing the values of game mode. If we stop the game, it will reset back to the original values and we can change its position, and change its scale, and do anything that we want but once we stop the game, it is set back to the original values.
And let’s press F to come closer, okay, so, let’s now make use of that limitation and make our balloon disappear, or destroy our balloon, if we’ve reached that amount. I’m gonna use now what’s called an if statement. So, I’m gonna type if and check for a condition. What condition do we want to check? We want to check that transform dot localScale and some number here, we need to check whether it’s greater or equal and that maximum scale.
But see how it is complaining because this is our vector and we are comparing a vector with a float number. What we need to do here is access either X, Y, or zed. So, if you go to the documentation Vector3, you will see that Vecto3 has variables, it has public variables, it has X, Y, and zed. So, we access the component and since we’re increasing all of them, we’ll just pick any, X will do. So, if this is true, if that statement is true then I’m going to execute some code in here. So, I’m gonna put this in here and what I’m gonna do is just type destroy and I’m gonna type gameObject.
So, there’s a lot happening here. We’re typing destroy, which is a method that allows us to destroy an object and gameObject is, just like we have that reference to the transform, we also have the reference to the actual game object itself, so this object here. When we type gameObject in a script, that gives us access to the game object of the game.
So, if I save that and I go back to the game and run the game, you will see now that as I increase the size, once I reach a certain point, it will disappear because we’ve basically killed it. If we stop the game, everything goes back to normal. So, to summarize what we’ve done here, we’ve accessed the transform component.
There is a way, that we’ll cover later, to access any component within a game object, because the transform is so commonly used, it’s so important, there is this shortcut, which is just typing transform. From transform, we’re accessing its local scale and as we saw in the documentation, localScale is of type Vector3. The type Vector3 represents a value of X, Y, and zed and we do want to modify the scale in X, Y, and zed. So, what we’ve done is multiply that by a scale factor and that multiplies X, Y, and zed, all of them.
So, we can take any of them and compare it to the maximum scale that we defined. Notice how this is greater than and then we add the equal to make it greater than or equal to a certain number. If that is so, then we call this function, this method which allows us to basically destroy the object and Unity takes care of that destroying process for you.
We are gonna have a challenge now. So, go to your script and add the start method back and instead of that method, create an if statement to check whether the value of scaleFactor is smaller than or equal to one. If that is the case, show a message in the console saying that the value is too small, it should be bigger, otherwise we’re not increasing the size of our balloon, we’re actually making it smaller and smaller. So have a try and then I will show you a solution.
Alright, you have your try and now let’s implement this in our code. So, I’m going to add this start method back. So, void start, that’s gonna create for me, I like leaving this private here. Alright, that is just my coding preference, my style preference, you don’t have to do anything like having that private keyword in there. So, what we said was that we were going to create an if statement, so, type if and what is it that we’re going to check? We’re going to check whether this value, scaleFactor, is less or equal than one. If that is the case, we’re going to print a message that says scale factor is too small. Let’s save that, go to Unity and let’s try this out.
So, I’m going to go here and change this to one, whoops, one, I press play and it says that the scale factor is too small. Actually, if I click nothing happens because the scale is just being multiplied by one, so, it’s not being modified. What if I set a smaller number, 0.5 and I press play?
You’ll now see that we are actually making that smaller and smaller. And just to make sure that everything works fine, let’s set it to two and now we can increase it and it disappears just like before. So, that was the solution of the challenge.
Transcript Part 9
Vectors are very important concept in game development and that is why we need to cover them. Let’s start with 2D representation of a vector. So I’m gonna draw my coordinate system here. I’m gonna have Y and X. And vectors are commonly represented by an arrow, like so. In math, a vector is a geometrical object that has both a magnitude and a direction.
So in this case here the direction is given by the orientation of the arrow and the magnitude corresponds to this length. So this is the magnitude. If you call your vector V, the magnitude is also written commonly like so with that line on top. So in order for it to have that magnitude the vector ends up at one point and that point has certain coordinates. In this case, let’s say that this point is located in one, two, so X equal two and let’s say that for Y, this is also located in two. Although to make it easier, maybe we can say that it’s located in three. So let’s say that this is one, two, three. So our vector V can be represented by the numbers two comma three.
Normally in math a distinction is made between vectors and points, because it is also how we write a point in space, a location in space. It can be in two dimensions or three dimensions. But when it comes to working with Unity, points are also treated as vectors. So when we talk about vectors we are gonna be using that for points as well. So a location, for example of an object, will be represented as a vector. So we have now this idea of what vector is and there are some operations that we can do between vectors. The first operation to talk about is addition. So let’s say that we have two vectors here so we have a vector called V and a vector called U.
How can we find the location or the result of U plus V? There is a geometrical way to determine that. I’ll explain the geometrical way, then I’ll explain the mathematical way, the actual calculation of that sum and then I’ll show you a couple of examples where that makes sense from an intuitive point of view and a game development point of view.
So the geometrical way to find that result is simply grab this, let’s say we want to add this one to this one, it doesn’t matter in which order you do it. All you have to do is just draw one vector starting from the tip of the other vector. So if this vector is like that, then I’m going to draw that exact same vector and the final point here is going to be that location that will be U plus V. So this V, this is U and this is V. When it comes to the calculation that is actually very simple. In just one dimension if I tell you add one plus two you will know that is three obviously.
But if you think about, if you had a coordinate system, you have one, you have two, and you have three. If I tell you this, I’m basically telling you go to one first, and now go two more. So one, two, and that leaves me in three. So it is the exact same thing that we did here. And what actually happens is that, if let’s say one of your vectors is X0 comma Y0 and you’re adding it to another vector that is X1 comma Y1 the result will simply be just adding each one of the coordinates like so.
If you are not much of an algebra person, I can easily show you with numbers. So if we need to add for example, one comma two and we need to add that to five comma 10, the final result will simply be six and will be 12 here. Because we are doing this set operation, we’re basically telling the person or the calculation we’re telling on X go to one and then go five further from that point, so that’s six. And in in Y the same thing.
Now a couple examples. I’m gonna start a new page to show you those examples. So let’s start with the first one. You are located somewhere on a map. So let’s say that this is you, this is meant to be you and that corresponds to a certain position, so a certain X and a certain Y. We’re gonna call this X0 and Y0. So this is your original position and I tell you now move out one to the right and move two up. That basically means where you are you move one in this direction and then you move two in this direction.
So I can make that a vector. In this case it’s a vector minus one, whoops, minus one because we’re moving on the negative direction on X and two. So I’m basically adding two vectors here and that is one example when you add two vectors and obviously very intuitive that the final result will be here.
So whenever you have an object in a certain position and you need to move it to a certain amount of meters or unit to units or feet from that position, you are basically adding vectors. So that is one example. Another example is when things are moving at certain velocity. So vectors are used obviously for velocity as well. And it’s actually better to make it with just the drawing. So if you are moving in a car in this way, a certain velocity, let’s call that velocity V, and if you throw a ball out of the window towards this direction and you throw the ball at a velocity U, the final velocity, the actual velocity of the ball, for someone who’s on the outside looking at that, it’s not gonna be just on that direction, because the car’s moving.
Let’s say it’s moving really fast. The ball will most likely, will obviously not move in that direction. It will actually be a sum of both vectors. So we can draw this U vector here starting from the tip, and the final velocity of the ball, which we can call B will be this vector here. So that is another example of where you can do it.
The other operation that I want to cover is scalar multiplication. So scalar multiplication. And that is when you multiply just normal number to a vector. So you have, let’s say a vector V that is equal to one comma two and let’s say that you want to multiply this vector by a number, by a value, let’s say 10. The way to do that is actually quite simple. So if you have something like this, the final result is you simply multiply this scalar value by each one of the components of the vector. So the final result here will be 10 and 20. This process is called scaling a vector. Something we need to talk about is how we calculate the magnitude of a vector. This is the length of the segment.
So for example for a vector V, the magnitude is usually written like that. You won’t have to do this directly when using Unity, but I think you still need to be familiar with how it is calculated. And that brings us back to, back in Greece this guy called Pythagoras. I’m not sure how we pronounce it, I don’t speak Greek. But basically he came up with this theorem where you have a triangle that has a right angle, and if the two sides are A and B and this side is C, you have a relationship that always holds true. That means that if we want the value of C we actually have to do the square root in here and that gives us the value of C.
When it comes to vectors, it works in the exact same manor. So if you have a vector here and let’s say that this vector is X0 comma Y0, the magnitude of that vector, so this is V. The magnitude of that vector will be the square root of both of these. When a vector has a magnitude of one, that is called a unitary vector.
So unitary vectors have a magnitude of one. You can make any vector into a unitary vector and this is also something that you won’t have to do manually in Unity, but the way to do it is that you simply divide each one of the components by the magnitude. So if you do that, the result of this, this is how unitary vector is expressed in this way. The result of this, the magnitude of that will be one. So you can do the math and you will end up with this having a magnitude of one.
And why is that even important? Well there will be times when you are making games, when you are working in Unity where when you’re only interested in the direction, and then you want to multiply that direction by a certain scalar. So you could have for example, you know that the current of the ocean goes this way and so you can express that as unitary vector. And then if you know the speed you can multiply that speed by the scalar and that will give you the velocity vector. So that is one way where this can come in handy.
Now let’s have a look at how Unity implements vectors. So I’m gonna open the documentation and bring you to the Vector3 page. The Vector3 is the object that Unity uses to represent vectors. And like I said, the representation of vectors is quite broad. We are using vectors for positions and also for other values that involve an x, y, zed, component, for example the scaled or the rotation when it is expressed in Euler angles, so in an angle for x, for y, and for zed.
So if you look at the documentation you’ll see that there are some directions that already come by default and these are all unitary vectors. For example, you can easily refer to the up direction, which is zero on X, one on Y, and zero on zed. So that is the up direction. These directions are gonna be used very often when working with Unity. And there are other variables that you can get like the magnitude or the normalized version of the vector, which I explained and there are some other operations that could come in handy with working with vectors.
Now what we’re gonna do is work on an example, and then we’re gonna end up with a challenge. So let’s open Unity and start a new project. I’m calling mine Vectors and Updates, because we’re also gonna cover the update method here. And I save my scene in the Scenes folder. I create an empty Scripts folder. And what I’m gonna do now is actually add an object here. It’s gonna be a cube. I’m gonna add that cube and what we’re gonna do is create a script that changes the position of this cube and when it reaches certain boundary, it goes to the other direction. So that will pre-practice the concepts that we’ve seen.
So in Scripts I’m gonna go and create a new script, and I’m gonna call this Cube controller. So I’m gonna open that. We’ve spoken about Start. You know what Start is used for. This is run when the first frame is running the game, so the beginning of the game. We’re gonna delete that, ’cause we’re not gonna use it in this example.
The update method is something that we haven’t covered, but it’s clearly explained here what it does. Update is called once per frame. So in each frame of your game, the update method is executed. In the documentation the update method is under MonoBehavior, because it is the MonoBehavior class that brings this method to us, that we can use. And it is explained here as well, when it is used which is on every single frame. So it is common to add things here, that you want to check at all times.
And in this case we want to be moving our cube at all times and we’re gonna do that from here. So we can access the transform of our cube, ’cause we’re gonna be adding this script to our cube. We can access it by typing transform. That gives me the transform of my cube. If I type position, that will give me a vector, a vector position. And if I want to move my position, let’s say up, so we said that we had this up direction.
We’re going to move our position up and for that we need to type in, we want to be adding something on each frame. And what is it that we’re going to be adding? We are going to be adding Vector3.up, so we’re gonna be adding that vector, which has a magnitude of one. So let’s start with that. And by the way remember that plus equal, this is the same as doing transform.position equals transform.position plus Vector3.up.
So let’s start with that and you’ll see what happens. So I’m gonna drag my script onto my game object and I can see that it’s been added and I’m gonna run my game. So you can see how it went up really, really fast because we’re moving it too fast. So let’s have a speed that is not as large. And we can add that speed as a public variable and give it a default value of 0.1f, public float speed.
So what I’m gonna do now is multiply this here. And as we saw, this means that each number in the vector will be multiplied. So let’s try again. Let’s go to cube and see how we can now enter a different value here if we want and if we press play, our cube is moving up. So that works fine. Let’s add something else to this. We can, for example, check if the location is too high, we can make it go back. So we can make it bounce up and down.
To implement this up and down behavior, we need to check the position of the cube. So if the position of the cube has reached a certain number, let’s say we want our cube to, if it reaches 10 in the y-axis, in the vertical axis, if it reaches 10, then we want it to move down and when it’s moving down if it reaches minus 10, we want it to move up. So for that we’ll be using an if statement, which allows us to check a condition. Before I write any code, I’m gonna actually write pseudo-code, which is instructions of what we want and the steps that we’re gonna take to get there.
So we want our cube to move up and down between values of minus 10 and the value of 10. So if we reached the position of 10 we now need to down. To be even more precise, if we reach the value of 10 and we were moving up, then we need to now move down. Also if we reached minus 10 and we were moving down, so we were on our way down, and we reached that point, then we need to move up. So now that it’s clear what we want to do, let’s actually write the code. So we’re going to write an if statement here and we’re gonna a check the position of our object. So I’m gonna type in transform.position.y.
Remember that the position is a Vector3 and I can access x, y, and zed in this way. So I’m gonna check if this is greater and equal than 10 and also if we were moving up. If we were moving up, that means that the speed is positive, because up direction is a positive direction. So I’m gonna write it in English first. I’m gonna write and speed is greater than zero. The way to represent and in code is with two signs like this. So this means and. Both these conditions need to be true in order for this code to execute.
And what exactly is this code gonna do. Well if our speed was say one going up, now we need our speed to be minus one. So we need to multiply speed, whatever value of speed we have by minus one. So the new speed needs to be the old speed times minus one. And as we’ve been doing, same that we did here, we’re actually gonna write this in a better way. So this is the same as this, we’re multiplying the speed by minus one. And now our cube is going down.
What about the second condition? We can type it here. So if transform.position.y is less or equal to minus 10 and we were moving down, so and speed was less than zero. So if that is the case we again need to change direction and we’re using the same code, ’cause if the value’s negative this will make it positive. Now it doesn’t make sense to check both conditions each time because if this condition is true, we don’t really need to check the second condition. For that we can use an what’s called an else if statement, where this condition is only checked if that condition was false. Now that we’ve written this code, let’s see it in action. I’m gonna press play, which is gonna activate game mode.
It’s going all the way up and all the way down. And if you monitor the values in the transform, it will show you that it reaches 10 and then it goes back down. Before we go onto challenge mode, there’s only one thing that I wanted to point out. It’s not relevant at this point but it’s something I wanna mention that will be taken care of later in the course. Basically update is called once per frame, but the frame rate of your game might not always be a constant.
Sometimes if you have a very slow computer, the frame rate might actually go down, so the frequency at which this is called is not constant. And that means that this speed value actually should, we should adjust this by the time that has passed in between these iterations. But don’t worry about that now, but I just wanted to put that out there, that this is not always called at the same interval of time. So now that we’ve done this, let’s go into challenge mode and have a try at the challenge and then I will show you the solution.
All right, so the challenge starts by creating a private variable that is of type Vector3. Let me show you how you can do that. So we have this public variable here. Just create another variable, but instead of float it needs to be type Vector3 and you can call it however you want. You can call it say v. Then this next step is to add the Start method back and instantiate that variable to any vector you want.
What exactly does that mean? It means that you’re adding again the Start method. Just so we have create, you have to add Start method again, and then inside of that method, let’s say that you called your vector variable v, a way to instantiate a variable is to type the equal sign, it’s assign a value basically and type in new Vector3. And Vector3 needs to a have a value for x, for y, and for zed. So you are creating a new vector in this way and you’re doing that inside of your Start method. So hope that that makes it more clear.
Then in Update, you’re going to use that vector that you created instead of Vector3. So we’re no longer gonna have Vector3 here, instead you’re gonna have that vector you created. And the last step is not related to the other three steps. And it is to make the, it should just say Y here, so ignore the X part, it should just say Y. The Y hard-coded values should be public variables. So what does that mean? So we have these hard-coded values, is when you have numbers in your code, which is always undesirable. So you shouldn’t have this 10 and minus 10. It’s better to create variables for them. So you’ll have a minimum value and a maximum value and you can assign those from the inspector.
So have a try of this challenge and I’ll be right here for when you finish and I’ll show you the solution. All right, so you had a try and it’s okay if you didn’t complete because this was a very difficult challenge and I tried to incorporate a lot of the concepts that we’ve seen before. And the main, what pays off really here is just the trying aspect, because all of these things, you’ll get accustomed to them as you repeat over and over and over. Eventually all of these things will make a lot of sense and hopefully be very intuitive and easy as well.
So let’s start by creating that private variable first which is of type Vector3. So I’m going back to my code and to create a private variable, I can either type private or I can not type anything. I’m gonna choose not to type anything, just type Vector3. I’m gonna call my variable vector and that’s it. That’s part one. Now part two, add the Start method back and instantiate that variable to any vector you want.
So I’m gonna add my Start method back and Unity auto-completes that for me, but I like to delete that private part and I’m going to instantiate my vector to a new vector object. So there’s new Vector3 and I’m gonna enter the value of one, one, and one. So that is my new vector that I’ve created. Part three of the challenge is that in Update use the vector you created instead of Vector3.up. So I’m gonna copy that and I’m gonna paste it here, replacing the Vector3.up. And we can see now actually in Unity, we can see what will happen. We will get that happening.
Last part of the challenge. Make both the range in X and Y public variables, so ignore the X part, just the Y part here. Sorry about that. So the Y part is this one. And I need then two public variables. One is the minimum Y and the other one is the maximum Y. And I’m gonna give this default values that we already had before. You don’t have to give them default values, that’s just an option. So instead of having that minus 10, now I have that variable. And instead of having 10, now I have my variable here.
The process of cleaning your code in this manner, of organizing your code, is called refactoring and it’s a very important process. So now our code is more clean, because it doesn’t have hard-coded numbers. And if we go to Unity you’ll see that your cube, once this refreshes, it has the values that we enter and we can obviously enter this, so I can have a much more tight range, and I can play my game and I can see what happens now. So we have that going now and that’s all for the challenge and that is all for the lesson. And feel free to come back to this lesson if you need a refreshment on vectors or any of these topics.
Transcript Part 10
In the following videos we’ll be creating our 3D platform for our game. But before we jump into coding and building and using Unity, there is a concept that I wanna cover and it is something that we should be doing before writing a single line of code in our game. Which is, a game design document.
Game design documents are used all over the game industry from big studios to indie developers. And what they are is a very short document that describes the main concept of your game. There’s not a single template for these sort of documents. So it really depends on the scale of the project and whether it’s something just for yourself, or you’re working with contractors, with a team, or in a large organization. But, some things in general you should include have to do with the concept of your game, what your game is about, what the main game player or game mechanic is. And also, describe some of the assets you will be needing.
There is a very good article in that link which is an article on Gamasutra that describes the anatomy of a game design document. But in my opinion that is more for the corporate world. It’s still interesting to read and can give you some things depending if that is your case. It might be very useful but in a smaller scale you want to do something a little bit simpler. And again there’s not a single way of doing it.
So what I’ll do now is just the sort of document that work well in my case. So I’ve already created a template for our document. The main concept of our game is going to be a 3D platform game. Kinda like Mario where you run around collecting coins, avoiding enemies and you have to reach the goal in each level. So, our game will be built for desktop. Which includes Windows, Mac, and you can build for Linux as well.
So Unity allows you to build for all the different desktop platforms very easily. And now I’m gonna describe a little bit more of the concept but not using words but using sketches. Which is what really works well for me. The level of detail of the sketches will depend, obviously, whether you are sharing this with someone else, with contractors or team, or it’s just for you. So, my concept, what I have in mind is having very small levels that are kinda like platforms.
The art is floating in this space. In those platforms there is a player which is gonna be like cube with a face. So that will be our player. And forgive me for my horrible cubes and bad lines. So the player move around, it will be jumping, and it will be collecting coins. Which will be your scores. You’re gonna see your score up here. And it’s gonna show you how many coins you’ve collected. It’s not just gonna be flat like that, but there will be platforms that you can jump into.
And in each level is going to have a goal, eventually. Which will be like a portal, like a sphere. Levels are also gonna have enemies. These are gonna be bad ass enemies that move up and down. They just move up and down, that’s all they do. So they have a certain range and they move up and down. So you have to avoid them so that they don’t crush you. You will lose if you run into an enemy or if you fall off then you lose, game over. There will be levels that are sort of like these and other levels can have multiple platforms like that. And the camera, the camera will follow the player as the player moves within the level. So the player can move in this direction and the camera will follow.
You can also extend the game to make it so the camera follows you in any direction. And there will be all sorts of platforms with enemies and lots of coins. So, this, I think is enough to give us an idea of what this game will be about. And what I’m going to do next is just copy this thing into my document. So you can use whatever clipboard, or paint, or anything. Just grab the image and then put it in here. ‘Cause these things need to have sketches and images.
And obviously if this a more, if you need to communicate this with someone else you might want to do something better. I usually work with very simple images with my team. So this can do, then I can describe my game more if I want. Some of the game mechanics that we mentioned include; player will be controlled with the arrow keys, and there will be jumping. So those are the only things the player can do. Game over if you run into enemies or fall off.
Which also brings me to, I can describe a little bit of the UI. There will be a Home screen. There will be a Game Over screen. We’re going to be showing, we’re gonna collect coins and show the score. And I also want to have a high score in my Game Over screen. It is obviously multilevel. So you find a goal and move onto the next level. And we are going to play a sound when collecting coins. I think this describes the main thing. Enemies will move up and down. And there will be platforms on different heights that you can jump into.
So, as you can see this is a very, very simple description of the concept of the game, but it already gives me a starting point. And these documents are usually updated as well. As the game makes progress. Now what assets are we going to need? We are going to need some models. So we are going to need our model for the player. So I’m gonna write, subtitle, models. We’re gonna need a model for the player, for the enemies, probably something for the terrain. And I’m gonna write the coins and the goal.
But in reality we will be using Unity’s primitive shapes for this. That is something you can decide as you go. Initially it was a requirement to have a model for those things. Also, what about textures or images? I will want to have some sort of rocks background. That I can put in my ground and also in the bottom of the game. Then I also want, what about audio, I want the coin sound. Coin collection sound. I think that’s all I want.
Then for the UI, I wanna have a cool pixel art font. And I also want a background for the Game Over screen and Home screens. So I think that is all that we need. And in this manner I can always add more things if there’s something that I didn’t think of. I can add it later. Okay, so now we have a clear idea of what we need to build. And this can obviously be communicated with other people. The very next step, because some of these things might take time.
You might be giving these model jobs to a contractor online, or a friend of yours, or someone else, or you have to do it yourself. But you also wanna work on say, the game mechanics. What you can do next is a prototype. And that’s what we’ll be doing in the next videos. Which is when you are creating the game mechanics before you have the assets. So I wanted to do it that way in this course. So you could see that process and see how it is when we add the models later. Which we will. Alright, so that is the basic of game design documents. And now, I have an open ended challenge for you. Which means a challenge that you have to complete and that we’re not going to be showing you because obviously it’s your game idea.
So, what I would like you to do is to make a game design document of your game idea. And if you don’t have a game idea, come up with one. It can be similar to existing games. If you can’t come up with one, just pick your favorite game and make a game design document for that game. So that you can mentally deconstruct how the game was built. And besides thinking of the finished game, I’ll also want you to think of what would be the simplest possible implementation of your game idea.
So if you wanna make a game that has all this class of characters and enemies. Could it work with just one type of character? One type of enemy? Could you implement that? Would that make it easier for you? Because the way to reach higher goals when it comes to projects is to make them into smaller chunks. And then smaller chunks are much easier to work on. And it’ll take you, maybe, just a few days to do a simple prototype.
Where as a full game could take months because of all the assets. So, I really want you to start getting into the mindset where you can work on your ideas with just the minimal elements. We’re using default assets. Using whatever comes in Unity or free things from the web. Just to get things going. And then you can make things more complex. As you go, as you get some feedback from players or from friends or testers of your product. So, that is all and I will see you in the next video.
Interested in continuing? Check out the full Unity Game Development Mini-Degree.