01a. Getting Started with Unity - Part 1
A free video tutorial from Mammoth Interactive
Top-Rated Instructor, 700,000+ Students
4.3 instructor rating • 279 courses • 254,006 students
Learn more from the full courseComplete Gaming Package: Learn To Code In Unity® And Unreal
Learn to code by building games in Unity® and Unreal Engine. This is a fantastic course to start learning technology.
53:50:44 of on-demand video • Updated September 2019
- Be able to make 2D and 3D video games
- Code in C#
- Design Games
- Build games from scratch
English [Auto] Right let's go ahead and let's make a new project here so we can go and make a new project here. And what I'm going to do is I'm going to call this game prototyping road to typing. And we'll be talking a lot about prototyping here. But the location I'm also going to make it as unity projects and we also want to make sure that it is 2-D only. OK. So we want to make sure that we have a name and you name it whatever you want here and I'll maybe make this unity projects and what that's going to do is if you really want to you can go and select an item here. So what I might do is go to documents here and I may go to a new folder here and I'll call this unity projects. And you always want to back up to the cloud here. So we'll choose that here and it will say game prototyping. And if you want to you could go ahead and put in for example P-BO one or something that we are one to tell you which prototype or you can put the date in there or something like that. Let's go ahead and push create project and this should go ahead and create the project. It might take a little bit of time. And if it does take a really long time you can always pause this video and you can go and add that in here. Now remember this is set up so that we can go and make our our game here. We have the scene where we drop everything in. We have what the game looks like and we have our assets in perspective here and all of these windows and a lot of these buttons will become very very important to you and well known to you. So the first thing we need to do is add in a sprite. So we're going to go to game objects to the game objects. Right. It adds a new sprite. And I'm just going to go ahead and I'm just going to call this player. So you can just click on that here and if you want to you can go and click on it and what you'll do is you'll see that cursor come up and you can go in and just put whatever you want push enter. And there we go. Now if we look over here you'll see that we have the Spryte renderer. And what this bright renderer is is how much how much the sprite will actually function here. So we also have the Spryte the color. We also have the flip you can flip the x and y. We also have the material as you can see here. And then we also have a layer here. What we need to be concerned with right now is the Right now the one thing about unity that unity does instead of a lot of other programming languages is it allows you to only add in graphics here. Now you can go when you can add in your own custom graphic at this point but for now what we're going to do is we're going to click on this little button here and you can pick any one of these that will work here. I'm going to put pick a square and I'm just going to go ahead and close that here. Now if we click on the main camera you'll see that this is a preview and you see that little tiny dot that is bright. Now we do not want that to be bright here. So what I'm going to do is I'm going to change the scale to X Y Z. I'm going to change it all. 15. Now at this point in time we're focused mostly on learning how to code and add something to Unity graphics is not our main concern at this point. OK. So if we add in and this has to be done to the player by the way. So 15 15 and 15 and you can see that now the player is indeed bigger. OK. Now if we go ahead and click on that main camera here and we might as well set that to 1 to 1 to 1 again. Here you can see that the preview is indeed a little bit bigger. So we run the games so that it will take a couple of seconds. There is our player here. OK. And you can go ahead and push the player key to play it and the pause key to kind of pause the game. OK. So here we go. And this concludes the Tauriel Welcome back. All right so now that we have an object here what we need to do is we need to add in some code. Now there's two ways you can do that. OK. First of all you can go to your assets window here and you can go to create and we're going to be only working with C-Sharp script here in go ahead and create a C-sharp script. And if you want you can see that it comes up with this C-Sharp script and it will also add in the the bottom here so that you can go and edit it. So for instance you can say that this is my script just like that. And there you go. Now as you can see over here you can see that there is a little bit of a preview. So you can see what it looks like. All right so that looks pretty good here. Now the other way you can do that here. And what happens now is that this script is kind of floating in space. It is not. It doesn't do anything. It's not attached to anything. So you have to attach it to the sprite. So you just simply drag it over which I just did. And as you can see under this inspector here we have our transform We have Spryte render and now we have my script. We have my script here and that is now attached to it so any code that we write in there will now be attached to this specific game object. Now if you want to you you can for example add in a component. OK. And you can see that there is a new script down here. OK. And if you click that you can see that it gives you a language C-Sharp and the name will be you know we'll call this sample script. Just like that. And I always like to make sure that there are no spaces. Call it an old school programming habit but that's just me. So you create an ad and now you can see that sample script is now up there. OK. So there's two ways you can add that little scripts in there for you. Now if you want to you can click on this little gear button and you can go and you can say remove component. OK. And that removes the Koepp component. But notice it doesn't remove the script. So we have to click on this right click and push delete game and we're going to go ahead and say yes delete that. And there we go. Now let's double click on this here. Now what should happen is that this mano develop should pop up here. Now modern develop if we kind of look at the icon here it should kind of look like this. Now I always have them right next to each other and if you're on a PC it should also open up here. Now if it doesn't open up there's two things you might need to reinstall unity because it does come with the unity and if Visual Studio comes up then you need to play around with it in order to make sure Monda comes up. Now if you really want to use Visual Studio you are more than welcome to the reason why I'm using Monod develop is because it looks pretty similar on a Mac and PC so that most people will be able to see it here. OK. And here here is our actual code. So we have our using statements and these are accessing libraries so we can actually go make our game here. So there's code in the back end that we reference to make sure that it works. We have our class is where all of our all of our work here. And we also have a start void and an update. We also have these comments here. Right. And the way that the computer works is it reads the code line by line and if you put in two slashes like this it will not read that line anymore. So you could add in little notes and stuff like that there. Personally I only use comments when absolutely necessary. But we're going to take a look at what the start an update void is in another tutorial. But right now we have we have this going here now before we end this here. I'm just going to add in one other item here. So right now we need a whole bunch of edits to this. And what I'm going to do is I'm going to save it. OK. And we go back in a unity OK you can see that. Now this script is now updated. OK it's different than before. You have to save it because you don't save it. The script won't update and if the script doesn't update all your code that you add will not be applied to the game. OK. So let's talk a little bit and actually do something in the next story. Let's actually do something with this. So how to do that. Well let's go to our model developed here and we're going to talk a little bit about how this voyde start and void update work. OK right now let's go ahead and let's add in our own code here. So what we're going to do is type and transform. And as you can see we have all these different options that automatically come up. Right. And you'll also see for instance that there is this this side here and it gets a little bit transparent when I go and use the magnifier. So let's go to transform and we're going to say Dot rotate. OK. And then we're going to add in the comma or the bracket there and you see you have different options if you use the arrow key you can add in all these different options here. Now the reason why this is that there's many ways you can rotate or transform and one that we want is just float x y and z. Now what we're going to do is we're going to just type in 10 10 and 10 calls off the bracket in the semi-colon. Save it and let's hop back in unity here and let's run and let's see what happens. All right maybe it may take a couple seconds here but wow look at that. That looks pretty good. Now another thing you can do is you see that there is this kind of button here that says maximize on play. Well if you don't push that for instance or if you go to the game here and you can just see that it won't maximize on play. And that's actually something that works out quite well in see that it is rotating here. Now let's talk a little bit about what just happened. When you attach a script to the game object. OK. So we had member we attached the script to the game object and we go back into the scene here. We click on this this my script is attached to it the transform. We are accessing that object that is attached to. So transform is just another fancy name for this game object transform game object game object transform. So we've got this transform and we're telling it to rotate. All right. Now you'll notice that the square just keeps rotating. OK. Now why is that. The reason is that the update method is constantly called and it access to something called the game loop. So for example if we just drew a square on the screen and we put that drawer in the update it wouldn't update just once it would update like 60 times a second. OK. And what that means is that this update will get constantly called. So in order to make any kind of games you need to constantly call these updates. So if you think about this and I played here the update method is getting called again again and again that's why it's rotating now. In contrast to that if we copy in or cut and paste this code in here it will only do it once. OK. So when a void gets called it executes the code it runs the code of course remember to save it hop back in a unity and we'll run it here and it will only it will rotate but it will only rotate once and you see that it doesn't animate there. And the reason for that is that the Start gets only called once at the beginning of the game. So we have the start we have the transform it rotates 10 and it does so update constantly gets called Start only gets called once right. So that is how we can go and rotate. And that is the difference between void and start. Now what we'll do is we will talk a lot about these two voids here and we'll add our own voids as well but these two voids are super important. And we will understand how all of that works when we go in maigre games. All right. This includes the Tauriel. Let's take a look at something that's really important here and that is the idea of variables. Now what variables are going to do is they're going to store data. Now we store all kinds of data when we make games we can store purchases we can store the player's score we can score store the player's height. We can also store things like you know the players speed projectile speed and jump height pretty much anything that you see anything in the game will be stored in some kind of data. OK. So first things first is let's take a look at the different kinds of variables. So the first one will take a look at is an integer. Now in order to declare a clear variable we're going to say. And we're also going to say integer and we need to say int which is an integer which is the positive or negative number. We'll call this some ench and make that equal to 1. Now you can't make it equal to 1.3 4 because it's only counting numbers again positive or negative. We can also have a float. We'll call this some bloat. Make that equal to one point 0. After all make it equal to something a little more interesting something like that here. So some float is two point three four. Now there's also another kind of variable called the Double we'll call this some double make that equal to two points will make it equal to something like negative seven point seventy seven. OK. And a double is also a number that you can use positive or negative. And it's also a decimal here. OK. So Lekan add in another kind of variable called the string nélisse string. Call this the string make that equal to Mammoth interactive K and also by the way you do need a semi-colon at the end of each line. So a string is characters now a character is obviously you know words if you want to enter a player's name or like a U or L address or something like that. That's when you use a string. Now the thing is is that if I add another string here we'll call this some other string and make that equal to two. Well here's the thing with these number variables. Up at the top here you can go and add them together you can add it together you can add floats together you can add doubles together but when you can't do that and into a float or double to a float. Likewise if you wanted to add in the string here and it says that it's two you can add that to a number here. Strings do not add the same way as numbers do. So just keep that in mind here. Now the other thing that we're going to do is we're going to add in a boolean variables so a bool is either true or false and we'll say some bool is equal to false. Now you can either make it equal to true or false. Right. So there you go. Now let's say for instance you wanted to change the value of some and K so if you wanted to say some and is equal to two. So when the game starts because remember it's in that start void some and will be equal to two OK. And let's do you know some ends is equal to some inch plus two. So at the end of this summit will be equal to two and then we will add let's say three just to make it less confusing. So when the game starts some it will be equal to two and then the next line says some it is equal to some inj plus three. So now will be equal to five. Now you can add as many different kinds you can multiply it. You can divide it. You can do whatever you want to that it can. But again what you can't do is go to some ints is e is equal to some bloat that won't work you cannot mix and match variables like that data types are super important. Likewise if you wanted to do some bhool is equal to stalls here you can go ahead and change it to false OK. And there you go. So that is what variables are. Now again I'm going to give some examples. Now these examples are just simply examples of things that I tend to use variables for and what it is is that it's intro's to give you a very good example of what used variables for. So for example I can say in scores equals zero in player with is equal to 10 or 12 inch player Heights. Is equal to 12 here. And again these are just examples of what I normally use floats projectile speed is equal to two point three four half float jump force is equal to one poignant one. Now by the way you see how I don't capitalize the first word and I capitalize all the other words that so that it's readable. So for example if I take a speed like this it's still the same thing it's just doesn't look as nice. OK. Bool in an app purchase we'll call this coin's can equal to false. All right. We can also have a string player name. Now one other thing we'll say temp name just like that. And one thing you need to understand is that this looks like a lot of variables right. One when you make your games and the more complex your games become the more variables you'll have lots of commercial games will have 100 variables. Now there's other ways to add in our data. We don't normally just use just variables we use other things as well. But for now let's just deal with that there. All right. So in the next tutorial welcome that. So what happens if we want to for example change the rotation rate in the editor here. Well it's pretty simple let's go into monor develop here and I'm going to delete all these variables and I'm just going to delete the space here and we're going to do is we're going to make it a public float and we're going to call this X rotation OK. And make that equal to ten point zero. And we need to do is I'm going to copy and paste that in once twice. And since you can't have variable names that are exactly the same going to say why rotation in zero station. OK. Now why did I choose floats. Well remember if we go back to this rotate here and we look at this gloat you can see how it says load X angle Well you have to match the like variables with the like variables. So in this case we're going to do X rotation comma y rotation and of course Z rotation semi-colon. Now let's save that out here. Now when you add something that's public you can see it in the editor. So once this comes back here you'll see that the script should go an update and that update you'll see that the rotation is there. Now watch what happens when I run it. OK. So run here and everything's working. And you can go and you can change it here so you can change the rotation to well 0. OK. Now keep a look at these numbers here. OK so keep a look at this. You got 10 zero and zero. And let's just stop the entire thing. OK. Now if we go in Stav the game here everything moves back to 10 and the reason is that any change that you do when you are running the game will get completely erased when you stop here. OK. So why is this important. Well let's say you want to build a platform game and you want your player character to have several different options that you want to tweak in the game like jump height jump speed you know bouncing kind of you know whether it has you know certain kind of power ups like a fireball or something like that. And you want to take a look at and add those in there as are working with the game. That's when you make it public. All right. This includes the Dorio. Let's take a look at specifically void's here. Now right now we have a star void and an update void. But can we add in other voids and the answer is an absolute yes. So if we type in void for example let's say rotate the transform. Now I haven't actually coded anything within rotate the transform but you probably know what it does. Now I'm going also and the curly bracket here and now we have the void rotate the transform and this void works that everything within that highlighted area or anything within that curly bracket is indeed rotate the transform. Now we put this if we cut this we paste it in here. We have rotate the transform everything should work out fine except for we haven't called rotate the transform yet. Now if we call rotate the transform in the start it will only call it once. If by cut and pasted in the up days what will happen is that the program will run exactly the way it used to. So the void update will call this Rotti the transform and this rotate the transform will get called every single time update gets called. Now this here. These two ways of making your program work is probably the best way of doing it. Adding in a lot of different voids that have specific functionality. Now I'm going to add in some example void's. So for example voyde move a player. OK that's an example void another void would be you know game over logic and will move the table will be the game of logic here and you can see that you want to separate all of your functionality into this in here. Avoid shewed fireball et cetera. And again these are just examples of what you can use voice for now you should have as many voids as there are functionality. The reason is is that if I was a person that was just coming onto the project and I see voyde rotate the transform Well I think I know what that does. If I see moves the player I can guess what that does if I see game over logic I can guess what that does. And of course I see shoot fireball. Again I can guess what it does but if my right avoid like this I have no idea what that is and you won't either because sometimes you might go on a vacation and you can even go for lunch read some code go for lunch and when you come back you have completely forgot what you wrote. So you always want to be specific here. So we go ahead and save this and hop back in the unity here and run it everything should work out just the way it used to. Right. Give it a couple of seconds and there you go. You can see we have this nice rotation transform here. OK there we go. This included tutorial.