LEARN UNITY - The Most BASIC TUTORIAL I'll Ever Make

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys and welcome to my channel my name is stefan paschan also known as infensia and this time around i'm gonna be making the most basic unity tutorial that i will ever make if you're brand new to unity or even if you wanna brush up on your basic skills or if you want to write your first c sharp script even though it's a very basic one this is a video just for you i've been using unity for over eight years now i've created a lot of assets that i sell in the asset store and i create a lot of small games especially for ludum there for example i've done 11 consecutive ludum dare games where you have to make a game from scratch in only 48 hours and currently i'm working on a multiplayer rts game with a colleague of mine and we're one year into development i'll be announcing that later on during this year so again sit back have a cup of coffee a can of coke even a cup of tea if that's what you want and let's just get started with the most basic unity tutorial ever so i've downloaded the most recent version of unity that's available at the moment 2019.4 and when you start that one you might get this unity hub up i've got a lot of unity versions installed since i work with a lot of assets so you should hopefully not have this many first i'm going to use this little drop down here to ensure that it's the version of unity that i want and i want the most recent one that i've downloaded yesterday 2019.4.4 f1 so it should look pretty much the same in any version of unity that you've downloaded if it's fairly recent and then i'm going to make a 3d project here and we're going to just name it something like unity basics and then i'll hit create okay so i normally use the professional skin but i've reverted back to the basic skin now so it should hopefully look very similar to this with a color scheme and everything like that if you're not running the plus or pro version yourself and i've also reset to make sure i've got the default view so the layout of the screen should be pretty much identical if you've started a brand new copy of unity first let's cover the user interface a little bit here in part one and in the center here you have something called the scene view and imagine yourself that you're a director of a film so basically what the scene view is is everything including the stage but you also have the cameras the lights maybe some actors off stage a little bit that are about to come in in just a moment so that's the scene view basically you see everything that will make up your game and this is actually where you're going to be designing your game as well on this second tab here you have the game window and that's basically what the camera sees so imagine that as if you were in a theater for example in a movie theater and you were looking at a big screen and whatever goes on on that screen that's the game view or for in our case it's going to be the player that's playing the game whatever the camera sees or what the player should see is what's going to be visible in this game view and you can see that by default here it's got this sky box with a blue sky and a horizon and if we look in the scene view we can see that it's actually this little camera here that's uh looking at something and as you can see here the camera itself and the light is not visible they're just showing the scene and lighting the scene on the left here you have something called a hierarchy and that's basically like an inventory list so of everything that's in your scene so they're very linked together the scene view has a lot of stuff with icons and items and the hierarchy is a list of that view and it's a collapsible list you can collapse and expand a few of these things here as you can see you've got the main camera which is the same as here and the directional light if you click on something here it'll also select it in the scene view so if you click on it in the hierarchy it will select it in the scene view as well you can arrange things up and down here by selecting something and dragging it and dropping it above and below this list and normally it doesn't really have that much of an impact for your game but if you're making a user interface or if you're making a 2d game the order will make a difference because it's a rendering in the layer from top to bottom but if you've got for example a ui it will render from the top and down so if you have a lot of boxes or windows that are on top of each other it'll render them in that order so it's going to be the one that's furthest down that's going to be the one that's on top but in a 3d viewport like this like in our game we're not going to be needing it to sort any of this because the sorting is taking care of the camera and the distance that objects have from the camera so in this hierarchy view you will have a lot of stuff for your game for example you'll have if you're making a platform you'll have a list of a bunch of tiles and you'll have your player the weapon that he's carrying all the enemies that he can encounter and they'll also be showing somewhere in the scene you could also instantiate stuff during the runtime we won't really be doing that this time because uh when you make a very simple game you can basically throw everything into the scene and have it active at all the time but when you're making more and more complex games you'll want to have stuff to be created and removed dynamically but again that's uh for a later video down at the bottom here you have something called the project window and by default it's split into two parts you've got the left sort of a folder view here and on the right you'll have a bunch of icons and it's very empty at the moment i usually use this little drop down to do a one column layout sometimes but the default view is two columns and that's what i'll be using now think of the project folder as a warehouse for example again if we're making a movie and you're the director and you want to make it that film the warehouse or the project window is where you'll have all the stuff that's stashed away for later use maybe it's stuff for your next scene or a future level maybe it's those tiles covered in snow or the next boss that you'll encounter a few levels down the line and then finally in the ui here we have on the right side something called the inspector and when you have something selected in the scene or in the hierarchy here whichever then you'll have a bunch of stuff for that object that you've selected in this inspector just think of it as a magnifying glass that zooms in on an object and you can change some values here for example you can type in different values you can drag some sliders use the drop downs buttons you can add and remove things we'll go through that in in a lot more detail in just a moment so that's the inspector and you'll be using that one a lot we're going to be talking about game objects transforms and components and those terms are very common in unity let's start by creating a game object i'm going to right click now here in the hierarchy and we're going to create something called let's create a 3d object and let's make it a cube and this is one of the primitive that comes with unity by default and it's created a little cube we can see it here in the hierarchy but we can also see here in the scene view what we've just created now is called a game object and everything in unity is a game object this camera is a game object this light is a game object and this cube that we've just created is also a game object and a game object is a collection of stuff like something called a transform and a lot of components that we can add to this game object not only did it appear here in the scene and in our little inventory hierarchy list here but it also appeared in the game view because we happen to create it in the center of the scene and we've got a camera that's looking at that one and the game view will automatically update as you make changes to your game if we even select this camera here you get a little preview down here in the lower right corner and you can see that it's uh even though it's a little bit faint you can see that it's actually looking at this cube and unity's coordinate system is working on x y z where z is forward and that's the blue arrow here and the camera is always looking forward in the forward direction and as you can see here the camera is looking at this cube and we see it in the preview and we see it here in the game view itself we can rotate the view here if we hold the alt key and then the left mouse button we can rotate around in the scene view here as we edit we'll need to be able to rotate and see stuff from different angles when we want to place things or modify things and that has no impact on the game itself as you can see when i rotate here the camera is still looking forward in its own direction and it's looking at that cube also in the ui that i haven't covered yet is in the top left here you have a little toolbar and you've got stuff like a move tool for example you've got the rotate tool and a scale tool you can also toggle these with the keyboard so w is this move tool e is the rotation tool and r is scaling tool so if you're coming from the world of blender where i do a lot of my work then it's not the same keys so it's just a brain melt but you'll get used to it in the end you just have to adjust a little bit and then rethink it if we press w now or we click this little move tool here you can see that you get this arrow gizmo on the on the game object and if i slide it here you can see that the camera is moving forward and we'll get closer to this cube here and i can slide it back and we can go up and down and also if i press e or select this rotate tool we can rotate it on an axis so we rotate the camera in different angles but let's move that one back to the default location we can press ctrl z a few times and the camera is back to its default location here and then we can also select the cube instead either by clicking it here or we could have also clicked on the cube itself here in the scene view and in the same way here you can move it up and down left and right and position it where you want it in your game world i remember that we talked about this inspector thing on the right here and as you can see when i drag this object here it updates the position so if i go up it changes the y-coordinate and if i go down it changes it down we can also just click here and press zero click press zero and zero so we can type in values there if we wanna exact positioning there so what is a transform anyway a transform is the position rotation and scale of a game object every game object will have a transform in your game and again it's the position rotation and scale of that game object and by default the scale is always one and that's what you should try to operate at most times you get better performance if stuff is scaled one and you also have less problems if you have a hierarchy of game objects if they're scaled differently it'll get a little bit confusing with the scales and we can also see a few other things here in this spectre and when you create this default game object or the cube not only did it add this transform automatically it also has a bunch of components here it's got something called a mesh filter and this is telling the graphics card what type of a mesh it should be rendering and in this case it's a cube if i click on this little button here you can see that i can select a few different meshes here and these are the default primitives that come with unity and later on when you make your game you'll probably import 3d objects from blender or something like that and if you do that then they will also show up here so the mesh filter tells the graphics card which mesh it should be rendering then we have something called the mesh render and that's actually responsible to telling the graphics card how to render this mesh it knows that it's a cube but we can also assign it stuff like a material and it's got a default material here by default how we should be interacting with the shadows should it be casting shadows should it be receiving shadows we've got stuff like probes and stuff like that we don't have to worry about that that's a bit of a future thing and we also have this thing called a box collider and a collider is telling the physics engine how it should be interacting or how it should be colliding with other objects in your scene and by default it also gets this box collider automatically so in this inspector here what we just covered now are different components that are attached to this game object if it looks a bit confusing don't worry too much about it now because as you start making your own games and prototypes with unity this sort of stuff will come to you through the process you'll repeat it over and over again and it'll start making its way into your brain so again don't worry about it too much come back to this video if you need it in the future as well and you can go through this back and forth but you'll learn this as you make your own games don't worry about it now let's rename this cube we should make something that resembles a little bit of a game so i can click on it here you can either rename it up here by typing it in here hello and that changed it here in the hierarchy as well and you can also click on it here and press f2 to rename it and we're going to make something that could be the start of a platforming game so let's call this one floor tile so that's the start of our platforming game let's press play and see if it's a fun game it automatically switches us to the game view that's what happens when you press play but it's not much of a fun game yet and technically it's not even a game because a game requires some form of user input and it should also be some challenges that you need to overcome and you should be rewarded for overcoming those challenges and we haven't done any of those so let's press the play button again to stop this okay since we call this cube a floor tile we should make it look a little bit more like a floor tile from a platform game so we're going to create our first material now to change the color and down here in the project let's make sure we have the assets folder selected here and in the empty space here let's right click and create a new folder we want to keep things tidy so let's name this one materials i usually create folders like this and i put materials in one folder so i know where to find them we can double click on this folder note and let's right click again now and create something else let's make it a material this time so we're going to name this one floor dirt because this is going to be the brown part of our tile we want to change the color of this we don't want gray dirt so on the right here when i've got this floor dirt material selected on the right here we still have the inspector and as you can see that can work here as well not only when you select stuff in your scene but also when you select stuff in your warehouse or in your project folder you can modify those so let's change the color here from white of this material so let's change it to a brown dirt color we can see a little preview down here at the bottom and dirt rarely shines unless you've polished it so let's drink the drink let's bring this smoothness slider down and you can see that the shininess disappeared it's still getting lit and it has some shadow but it's not shining anymore don't polish it third okay so now we need to move this floor dark material we'll click on it and drag it onto the cube here you can either do it onto the one up here or straight into the scene let's drop it on the cube here in the scene since we can see it and if i select the floor tile now you can actually see that it replaced this material here and the first element to floor dirt we dragged it onto there but you could technically drag it onto there or you could have dragged it onto there there's many ways to do it so pick your weapon of choice okay so a little bit more about the navigation so i mentioned before that in this scene view we can hold alt and then click the left mouse button to rotate around the view you can also hold the shift key down and press your middle mouse button to pan around in your scene and you can use the scroll wheel to zoom in and out and sometimes you get a little bit off centered like this and when you start rotating it's rotating way out of what you want to be watching and if you have if you select the floor tile here and then press f it will frame it into view that's very useful as well and now we can when we rotate around it it nicely orbits around our object so remember the f key will focus onto a game object same thing if i select the camera press f then it centers on that one so let's go back to our floor tile now f zoom out a little bit and here we go all right now we're going to make it look a little bit more again like a floor tile in a platforming game so we can actually select this game object that we already have selected and then i press ctrl d and that created a copy of it here and we can't really see anything different here in the scene view because it created a copy in the exact same location but let's rename this first of all we'll click on it here in the hierarchy press f2 and let's just name it grass and then we also need to make a grass material so let's click down here in the project view we click our floor dirt material press ctrl d and it's created a copy here so let's press f2 and name it grass and then we should change it as well make sure that is selected and then we click on the albedo color up here and make it green green light grass okay there we go and now it's it'll be tricky to move it onto here because we don't know that we've got two game objects now with the same type of the cube and in the same location so we don't necessarily know which object we're dragging onto here so here's a good example where you want to drag it onto your grass here instead and again it showed it probably would have hit the right object but we don't know that for sure but now we know that it was the grass tile so if i select the grass here we can see that it's got the grass material and here the floor tile is got the floor dirt but they're in the exact same location so let's make sure we've got the grass selected here and then slide it up a little bit on this axis and now you can see something called zed fighting zed fighting is when the graphic card is trying to render surfaces that are exactly in the same location on the one plane then it doesn't really know which one to pick so it just does its best job and here we can see that sometimes it's picking the dirt and sometimes it's picking the grass to render and this is a bit of a problem but we can work around that for now let's start by sending it up to make sure we've got it we can type here in the inspector 0.5 and now we know that we raised the grass up to half the distance here the the cubes in unity by default are one by one by one units and we've erased it now on the y axis which is up by 0.5 and now we can also change the scale here let's make the x scale 1.01 and you can see that the z fighting just disappeared here and we'll do the same on the z axis here 1.01 and now since that cube the grass cube is slightly slightly bigger it only has to be a little bit bigger then the graphics card has enough precision now to not have to let them fight but it's a bit thick the grass so we have to mow this a little bit let's change the y scale here to 0.1 and that compresses this cube down into a flat grass texture and now it starts to look a little bit like a floor tile in a platforming game i mentioned before that you should try to avoid changing the scale but for this purpose if you're going to create primitive game objects like this using the primitive cubes you don't really have any options you have to change the scale but you want to avoid changing the scale if you're designing your game objects or your meshes in blender for example try to model them in the scale that they will be using in the game engine because it will save you a lot of time and hassle later on so the floor tile and the grass they're two different game objects so if i move this floor tile here you can see that the grass stays there and we don't really want that to happen the same way if we move the grass here just the grass moves so we can actually change the hierarchy that's why again why it's called a hierarchy window here because i can pick this grass now click on it i hold it and i drag it onto the floor tile and that made the grass game object a child of the floor tile object i can still move the grass independently like this ctrl z let's move it back but if i click on the floor tile and move it then this grass is a child and then it will follow its parent around so that's very useful you'll often have big hierarchies in a game object for example if you've got a spaceship you'll have maybe wings you'll have weapons as children you'll have turrets flames thrusters you name it but again still both are game objects with its own set of components so keep that in mind also the in the inspector here now that we can see that to transform if we select this floor tile it's showing the position and this happens to be the position in the world because this is the root object it's placed at the very top of our hierarchy here so whatever value it says here in the position for this transform is the world position so when we centered it to zero zero zero here it's in the center of our world this is in relation to its parents so if i move this floor tile to the side here let's change the position here on the x-axis when i click the grass now guess what it'll still say zero and then we've shifted up on the y position but x that we changed for the parent is still zero and that's because the child is showing here in the inspector it's a relative position to the parent all right think of it like this i'm a parent i've got a bunch of kids and uh for example when they were younger i used to carry them in my arms so they were in a relative position to me they were zero zero zero centimeters for me in an x y z coordinate system but i could walk around in the house i could go up the stairs down i could change my position but my child's position relative to me was in the same location if i held my child out like this maybe one meter if i've got long enough arms from myself then the relative position from me is one meter in my z direction in my forward direction and then for example let's say there was a strange sound coming from this baby something really strange and then it started to stink then i can re-parent this child object to another parent and then the problem pretty much goes away it takes care of itself that way so think of it like that if you're having struggles now with the local coordinate system maybe that'll help okay now we're going to talk about prefabs and prefabs are prefabricated game objects and that's something you should really consider using pretty much every game will be using prefabs that you make and it'll save you a lot of time and hassle so try to make sure that you grasp the concept of this let's go back to the project folder here and let's make sure we go to the root here in the assets and let's right click here and create a new folder again and let's call this one prefabs remember let's keep it tidy now we can double click to get into this folder and if i drag this floor tile now i just click on it in the hierarchy and then drag it into this folder we've created our first prefab that's how simple it is and you can see up here it turned blue now the floor tile and the child and that's how unity will tell you that this is actually a prefab that you've got let's drag this prefab now that we've created from the project folder from our warehouse into the scene and we can instantiate or create this prefab or an instance of this prefab now in our scene and since the game is working in a three-dimensional space and our monitors only show two dimensions it could be a little bit difficult to place this it does help you a little bit because if you hit a collider it will actually try to align it to that collider as you can see so that can help you sometimes when you position it but let's just drop it anywhere in the scene now and it's automatically selected it for us and let's just manually change this position maybe we'll put it to x equals three zero zero and rotate okay let's make a smaller gap let's just put out position two here we've got two game objects now but they're based on the same prefab here you can actually duplicate the objects here straight in your scene as well if i have this one selected now i press ctrl d here in the scene view and if i hold the ctrl key and slide it it'll actually snap it nicely in the coordinate system here and we could snap move it to x location three here and now we've got three prefab instances here and unity will know that it was a prefab that you copied when we pressed ctrl d here so it's still using you can see that it's still blue here and all of these three game objects now and their children are using this prefab that we've created earlier we want to keep things tidy as i mentioned before and we can see that in the hierarchy now we're starting to mess things up a little bit it's starting to look a little bit messy so we can we can fix that let's right click here in the hierarchy and create a new empty game object we'll press f2 and let's rename it to level and we can also manually i like to keep all my objects that are placeholders or folder objects to 0 0 0 in the scene because then the children of that one will have the same coordinate that it will show in its transform will be the same as the world position if your level object here is a little bit offset it'll look a bit strange so keep that in mind try to keep these folder objects to zero zero zero in your scene and then i can click on these and then i can hold the ctrl key and multi-select these objects here and then we can drag them into the hierarchy of the level and the beautiful thing now is that we can collapse this so we can hide it and out of sight out of mind remember you can also hold the alt key when i expand on this little triangle and as you can see it automatically also expanded the children here as well so if you want to expand the whole hierarchy hold the alt key when you press on these little triangles and they'll collapse and decollapse them so what is the deal now with prefabs and why are they so useful if i select this game object we can see let's expand this here as well i've already messed it up again but we can collapse this again later on but if i select the floor tile we can see that it has a box collider here and if i look on the grass here we can see that it also has a box collider and when you're making a game you want to try to reduce the number of colliders if you can for two reasons really one of them is performance the fewer colliders that you have the better your game will perform especially on mobile devices for example and also it makes it a little bit less complex to work with fewer colliders so you'll know a little bit better what the physics engine is up to and what could be colliding with what so now we want to remove this grass collider and we want to make sure that happens for all the game objects so let's double click on this prefab now instead down here and you can see that the background here turned blue it looks a little bit different and also here we've got in the hierarchy we've got this title now saying floor tile and we're actually in prefab editing mode now and it'll only show you it won't show anything from the scene it'll just show you stuff that's related to this prefab so let's go to the grass object here and see this box collider we can right click on it and remove component and now we've taken away the collider from the grass part but the floor tile itself still has this bigger collider on it if we click this little left arrow or back arrow here now when we select these objects we can actually expand all of these and see that none of these have the box collider anymore for the grass so that's why the prefabs are so useful and you can make one sheet imagine if you had a thousand floor tiles in a level now and you didn't use prefabs you'd have that pain to do all this work repetitive over and over again so keep that in mind use prefabs and make the modifications to the prefab the beautiful thing is that you can also override stuff that prefabs are using so let's say this floor tile we wanted the grass to be a little bit taller on this one so let's go into this grass object and instead of going into the prefab now we're actually in this instance of this prefab so we've selected in this scene view here and let's make the grass a little bit thicker here so the y scale for this grass component let's change it to 0.4 and we can see that the grass got a lot thicker here but it didn't affect the other tiles and another thing that happened is that this text here turned bold and it's got a little blue mark here that's i think the blue mark is new in the new ui here but it went bold and we can also see that just the y position or the y component for the scale here turned bold and this is how we can see that it's actually overriding a value we can right click here now and do either revert if we want to go back to make sure that it was the same as all the other prefabs it's quite often that you want to do that you can also do right click and apply to prefab if you want to apply to all the instances but quite often controls that out of that one sometimes we just want to override a value of a prefab and you can do this pretty much anywhere you could change the material for for this one and you can see it turns bold and it's got this little blue thing here so that's why when when we know that we're overriding it if we pick the parent root here of the prefab as well we can see that there is something here in the inspector and it's got overrides and here is a list as well that could be useful that you can see which overrides are currently in effect you can revert them all or apply them all for example or you if you want to just check what you have changed or if overridden that's possible to do here and here's a little shortcut as well to select the prefab and it highlights it down here and you can also open the prefab if you want to go into there so there are a few shortcuts for you okay so let's go out of prefab editing mode and let's also revert this one we don't want the grass to be thicker there or greener for that matter on the other side and let's press play again see if there's a fun game no i'm pressing a lot of stuff here can't jump can't move can't do anything we just got a bunch of tiles so no fun game let's press stop again so now we're going to add a player to see if we can make this a game a little bit more fun first of all i remember out of sight out of mind so let's collapse the level for now and let's right click in the hierarchy and create a 3d objects and this time let's create a capsule and when you create games especially your first one don't start modeling lots of complex stuff in blender with walk animations and try to bring those in keep it simple like this i'd suggest so the tiles they're just boxes they're player it'll be a stupid capsule but live with that for now it's really good to keep it so simple just so you get the basics down and then you can gradually upgrade your game by replacing this capsule with an actual maybe a sliding player and then maybe a walking player in the end but we're gonna make a capsule for now so that's what i recommend to start with let's manually type in the transform here to zero zero zero to begin with to have it center in the scene and now it's in the same location as this tile so we can move it up a little bit in the air and that's okay we can have it drop fall down gravity will bring him down to the ground in just a minute but it's also a little bit big for being a player maybe compared to the tile so let's change the scale to 0.5 0.5 0.5 maybe we'll keep the y at an even two here as well also this light doesn't have to be there to annoy you because this is a directional light it doesn't really matter where it's placed you can send it in far away far far away because it'll always just have a the same angle of the light so you can just move that one out of the site let's go back to our player let's rename this one it's not going to be called capsule we're going to call it player let's do one more time let's press play and see if this is a fun game nope it still sucks okay so what can we do to improve this well we should be adding some physics now since it's a platformer we want the player to be falling down and hitting the tile and fall off the world if he misses a tile we're going to add a component now called a rigid body and a rigid body is something i usually get if i do something complicated like trying to pick up a sock from the floor i'll pull my back and get a really rigid body but in this case that's not what it is so we can add a component here so in the inspector with the capsule selected we'll click add component we can type in here rigid and we don't want the 2d one because we're making a 3d game so let's pick 3d or the one just called rigid body here by default it gets a mass of one kilogram but that's fine for now we can just keep it like that we also have a few other things like should we apply gravity and yes you should and should it have drag no it doesn't have to have drag at the moment and so that's air resistance and things like that just keep everything default and let's press play again and we can see now that the player or capsule just fell down and hit the ground so it's already unity's physics engine is already in the works for you now so it's still not a fun game because we can't do anything but at least we fell from the sky and hit the ground so we finally got some movement in our game isn't that amazing we still can't control it so it's not a game but at least something moved after all this talking so what this rigid body thing is doing now it's a it's got a mass of one kilogram it's applying gravity and it's pulling this capsule or player down towards the ground and this capsule collider is colliding with this box collider and when it's doing that it's preventing it from it pushing through this box collider we can actually switch back into the scene view now you can independently go through the game view and the scene view when you play you can even drag this one out actually if you want and look at them at the same time that's perfectly fine too let's keep it there for now so this capsule collider if i even you can even modify it here we're still in play mode as you can see up here and if i drag this capsule up it'll drop it down drag it up drop it down drag it up if i drag it to the side it'll fall over so that's pretty nifty in unity you can actually modify the game scene as you're playing the game if you want to test something you can reposition it and without having to stop and start the game just keep that in mind you can modify stuff in the scene view as the game is playing and you can also pause the game so let's restart it here and then i pause it here i can still move it to here and see okay i want to have this one oop if i can hit it there and then yeah i'm pretty happy with that let's unpause it and see what happens and that's uh useful to know as well possibly and also when you come out of play mode it'll reset it to the way everything was before you press play because you don't want the game to remember how it was when you stopped the game but like the player dies for example you want to have everything reset so unity also does that for you when you press play again or the stop button in that case it'll reset everything to the way the level starts where the scene starts just remember now before the days of unity and in the other game engines there wasn't such a thing as a built-in physics engine if you wanted something to fall down to the ground you first had to code the part that dropped that applied the physics to the game so it's become a lot easier nowadays luckily enough to make games and we can focus on our creativity instead of coding a physics engine but maybe that's something you're interested in so that could be fun as well obviously okay side note okay now we're going to be adding some user input so for a game to be fun we have to have some form of user input and that's going to involve some scripting are you still with me don't worry about it if you haven't created any scripts before or if that's been scaring you off all the time don't let it scare you off because a script can be very very simple and easy and you can learn more and more as you go along that's how i learned scripting i started to think just actually this is exactly what i did the first time i tried to make a game i wanted something to happen so i created two boxes and i wanted one box to hit the other one and then i just gradually increased my scripting skills that way so don't be afraid we'll get through this don't worry about it you'll repeat it you'll learn it and you'll get there so have i convinced you to continue now let's continue so we're going to do something at a very basic level now and i recommend using c-sharp that seems to be the most common scripting language that's used in unity and it's also very versatile and useful in other cases so i recommend that you learn that one a lot of stuff that you do google search it for will return results for c-sharp scripts for you there's also javascript that i used to start with that one but i've since long switched to c-sharp i find that one to be a lot more a lot more better a lot better okay so now cast your scripting fears away go to the root asset folder here and in the empty space let's right-click do create folder and let's call it scripts and double click on that one to go into it and now let's right click in the script folder here and create and this time we're going to do a c-sharp script and let's call this one player and press enter unity will keep the extension here as you can see but here in the view it'll just show you the the name of it so we've got our first script created now we can double click on this script here actually i should mention as well that if you go to edit preferences then you'll have external tools here and my external script editor is visual studio 2019 community and that's free to use for anyone but i think oh yeah maybe that's all that is supported nowadays it used to be mono but maybe it doesn't come with that anymore i recommend that you use visual studio anyway it's a great editor so when we double clicked on this script and it loaded in visual studio you're actually looking at the first script that unity creates by default and again don't be scared we'll go through this and you'll nail it in the end if you know what this default script is all about just skip forward but for those of you who've seen a script for the first time i'm actually going to go through this with you but i'll timestamp everything in the description so you can fast forward to other sections if you want to skip ahead so i'm going to start from the top here and the first thing that we see here is this keyword called using and it says using system collections and it says using collections generic and it says using n unity engine and this is something that's called declaring the space and again you don't really have to learn this in much detail but i'll tell you anyway because i want to leave no stone unturned in the first thing i do and a new script here but if we have this using unity engine here it's just telling the engine or it's telling unity what namespace to use just to demonstrate if i were to take this line away let's just delete it you can see that something happened here this one turned red squiggly it doesn't understand a monobehaviour okay what's that and that's because that was hiding in this unity engine namespace and what that is basically it's a collection of lots of stuff that you can do in your script and so it just knows to look without having to type it if i were to type in here unity engine period then it finds it again but you don't necessarily want to have to type this all the time unity engine period something unity engine period something unity engine period something so that's the whole reason for this uh using statement here using unity engine and as you learn scripting you'll find that you want to bring stuff more into this using stuff by adding maybe you want some ui stuff for example that would be needing to using the unity engine.ui for example so again don't be afraid of this it's just here to to make it the script a little bit simpler and just keep these as default for now the second thing i should mention here is the semicolon and all the lines in c sharp ends with a semicolon and that's how you differentiate from a new line and just as i say that i realize that not every line ends with a semicolon because when you have stuff down here like a method which is a function then it doesn't need disk but every command inside this function will need it so you'll have to get a little bit used to when to use it and when not to use it but most of the time you're going to have to put a semicolon at the end and that's quite a common mistake when you write code later on you'll find that you've missed it but luckily enough the the script will often tell you that oh okay here's your line of code and you'll usually find it fairly fast so with the semicolon you could theoretically do this you could actually move this line up to here and this one up to here and it will still work this script because the semicolon is was telling that it's something new is coming it's not the line feed itself but it's not very common to use it like this so let's just keep it like this you want to separate the lines it's very rare that you want to put multiple things on the same line just keep it tidy okay so the next line here we've got something called public class player colon money behavior and a class is basically a collection of variables and functions that the object object-oriented language of c-sharp can use to create stuff let's just keep it simple as that we don't have to demystify it more than that at the moment but remember when we created the script we named it player and unity automatically named the class player here and we don't have to worry about this too much more now let's just keep it in mind now that we can in this class inside this class we can have different types of variables and functions to store values and we can have some functions to do stuff like jumping or moving before we move on now we should actually add this script which is also a component to our player i minimized the visual studio here and back into our project folder i'll take this script now and i'll click my left mouse button drag it onto the player here and that automatically adds this one if we select the player now we can scroll down we see we have the transform the mesh filter mesh renderer we've got the capsule collider rigid body and har pow a new component and this is our player script and what's important here as well is that the name here of the the class needs to be the same as the c-sharp script name let's say you wanted to change this one and it was called hero instead or something ctrl s to save it now when i go back here we can see here that it's shouting a little bit we've got a little exclamation point the associated script cannot be loaded please fix any compiler errors and assign a valid script and keep in mind if you get some error like that it's because the name here of the class needs to be the same player and if i rename that one press ctrl s select the player again we can see that that error has disappeared if you did want to rename the script on the class click here on the class name press f2 which is the shortcut to rename and then type it in here instead you can see that it turned a little bit cyan colored or light blue we'll type hero there when we press f2 remember that f2 and now when we go back into unity you can see that it'll do some work and it actually re-imported it it renamed the c-sharp file now to hero and everything worked again the player now has the component called hero instead but let's go back we wanted to keep it named player so f2 again and press enter okay so we've got one more thing on this line that we didn't demystify yet and that's this colon mono behavior what the is that so a class can inherit something from a parent class a little bit like the game object the child grass game object that inherited the location of the parents for example and then applied its own little custom local location but in the same way the this uh this player class that we've just created is inheriting from something called monobehaviour and monobehaviour is a unity predefined it's a whole bunch of toolkits that is contained within this parent class it'll have so many things when you look in the unity documentation later on pretty much anything that you can do in the scripting language that's relating to unity specific things it's inherited through this mono behavior you could also have classes that don't have this inheritance but then you'll lose out on all of those nice features and as i mentioned before when you create a default script the money behavior here is by default so let's just keep it there and see what it can bring us and then we have this squiggly bracket and when you've defined a class like this like our player class it has to start and stop by these squiggly brackets open bracket and close bracket and now we get to something called a method here so we've got something that was also pre-configured here it says void start open close normal bracket and this is called a method and in other programming languages it's usually called a function but the c sharp name for this is a method and this line was also included kindly by unity and it says start is called before the first frame update and here is actually something that this money behavior now brought us since we're inheriting from money behavior monobehaviour contains a whole bunch of these type of methods that we can use to put scripts or script code in and as it says here this start method will be called the first thing basically when when you press play and we have our capsule now our player created here it'll run this automatically whatever is inside these squiggly marks here of this method it'll run that piece of code and it'll only do it once and then it'll move on and usually when you create a method it doesn't have this automatic run feature that's something that's quite specific for unit you know if you had created your own method here let's call jump for example like this then it this one doesn't get called automatically by any means it's just there so it's a bit tricky sometimes to know which ones are unity default or which can you have and that's when you have to revert to the unity documentation but i'll cover the most important ones in this video as well and then this void thing what's that all about and the word void means nothing and this is what the method or function will be returning and it's not if it says void here it doesn't return anything and by default here this start method that comes with the money behavior it doesn't return anything so that's why and it also has to have something and if it shouldn't return anything you have void if it should return something it needs to be stated what type of information it should return but we can get into that later on as well and the next thing we have here is another method that comes by default with this mono behavior and that's update an update is a method that's called by unity automatically every time the frame is rendered so once every frame it'll go through this for all of your scripts or all of your game objects that have different components it'll find this update and execute whatever piece of code is in this update method and it'll do it once during that frame and then it'll leave it alone and when it comes back next frame it'll do it again and again and again and again and again and again and again and again and again and again and it'll just keep going like that forever pretty much until you run out of power so the frame rate actually will depend a little bit on how your game is configured and um usually it runs about 60 frames per second but if you've disabled something called vsync if you have ever seen that in a game maybe that's disabled then it could theoretically run a thousand frames per second if it's a really simple game so the number of times this is called you can't rely on it being a specific value every time so you have to keep in mind now that when we put code here if you were to say that it should move one unit distance every update then on a really slow computer it'll move uh based on the frame rate on a really fast computer it'll just shoot off screen because it rendered a thousand frames and it moved one unit so that's something we'll keep in mind a little bit later on as well and finally again just this open and close bracket is just saying now for this method called start and for this one called update we have a beginning and an end so the code that we put within these squarely brackets here is what's going to be executed in this method so now we're finally going to write our first line of code and what we want to do is create something called an if statement and check if you as a human player is if you're pressing a jump button we want our little capsule player to jump that's our objective for now so what we're going to create is something called an if statement and that's very common in many programming languages also we want to put that here in the update because we don't just want to check if you jumped at the beginning of the game that wouldn't really work so we have to check for this jump button in every frame so in this update method that's where we need to put our code because we want at any time in the game the player should be able to press jump and the player should jump let's just put this in as an example so if i if in real word for example if you're hungry so if hungry equals true then you would do something you'd eat so now this co wouldn't work but i just wanted to put as an example an if statement is if something is something then do something so if hungry is true then eat so that's something you do in real life that wouldn't work here because we need to check for an input instead and do something in the game for this example to work we would need something called a variable and this if you want to check if something is true or fall the data type of that variable is a bowling or a bool a bool for this example to work just to make sure i don't leave any stones unturned like i said before we could have declared a variable here of the data type bool called hungry so if you put something that's not inside one of these methods here i put it at the very top here i declared a variable called hungry and it's a bool data type so it's not a number it's not a piece of text a bool can only be true or false and see how then i typed in here bool hungry which is declaring this one then the if statement suddenly the red lines here disappeared because now there is such a thing called there is a variable that's called hungary and we can check if that one is true and you might ask why is there two equal signs here instead of just one and that's in c sharp if you have one equal sign it's actually setting this variable so it would set hungry too true and we don't want to do that we want to check it and the syntax for doing that is having two equal signs so maybe it'll be confusing at first but you'll get used to that one so this one is still squiggly here it's called eat and that was a method that we wanted to call so i'd have to declare this one and here's another thing actually you could type it like this void eat and those squigglies will disappear now because there is a method now if if the variable hungry is set to true then it would call this eat function or e method and execute whatever code is in here but there's actually a nice way to to do this as well i should mention when this one's read squiggly like this in visual studio you can press ctrl period and then it'll give you some nifty options here it says generate method for player.eat so if i click on that one it already declared this one actually threw out some more code that we don't necessarily need private is to say if only this script should be able to access it or if another script should be able to call it that's why the difference between private and public is and by default if it's not typed there it is private by default and here as well when it auto-generates it it just creates a line of code to throw an error it creates an error that it's not being implemented yet but it's very common in visual studio here that you can use the control period we could have done the same here when we wanted to declare this variable we take this one away so we're back to here i click on this hungry here i press ctrl period and then it says generate field actually the proper name isn't variable it's called a field in c sharp but it if you hear variable or fields it's usually the same pretty much so we can generate the field player hungry and you see it declared it as private it was the bull it knew since we were checking if it's true it knew that it should be a bool data type and it knew that it was going to be called hungary so again we're not going to check if we're hungry we're not going to eat we're going to check if the player should jump so let's make something real should we so if i press space now i should be able to jump so space jump get it so let's do that in code now instead in this update method now since we needed to do check for this all the time we have to type an if statement that we just learned a little bit about and we have to open it with a bracket so now we have to type the code here what are we going to type to check this we're going to check for something called input so and it happens to be something here if i start typing now in you can see that it's actually suggesting here a bunch of things usually when i want to do something i google it and let's see i want to check for input i google uh unity c-sharp input or jump or something like that and then it'll give me a bunch of things and that will direct me to either some sample code or the unity documentation and it'll tell you a lot of stuff how to check for input and how to do it and as you learn this and i google all the time pretty much every day as i type code i have to google it all the time i have to go to the documentation back and forth but gradually you'll learn more and more and more so we're going to type input here and then we want to check for something and if i press period now you can see that we've completely typed here but if i press period it's got some more stuff here we can actually start reading what we can check for here any key any key like homer simpson would have pressed we can either scroll through here and see what there is there's a lot of stuff here like the whoo let's see get access what could that be maybe a joystick we've got getkey get a key press we have get joystick names key down mouse button up and down you see a visual studio will give you all these things just for free so we want to check what did we want to check we wanted to check for um a key let's make let's make a space the button that we need to press for jumping so get is there something called key yeah key and should it be on key down maybe when you hit space bar then we want to check that one and now it's out of suggestions here but it gives us something here as well so get key down then we have to provide something here called a key code so what could that be i press bracket again as it is expecting and it's already suggesting here key code okay so let's double click on that one and it was space so maybe we press period again here oh it's actually suggesting a bunch of key codes we can scroll up and down here with the up and down arrows so let's see or maybe we can type here's yeah we can space so just by knowing that we wanted to check for input and we wanted to check for some sort of a key command and we wanted to check space the combination of the starting to type what you need and also googling what you need will help you along the way to do to find out what you wanted to do and we all we wanted to do was check for a space key to be hit and this is what we ended up with and we wanted to see if this one is true so equals equals true so if get key down and that's actually the down action it's not if it's pressed all the time if i would have just typed input.getkey instead of getkeydown it would actually return true every time as long as i'm holding space down but if i'm only checking it on the key down action it'll only do it once so if i hit key even if i hold it down it'll just jump me once and after you have checked an input remember we needed to have the squiggly open and close bracket here the only reason why this input actually existed in the first place again is because we've got this using unity engine so if this one wasn't there press control s see it has no clue what we wanted to do so all of this is very unity specific and it's requiring this using unity engine then it knows ooh i'm going to look in the unity engine i'm going to see okay there's an input thing we can handle the keyboard input we can check for key dials joystick mouse you got it we could improve this code a little bit more as well because we wanted to check if this one is true and if you wanted to check input get key down this is we can see as well here it's a bool remember we learned what a bool was it was could either just be true or false but when you write an if statement you don't really need to write this equals equals true because it's expecting that if we want to check something it just expects me okay do i want to check if this is true or not so we can actually take equals equals true away and keep it the code a little bit smaller but in theory it still says the the compiler will still interpret this as saying equals equals true you can keep your code a little bit tidier if you just want to set if input get key down key code space then do this and what i mentioned before as well when we started to get all these freebies with what to type that's called intellisense let's see how far we can take this how much code do we actually have to write to make this thing if i let's take it to the extreme so if i typed i i wanted to create an f command okay so it's already see here intellisense is top choice here since it's so common is if i press tab okay and then i'm going to do an open bracket and we want it to check for input right so i press i n p and now we've reached as far as to say the top choice now is it knows that we probably want to type input so i press tab okay period and then we wanted to check for get key down so we press g oh see it's already at the top and the most common thing to check for input is actually get key down so i don't even have to type that one i can just press tab again and accept this top recommended thing here and then i do another open bracket and then see it's already checking okay we probably want to check a key code it's the top choice here or the highlighted choice so i press tab again and then we wanted to do space so i press period and s p and then it's suggesting space and then we do the squiggly marks so how much code did we really have to type there we had to type let's see i tab tab capital i period tab open bracket tab period space tab close bracket so keep in mind that intellisense is really useful it'll often give you hints on what to type or what it expects and things like that so don't just ignore that advice see what comes up when you start typing things see if that's what you're after press tab to complete it and use those uh hints to help your coding a little bit along the way the beautiful thing with this is that you don't have to memorize the code you just have to sort of know what you're after and in the beginning it'll be a little bit tricky but as you get more and more used to it you just have to sort of know what you want to do and start typing a little bit what you think it is and then hopefully this tab and intellisense will help you along the way and again combine it with googling how do i check for input in unity how do i make a player jump how do i make it not jump how do i do it every frame just think of stuff to google google is your friend hopefully but we don't want to check this twice so let's delete that one that was just an example to show you intellisense also these uh maybe i didn't really make this one clear before but these double slashes here is a way to document your own code so if i press enter here and do slash slash we can actually write something let's called check if space key is pressed down and these are basically just hints in the code or documentation in the code either for yourself or for your teammates if you're writing code for someone else to to be working in as well and i wouldn't suggest going overboard either you don't have to type comments for every line of code like yeah check if space key is pressed down because it's very apparent here that we're checking if space key is pressed down but know that you can write these type of comments and often you want to you want to use these comments when there's something that maybe it's not so intuitive like why are you doing something maybe you've done some something that looks a little bit awkward or why am i doing that in this update loop or why am i doing this that could be a time but often try to get the code itself to document itself and if you need to add stuff like here unity has added this update is called once per frame it tells us something okay so this is just not any method this one is actually called once per frame so that's a useful message this one is not so useful it's also very important where we put this line of code so this if statement now when we did check for if key down that's actually only valid one frame in your entire when you're playing the game here it runs through this update loop over and over again and the moment that you hit space the key down here it's actually only reported once on that frame and then it's not going to be reported again until maybe a release space and press it again that's the second time it'll hit this line of code so that's why it's important to know that you have to capture it in this update loop okay nothing is happening now if we press play let's press ctrl s if i were to start the game now nothing would really happen because we're only checking if the key down is pressed and it's actually hitting this line of code but nothing is happening and we can make sure that we write maybe something a little hint to see is this working we can do something called that i commonly do is it's called a debug log message so let's type debug press tab on on period log it'll suggest we just want to put a log entry here and then let's type here space key was pressed down and then remember we have to end it with a semicolon and strings have to be in these quotation marks here as well so this is again something that unity has and it's called a debug and a function here or a method called log and it'll send this string here now to our console and we haven't covered the console so let's minimize this we'll press ctrl s to save it minimize it and here is a console it's also here down at the bottom you have to toggle away from the project window here you could also drag this one down to the bottom if you wanted to have it visible at all times but now we press play we see that it enters here and we press let's see what happens we press space and nothing happened because we haven't told the game object to jump yet but we did get this line of code that we expected the debug log message that happened happens here in the console even if i was in this project folder it shows the latest line of the console down here at the bottom space key was pressed down so we know that unity is hidden every time i'm pressing this line out let's see and you actually have to make sure that your this game view should be active so if it's not updating maybe you have to click in the game window here to make sure that it's actually active and then when we hit space every time it'll log this message okay but we didn't want to log an error message every time we jumped or a log debug message we want to make the player actually jump so let's do that so let's make the player jump really so now we need to take this log message away you can either erase it with backspace or you can press ctrl y to delete the entire line let's think now logically what needs to happen when we press space we've checked for the input we press space and we had something called a rigid body remember that is applying physics to make the player drop down to the ground so there's something going to do with the with physics or rigid body so we have to actually access the component now the rigid body component and here we can do that you can access any component let's minimize this remember now that this player game object has this player component or the player script and it also has this rigid body component so somehow we need this script to be able to access this rigid body we need to get this component and there's actually something called just that so here we want to do get component and then inside these sharp angles greater or less or greater than signs we can type what type of component was it that we needed to get and it's the rigid body so i'll stop typing re and there it's got it so i press tab to complete it and then when you get a component like this this is actually a method so we need to have these empty brackets so and that's the syntax to get the component and now you can press period to see what can we do with this component so we've got the access we've got a reference to this component now and when i press period there's lots of stuff again lots of magic stuff we can do okay what is their move position we've got velocity that's probably how fast it's moving at the moment we can add force we can add explosion force cool stuff we can let's scroll down we can change the center of mass on it we can probably change the mass itself let's scroll down and see remember that was one kilogram in the inspector so we can actually set it here to something else if we wanted to through program but what do we want to do we want to add a force probably to make the player jump up so let's type add force okay so when we press space we want to get access to the rigidbody component and we want to add a force so so far so good open bracket here and then it suggests we need a vector3 a force vector three so a vector three what what is that and that's something that has got in the direction and a magnitude so it can point in some direction and it has how much how large or how what's the magnitude of that force so remember now the x-axis is left and right the y-axis is up and down and the z-axis is back and forth and that's how unity's coordinate system work and when you jump in this case which direction do you think you want to jump so okay we're probably wanting to jump upwards you could probably run and jump forward as well but we want to apply the force upwards as you jump and luckily enough there's actually a predefined vector already so we can type vector 3 dot up and that would take and by default this up has as you can see shorthand for writing vector three and then in brackets here zero comma and then the y component is one which is up one unit and then zero on the z axis okay we're going to apply the up force but then if we type comma again we see if there's something else we need to provide and there's something called force mode here and we have to pick the right force mode so if i press tab again okay what let's see if we press period it'll suggest what type of force mode we have and we have acceleration force impulse and velocity change and here for acceleration add a continuous acceleration to the rigid body ignores its mass force add a continuous force to the rigid body using its mass we've got impulse add an instant force impulse to the rigid body using its mass and velocity change add an instant velocity change to the rigid body ignoring its mass so now we can think okay which one is probably appropriate for us we don't probably want to have acceleration because we don't want to accelerate it up we just want to make it go up also force it's using the mass so it would basically jump different heights based on what the mass value was set so if it's one kilogram 100 kilograms and for our simple platformer game we don't want it to have a really pay attention to how much the player is weighing so we probably ignore that one impulse also uses the mass so we probably just want to do a velocity change here we'll instantly set the velocity to something and then we ignore the mass so let's pick that one let's try this so we have to end this line now with the semicolon remember i press ctrl s so let's see if this works now we press play and then we jump oh okay it's jumping but not very much okay so the force the magnitude of this vector up is not enough it's adding a velocity change up but the gravity is capturing us way too fast and sending us down again so let's go back and multiply this we can actually multiply it by five for example press ctrl s again to save it it's important to control s to save every time otherwise unity won't really know that something has happened press play again and press space and now we've got a jump that's a pretty decent jump but still can't steer anywhere so it's not fun game oh and here's another thing we can multi-press so air jumping probably not what you want to do it's like a rocket landing game now suddenly and it's tilting as well so is this a game yet no it's not even though we've got user input we can't really do anything about it there's no goal that we can't achieve anything there's no challenges for us to conquer so we have to work a little bit longer on this it's time to make the player move a little bit not just up and down but left and right let's make it move along our little platform level here we'll use the keys a and d to move left and right so those are the common aws d keys i'm supposed to be saying wasd but i say a wsd but it's the was the key setup but we'll be using physics to move this as well there's you could move it without physics you could translate it in scene some games will just have translating which means moving the object left and right but since we've already got some physics going here with the up and down movement we're going to stick to physics to move this little platform guy of ours so we have to say hello to another money behavior so we've got start and update from before and there's another one called fixed update and unless i told you so you wouldn't necessarily know about that but again the unity documentation will tell you a lot of the existing methods that are already pre-defined in the mounted behavior but for now let's do void remember because it's not going to return as anything fixed update and it's suggesting here so let's do tab and it's already defined this one and we could have left the private keyword out but it stuck it in there for us so let's just leave it for now to keep it extra obvious that this is only going to be executed within this little script of ours the fixed comment is a little bit different from update or it's very different even though it runs frequently like the update do let's put a little comment here to make it extra clear for us so fixed update is called once every physics updates so what does that even mean so by default unity runs with a physics engine that updates 100 times every second so at 100 hertz and what that means is that the physics engine will update everything in the world 100 times per second even if the let's say you're playing on your grandparents computer and the game's running like at five frames per second then that's uh the physics engine will still update as if it was running at 100 seconds so anything that's falling like with gravity for example will still have the appropriate speed even though it's like a slideshow because their computer is so slow then at least you know that the physics is reliable it'll still update the way it should so the difference here would have been like again on your grandparents machine it's running update once every five seconds because it's a like a graphics card from 1984 but then the physics update will actually run so it keeps in sync with the proper physics so i hope that makes sense so by default 100 times per second and again you can customize this in some games you might want to increase the frequency and you can change that in preferences but we're just going to stick to the default of 100 times per second though and here's something very important now remember when i told you about this key press here the get key code down then it's checking for space and that's only true once every frame but that's actually every rendered frame so we could actually miss it if we stick it here in the fixed update and that's a common mistake to do and very confusing probably so remember anything that is registering a key press or a key stroke or a mouse click you need to have that one in update because you risk missing it in the fixed update so keep that in mind and at the same time saying that we're actually applying physics now in the update loop and that's not really good practice either so we have to change that one as well it works and because we're adding a force when only once and we're adding a velocity change then it was sort of okay but it's not good practices so we have to fix that i can't start your first script by breaking a principle so we have to change this so there's two things we need to consider here the key press needs to be registered here in the update loop but the force should really be applied here in the fixed update so before we start with the left and right movement here let's just fix what we did here so we have to set a little memory here now so when we hit the keystroke of space then the computer code here needs to remember that space was pressed and then you need to do the change here down here in fixed update i remember i told you about that hunger variable hungry variable well we're going to apply something similar let's go to the top of this class now and we're going to declare our first proper field or variable the proper name is field but you'll hear variable a lot and it's going to be a bool type we only need to check if it's true or false and up here we type private and we could have really left that one out but let's make it ultra clear now that this is a private variable only this script will know about this one and it's going to be a bool type so after the private comes the data type and if it would have been a number it would have said in there if it was a string we wanted to keep track of it would have been saying string there but we want to just check if something is true or false so we type bool there that's the data type and then we're going to create our field name or our variable name and let's do jump key was pressed it's also very good practice to name your variable something very descriptive so you don't have to document it theoretically i could have named this variable a or something like that something that's not used by the scripting language but then would be really difficult to know when you're looking at the code what what's a what's that do so it doesn't matter if the variable names are a little bit longer just as long as they're descriptive they don't take any more space they don't make your game bigger they don't make the game run any slower it just makes you be able to read them better so let's keep this jump key was pressed and down here now we need to move this code so i can actually we're going to have to do if statement in both here so i'll mark this with a mouse i'll just hold the left mouse button mark that press ctrl c to copy it and then i'll paste it down here in the fixed update ctrl v and we don't really want to do this same check twice but now we can replace the code here so in update we do want to check if the key was pressed down but we're going to replace this code so let's take that mark that one press delete and instead of applying the force there we'll just do jump and remember intellisense already knows we're going to type this probably so we'll press tab jump key was pressed equals true and now we only use one equal sign because we're going to set this variable or field to true so now we've stored that variable by capturing it here in the update but then we need to apply the force down here so instead of checking for the key down here let's replace this part of the code and here we're going to type if jump key was pressed equals equals remember true because if you want to check if it's equal we don't want to set it to equal that's why you have to have two here and also remember that we can take this away we don't actually need the equals equals true the if statement will know that it's going to check if it's true or false since it's a bool type and now we apply the force down here which is the correct way to do it you check for key presses and input in the update loop and then you apply forces in the fixed update okay let's try to press play now and see what happens what do you think will happen did you spot anything so if i press space now oh it became rocket man so why is that well let's look at the script again it's setting it to true but it's never setting it to false again once it's applied the force here this variable is kept to true so every time we hit the fixed update loop here it'll apply that force because jump key was pressed is still true so when we've checked for this we need to reset it back to false so i'll press enter here and create a new line jump key was pressed we're going to need so i'll press tab there and let the intellisense do the job equals false so we set that one to false now so this should make this only run once now until we press the key again so let's minimize this one press play and press space and now it only jumps once so that's nice that's what we wanted so we've changed and added code now but the game is still behaving exactly like before so what's up with that well you've successfully just done your first refactoring of code and this is something you will be doing a lot when you make games you won't write perfect code straight away first of all and you'll never write perfect code and you shouldn't really even try to write perfect code because if you do that you'll fail to make a game so what you will be doing is like iterate over your code over and over again improve it over time change it update it make it run a little bit better like make it a little bit more structured but not more than necessary and what we did now wasn't really necessary to keep it tidy we needed to check for the input in the update and we needed to apply physics in the fixed update so it was pretty necessary what we needed to do here don't spend too much refactoring either you'd basically get stuck in an infinite loop trying to improve your code over and over again and uh i once heard a quote saying someone said that you don't have to write pretty code to make a fun game and when i googled that to see who actually wrote that quote it was only one google hit and that was a post that i wrote about three years ago or something but i'm very sure i didn't come up with a quote but keep that in mind you don't have to write pretty code to make a fun game try to structure it quite nicely and try to make it by the book but don't overdo it it's perfectly fine if it's not the best practice if it runs if it works that's great and improve it over time if you need to so now we're finally going to add that horizontal movement and we have to declare another variable here so at the top under the jump key was pressed we'll do another one private float it's going to be called this time i'll tell you why in just a second horizontal input if you're wondering why i'm starting my variable names here with a lowercase and then i have uppercase for every subsequent word it's called camelcase and it's quite common way to write your variables or field names usually methods will have a capital letter all the way through and capital like fixed update here for example and private variable commonly have has this you'll also find that they can start perfectly fine with the capital and it's really down to personal preference but this is one type of naming convention that i tend to use for public properties for example i would have started with a capital case then i know it's a public one and for these private ones i start with a lowercase so this horizontal input now we've just declared it but what is a float well a float is a number it's a floating point number it's called and that's a data type that can store a value like 3.14 for example for pi it could also store 3.0 but it doesn't the strange thing about float numbers is that it doesn't necessarily mean that it's exactly 3.000 forever and that's a little bit strange with the floats it'll do its best to represent a decimal number but with its limitation and usually for for the purpose of what we're making it's perfectly fine it's when you run into like if you wanted to check here if three point uh and then a whole bunch of decimal numbers equals and then three point a whole bunch of decimal numbers you might run into issues there that you should be aware of but for our purpose this is going to be perfectly fine so a float is a decimal number and that's because we're not going to check for a key down this time we're going to check for the input of an axis and it could be a joystick axis it could be the mouse movement for example but it's also actually registering the axis for the a and d key that we have for movement and for the arrow keys so here in our update method let's press enter a couple of times and let's do and this time we're not going to do an if statement we're going to write horizontal horizontal input tab there equals and then we have to revisit this we still want to check for an input like we did before so we have inputs we press tab to complete it period and this time we didn't want to check a key we wanted to check an axis so get we it's already suggested there get axis and the reason why it knew that we probably wanted to get access this time is because we're trying to feed this value back into this horizontal input which is a float so it knows oh you probably want an axis because that will return a float so that's how it knew that's what we probably wanted and then i have to write here in quotation mark this is actually a string and it's called horizontal again and it says here string axis name and again how come i knew that it was going to say horizontal here and it's because i've made unity games for quite some time but there is actually a logic to this so it's not just any word taking out of nowhere i can go here into edit project settings and let's go to input manager and then we have access here and then we have horizontal these are the different axises and buttons you also have instead of doing a key code like we did for space there's for example the the jump button here which is space funny enough so we could have checked for a get button instead of a get key down here's the input manager and there's an axis here called horizontal and then we can see we've got a negative and a positive button and it's called a and d and those are the keys that we wanted to check for the standard inputs for for example a first person shooter movement we were actually reading the alternative buttons because the default negative and positive buttons are the left and right keys so you'll be able to press either left or right on your keyboard or a and d to achieve the same input you also have some settings here so gravity and dead and sensitivity and that's when you press a d key since that's actually a digital key it'll either be down or up but these settings here can make it gradually increase it like you were twisting a joystick gradually or slowly or fast so these values you can change around a little bit if it's not direct enough or if it's too fast you can change these values later on so that's how i knew that there was an axis named horizontal and you can revisit that input manager if you want to see what the accesses are called again and you can even define your own ones there as well if you wanted your role in a pitch axis for example for a flight game so let's recap a little bit what we've done we knew that a bool is a true or a false we knew that a float variable type can contain a decimal number which is this horizontal input and we knew that a string here can contain characters this is a string class so now we need to use this information so we've collected here horizontal input it takes this in all the time and we don't have to do an if statement like i said because if we're not touching these keys a d or left and right it'll just feed in zero here all the time so the horizontal input will be zero every frame but as soon as we hit a key it'll start increasing that value or decreasing it either negative for left or positive or right so we need to make something happen now we scroll down to the fixed update because we wanted to apply the physics remember we have to do that in fixed update so again here we'll do get component it's again the rigid body that we need to access and now we're going to change the velocity we don't apply a force let's just change the velocity directly so left or right so i press tab to auto complete that velocity and now this velocity is specified in a vector three and remember that's something with three axis x y z or x y z and a magnitude so the size of it so in c sharp this might be a little bit confusing but you actually have to define a new vector3 so we'll type the new keyword here vector3 and then we have to supply what information here in the brackets what should the accesses be and here we type in this float valuable that we collected up here so horizontal input and that's the x movement here so left and right of our character and then comma we don't want to alter in this case we don't want to make any changes to y which is up and down so we'll type 0 there and then comma again and we don't want to do anything in the depth forward or backwards into our scene so let's put zero here as well and this might be a little bit confusing because what we're taking this value now from our left and right key movement and we wanted that to be the x component of this velocity and the velocity of a rigid body is the movement speed let's press play and see what happens okay so something's strange now what's happening with the gravity see it used to fall down quite fast and it tumbles over now when i moved it so it's something really strange we've done something wrong here and the reason why that is happening is because remember here we're adding force but here we're setting the velocity to up and down to zero we're forcing it back to zero so we're applying force and then directly after we're saying well the speed up and down is going to be zero and that's why it's fighting with itself now it's applying a force but it's resetting the speed to zero so we have to fix that so instead of setting it to zero here we can actually leave the velocity of the y component or the up and down movement to the same value that it was and we can do that again it's actually stored in this velocity already so theoretically we could copy this ctrl c and put it for the y component here velocity dot y you can actually access the individual x y and z components here so this would say that okay we want the velocity to be a vector 3 and the vector 3 should be the horizontal input but we want to remain the velocity of the y component should be the same so we don't want to affect that so let's try to press play again here so now when we press space it works uh it's not slowing down it's jumping up and down and when i move it's actually not sliding or moving it's tumbling over and that's because we've got friction at the bottom of this collider where this capsule collider is hitting the floor tile we've got friction here so that's why it's leaning over but theoretically we've got something that could nearly be a game now we've got a challenge here i have to clear that gap and i could so we're on our route to making a game now maybe not a very fun one but it could be called maybe like flappy salmon or something could be a thing i don't know flappy pill pill flapper flappy pill fill the fill fill the pill flapper flappy flappy filled pill flappy pill felt now i don't know also i noticed this as well that when we press play now you can actually jump in the air so we've theoretically actually accidentally caused a flappy bird clone so by pressing the jump button multiple times we've created our first clone of the game as well so there you go just launch it in the app store now and make millions all right so now we're going to start fixing our first bugs maybe that's not the the most fun sounding thing but it could be quite amusing sometimes when i'm making games some of the bugs i run into are hilarious you'll have so many unexpected things happening and it'll make you laugh over and over again take it with a grain of salt try to enjoy the bugs and fight your way through them to eliminate them so in our code here now there's one thing i was reacting on and that was that we type the same thing here over and over again we have get component rigid body here we've got get component rigid body here and we've got it here again and i also happen to know that it's not very good to do this all the time because every time you get a component unity will have to look at the game object see what components do i have and it has to grab a reference to that so for two reasons a the code is a little bit messy because we have repeating it but more importantly we're actually slowing it down a little bit it wouldn't really be a problem for our little flappy flappy pill clone or whatever we're going to call it but i want to show you how to improve this a little bit so up here where we have our fields or variables let's declare a new one now private and it's going to be the type rigidbody let's call that one for clarity rigidbody component so we've declared a new variable or field up here and then now we're actually going to use this start method remember that this one only runs once and the component the rigidbody component will never change so here we can type rigidbody component tab to autocomplete it equals component rigidbody and now it takes this reference to the rigidbody and it stores it in this rigidbody component variable that we've got and now we can copy this here and replace this one down here so we don't have to type that one over and over again so we're improving performance and making a little bit easier on the eye and again the code the game will actually do exactly the same thing now but we've improved the performance and done it a little bit more by the book and i just said you don't have to do everything by the book but you'll find a balance in the end the actual bug that we had though was that the player was tumbling over here so remember we press play it's tumbling over how do we fix that well there's a few things that we could do for example first of all remember out of sight out of mind so let's collapse the level we don't want to see that for our player here if we look at the rigid body there's something called constraints here that i can expand so when we've got that expanded we've got something called freeze position and freeze rotation so we want to freeze the rotation remember it's tumbling over and we don't really want this player to ever rotate so we can actually freeze all the rotations let's try that and press play and now it's sliding pretty good and we can jump maybe still yeah and we could even clear that gap go back come back come back sometimes you'll actually change these things you could change these on the fly while the game is playing and remember it's tumbling over now and if i press stop now you'll see that these will reset back and that's probably a common mistake that you'll do sometimes as well you'll change something in play mode i do this again all the time unfortunately when you realize that you were in play mode you press stop and everything that you changed reverts back so that could be a little bit of a pain but you you'll get used to it also the position we're moving left and right and up and down but theoretically our game object could be going this way and especially if you have a lot of collisions going then it could actually move in the way that we didn't want so we can even freeze this remember the blue axis here is the z axis so we can freeze the position on the x z axis and if i press play now it'll look the same but we've just protected ourselves from the object being able to fall in or out of the scene so you could actually apply this freeze position as well okay we had another bug as well remember or a feature and that was that we could jump in the air and we're not going to make the flappy board clone out that's already been done and many clones with it so we need to protect ourselves from from the player being able to cheat and use a glitch jump in the air there's multiple ways you can do to fix that but we'll just pick one method and again let's define another variable up here private and it's going to be a bool and let's call that one is grounded we need to check if we're grounded or not and then there's another money behavior that we can make use again as i said before there's multiple ways you can do this and i tend to vary between them but this is one method that we can use so we have a method here so let's start by typing void and then this one is called on collision enter and if you press tab it'll auto complete it with a bunch of the stuff and this is actually giving us something that we haven't seen before and this is typing collision and then this is the data type and a variable name collision and that means that when we have a collision we can actually get some free information here this collision will contain for example information of the coordinate where the collision was what velocity it was for example if i type here collision period we can see here we've got relative velocity to the two bodies that collided we've got the other rigid body that we collided with we've got the contact points so where are the coordinates that we hit um we're actually not going to need that information for r so theoretically you could actually delete this one if you don't need it it'll still uh do what we need to do with it but we'll just leave it in for now since that's what intellisense automatically gave us when we typed it so when we detect a collision here let's just set that s grounded variable that we created to true and equally there is another method called on collision exit so let's do that one void on collision exit and then we'll set is grounded to false basically now when a collision occurs it'll set is grounded to true and when you exit the collision when it's no longer happening again so when you've jumped for example there's no collision at that time so it'll set is grounded to false but we need to do something with the information that we're collecting down here so in this fixed update we'll type if and then we're going to put an exclamation mark here and they say is grounded and the exclamation mark here or exclamation point that means if not so that's actually taken if it would have just said that it's like if is grounded then we do something but if there's an exclamation point there then it's means is not you could also type this like this if it's not true or you could have typed if it's grounded is false so there's multiple ways you can do that but i commonly keep the code as compressed as i can so exclamation is grounded means if not is grounded then we want to execute something here and here i'm just going to type return so remember now the physics engine and money behavior will run this fixed update every time it hits the loop but then it'll detect now okay if it's not grounded then i'm going to return so i'm going to not execute this code that's down here so return you can you can actually exit a method earlier by doing this so okay if it is grounded it's going to continue here and it's going to do our key press here for the jump it's going to do our movement velocity so if we press play now and now i'm trying to jump in the air and it's not working and that's how we wanted it we don't want to do air jumps in this one you could to do if you want to have a double jump feature you could check here a counter instead for example to see if it's been pressed once or twice but let's just keep it simple for now so you can only jump when you're on the ground and now i've ran into another bug here see that i'm actually trying to move this object but nothing is happening so what could that be and i think what's happened here is that we're actually hitting between these two blocks so if we try again let's see if we can replicate that problem i'll move i'll jump i'll go here and there it stops and i think we're hitting right between two colliders now so it's exiting and it doesn't really know what to do now it's right in between these two colliders so maybe that's not the best best method to check for this collision okay so how do we fix that problem again as i mentioned before it's quite common when you make a game that you'll find that you'll try one approach and it might not work or you'll find a bug then you have to find a different method to do it so maybe this on collision enter and exit wasn't the best approach but hey we learned that those existed so they it wasn't exactly for nothing it was pretty good to know that anyway but let's uh let's scrap this method and let's find something else let's even take away this is grounded and go back to where we were and let's think of a new way to do that we can actually check there's another physics feature called overlap sphere and that will check if a sphere is overlapping with something like a fictional sphere and if we imagine that we want to check at the feet of the player if it's touching ground maybe that's a better approach to do it so first what we're going to do is we're going to put another reference up here but this one we're not going to make it private we're going to do public and this is going to be a transform so why is that okay so round check transform we're going to call it and you'll find something is different now when we do public here and we minimize this then when i click on the player here we can see that something happened we've got it's exposed in the inspector so ground check transform it actually inserts some spaces here for human readability you can also change the width of this if you want so it's exposed that it's called here and this is the easiest way to expose something you could also do something which is the i think the proper way to do it so private you could do but there's a little tag you can put here as well serialize field and that will uh actually also expose it here and this is actually the proper way to do it so we'll leave it like this for now if you get a warning i should also mention then you can actually set that the default value should be null so again you could either just keep it at public to keep it simple or you could do this method which is the proper way to expose stuff in the inspector the result is going to be the same it's just that you're not exposing it to other classes as well it's just to the inspector that you do it okay so this now we've got a ground check transform but what should we do with this one it says none here no transform and remember what a transform was that's the position rotation and scale of something of a game object so i right click on the player here and let's create an empty game object now and since i right clicked on the player it'll create that as a child here and it's called game object but let's press f2 on it and rename it to ground check transform and it's positioned in the same place now in the relative position of zero zero zero of the player object the parent object and we want to move that to the feet because we want this one to be positioned we're going to use this as a little helper object to know where our feet location is and to position it properly remember we could hold the alt key and left mouse button to pan around here but we want to position it pretty much exactly at the bottom so you can see this this little uh access tool up here first of all you can click on an axis like this and it'll view it from the side but we still don't really know exactly where it is because it's perspective enabled here so even if we pan around where is the bottom exactly you can then also if i rotate see that it says perspective here if i click on that one it'll switch to isometric or orthographic view and that removes perspective so if we click on this z axis again you can see now it's basically looking like a 2d game without the perspective and now it's really easy for us to reposition this so we can check this make sure we've got this ground check transform and then we slide it down on this axis and position it at the feet and now it happened to be that this is two units high and the center is here so the proper position here is probably minus one to get it exactly on the feet and don't forget to switch it back as well to perspective if it looks all weird on you and then you can just hold the alt key again and rotate and now this child object now is right at the feet of this capsule so on the player we're actually it still says none here and that's now it's because we need to assign this one we need to connect this reference so i take this game object from the hierarchy and i drag it all the way down here to this exposed field and then it's got this link now if i click on this one it'll highlight it here so i know that this is now connected this is also something you'll be doing a lot when you create your game objects you'll expose things here maybe you want a camera follow script and you want to drag the camera reference to it you could expose for example the jump height or the speed that a character can move you can expose all sorts of things here in the inspector and instead of having to go back into the code to modify it you can do it out here but now we've created this little reference so let's get back to what we were doing and so we've got this transform exposed now so we can use this now and down here in the fixed update let's do a check here if and then we can access the physics engine directly here physics and then we've got this overlap sphere function and again i know this because i've created a lot of small simple prototype games and i've found that there's so many ways you can do things you can shoot raycasts to check the distance to the ground you can do in this case overlap sphere or check collisions there's so many things and this is just one more method that you've got at your disposal so this is expecting something and we can see here in the little hint here that it's expecting a position and remember now that the transform that we exposed up there the ground position then that actually that was a transform so we know that that has a position so i can check here ground check transform and then this one has remember it's got position rotation and scale but we only need the position here so position and that's going to be the world position comma and then we have a radius here so how big should this check be and we remember that the character's height was 2 units the capsule so maybe we only need to check a little small area down there so maybe we'll keep it at 0.1 here's another little strange thing when you type a decimal number in c sharp you actually need to add f so those squiggly marks there that was there before it's because it needed to to have f to indicate that it's a float number that we want and then it's still complaining for some reason let's complete the code so why is this not working we have a ground check transform position so everything looks right but if we hover over these red squiggly marks we can see okay it says cannot implicitly convert type collider brackets to bool we're trying to check if this is true and it's not actually returning if it's true or not for us it's returning something called an array of the colliders that it's collided with so let's say that this overlap sphere was really big checking for an entire world maybe it like encompasses four tiles then it's actually returning references to all those tiles and we're only interested to see if it's colliding with anything at all or overlapping anything at all so we can access the length now of that array and the length it will say how many components are we hitting at the moment and remember we only want to run this code now if we're connected to the ground or if we're touching the ground so it means that it has to be at least one collider that it's overlapping with so let's check if this length the number of colliders is zero and if it is zero it's not colliding with anything then we know that we're probably going to be in the air so now we do the return here so we'll check is this little fictional sphere of ours that's only 0.1 units in radius if that one is not overlapping with anything we're going to assume that we're in the air and we're going to exit this fixed update so let's try to press play again that's still not working so why could that be well it's because the capsule collider it's got a collider itself so it's actually colliding with itself and there's multiple ways to fix that you could use something called layers here for example you could create a new layer and call that one player and we could have this little overlap sphere ignore that layer that's one way to do it so since it's always colliding with its own collider we could go for the method of saying that okay if the collision is only one for this little sphere then return and what this is saying now is that i know to expect that i'm always going to colliding with myself so i'm going to expect if i'm in the air it's only going to be colliding with me if i'm near the ground it's going to be two collisions because i'm going to overlap my capsule collider and the ground collider so let's press play now and have a look and now it works to jump because it's colliding with two colliders at the moment this little fictional sphere now that we've got is colliding with the capsule collider here in the scene view we can see it's colliding with this green collider here and it's also colliding with this green collider here so the collision when we're touching the ground is two the little fictional because remember now that at this ground check position now imagine that there's a little fictional sphere about that size now that is checking how many overlaps am i doing and when we're on the ground it's two overlaps and when i mean the air it's only overlapping this one maybe the best way to do it would be to separate the player into its own player layer here so for the sake of completeness and just to make this video a little bit longer let's try that method as well so you can learn that one layer filting is something very useful that you probably want to be using sometime in the future so let's create a new layer here we'll click the player here use the drop down click add layer and here let's just call it player there are a few built-in layers here that you can't change so let's just pick the first available one which is number eight and when you define a layer this is also something i mistake myself on quite often it doesn't mean that we actually gave this player that layer now because when i go back to the player we only define the name for it so we have to go back here and then in the inspector we'll do the drop down and then change it out to number eight and now it's going to ask us if we want to do the children as well and yes we want to change the children and that basically sets this one to the player as well so to this layer so now we've separated this capsule collider now we'll be colliding on this layer but but if we go to edit and go to the project settings we can see here in the physics we scroll down a little bit this player layer has appeared here now and you've got this weird collision matrix and here you can actually tick in and out which colliders should be colliding with which ones but i'll leave this one here for now but know that it exists at least that you can define which layers should be colliding with which here but for our case here now let's go back into our little script and then we want to change this we don't want to be checking the length now how many colliders we hit we want to be using this layer mask name now and to make it as simple as possible you could actually type in loads of weird code here to generate your own mask but to keep it simple let's just call it the player mask and this one's not defined so it doesn't know what to do with this word but remember we can press ctrl period here and then generate field player player mask and then when we scroll up here we can look that it's an integer here but let's change this from a number to a layer mask because that's actually a thing called a layer mask here and again we want the length here to be zero because now it shouldn't really be colliding with anything soon but we also have to define this player mask and this one is a private now and we want to expose this one into our inspector so let's move it to the top here it doesn't really matter where it is but i like to keep them grouped together we'll copy this serialize field here now we'll tab back here into and then see we've got uh this one is exposed now called player mask so let's drop that one down and let's try to click here player and let's see if this works press play space we can still jump in the air and then it probably means that we did we thought about it the wrong way we're actually checking if it's colliding with player and we wanted to check if it's colliding with everything except the player so let's do everything first and this player mask and then let's untick player and then press play so now we're actually doing it the proper way here as well where we've successfully implemented a layer mask instead of expecting that it's going to be collided the reason why i didn't really want to assume that it was going to collide at all times with one collider at least it's because imagine if this player also carried a weapon or something or maybe i had colliders for its shoes or some armor then it's difficult to know how many colliders should it always be colliding with for it to be assumed to be in the air and now we could revert our code here back to zero and it's because we're actually ignoring all the overlap collisions that we did for the player layer if you don't understand this fully it's perfectly fine don't worry about it i just happen to show three different methods to solve the same type of problem now or how to address the same problem and again there's multiple more as i could have shown but let's just leave it like this for now it's long enough as it is probably but as we did this i realized that when i'm playing now it's quite nice to have the control in the air because now when you steer it's a little bit strange i press play again i think it's quite common in platform is that you should be able to steer in the air okay we shouldn't be able to double jump all the time or triple or quad jump but at least when we come here we want to be able to change it here so because what if i want to steer a little bit in the air because if i jump here and press side nothing happens and if i press it's just a little bit awkward so let's leave the movement for the side side to side movement let's make that possible still so in order to do that we can just change the order here let's control x this line and then move it to the top here so it's always going to run the side movement script here the horizontal input script part so now make sure ctrl s to play to save it and then press play again and now we should have yeah now we have the steering ability in the air again it's a little bit more platformy this way because there and change my mind here in the gap here's our next bug so bug number three we have to fix i'm keeping depressed now depressed i'm not depressed but i'm keeping depressed moving right so the capsule is actually sticking to the wall now even if i let go of the key yeah it tends to stick a little bit so we can fix that by there's something called physics materials as well and if you can see here in the collider it's got material physics material and it's got nothing here so let's go back here into the project into our assets folder and let's create something new here a new folder called fizz materials and they're very different from the other materials that we created those were just to render objects this is going to tell the physics engine how much friction and bouncing is something else so here let's right click create let's create a new physic material and let's call this one no friction and here we can see dynamic and static friction let's just set those to zero and then friction combine because it'll take my value of zero and then if i'm colliding with a tile that has 0.6 it'll take the average of that which is 0.3 but we want to change this to minimum because we always wanted to pick the lowest which is going to be 0 in this case and then we go back to the player here scroll down to our rigid body node to our capsule collider and now we can drag this no friction physics material onto this one and that should hopefully address the stickiness problem that we had yeah so now even if i'm moving towards the wall the friction is going to pick the lowest and its own physical material has zero infection so it's just going to slide you right off there now maybe we want the camera to follow the player so how do we do that we've just been working with a static camera let's add some more floor tiles first i can collapse these a little bit and let's just create a little bit of a level here so i'll select this object here the floor tile press ctrl d to duplicate it hold the ctrl key to snap it nicely into segments here move it up maybe scroll it a bit light ctrl d create another one ctrl d maybe lower that one down again and now we want the camera to follow the player let's press play again here and see so here see it's a little bit boring it wouldn't be so much fun if you just ran off screen and you can't even clear that jump so maybe we should also increase the jump height a little bit let's do that straight away let's increase the jump height let's put 10 here so we can make sure that we clear okay that was probably too much so even if we just double the number actually it has more of an impact when it's in the world of physics so let's put maybe seven let's try that again making a game is a lot of trial and error so that's pretty good i think let's see if we can make sure that we can make that gun jump yeah that's good okay but again what we wanted to do was make the camera move along because you'll quickly run out of screen space here so let's go back here you could write a script for that but i'm going to make it even simpler than that let's just take this camera now remember this whole child parent relationship thing let's just drag the camera and make it a child of our player and everything looks the same but when we press play now the camera is actually going to follow along here with the player because we can see here that the local position of the camera is the same all the time as it was before because the camera in relation to the player is not changing but it's following this player object and that one is moving so many times when you're making a platformer for example you could get away with just parenting the camera onto the player object but then saying that sometimes you want to do this entirely by script instead so there's multiple ways to do it but we'll stick with this one for now if you wanted to offset the camera a little bit higher up you could press stop first of all go to the camera put y to zero press play again and now it's a bit more centered there let's continue with this level let's just ctrl d let's make a little overpass here as well so ctrl d ctrl d ctrl d ctrl d ctrl d so you can make your level this way and then start creating some new prefabs for new floor tiles maybe let's see if this is completable let's press play again let's see we can get under here yeah that's good oh that was impossible to make so you can't have a level where you can't clear the gap so maybe it would have been good to give a little bit of a ledge there so you can actually come clear and jump it there let's collect some coins as well but in our case it's not going to look like coins it's going to look more like white pac-man pills but the mechanic will be pretty much the same as coins let's right click here in our hierarchy and create a new 3d object and this line in this slime and this time let's just create a little sphere and that threw it into this position for whatever reason let's reset that one back to zero zero zero and then hold the ctrl key and slide it up to maybe there and that's one big pac-man pill so we don't really wanna have that size so let's resize it 0.2 it's good yeah that could be that'd be all right we could even create a new material for this one so let's go to the materials folder right click create new material and let's call this one coin and then make it yellow here we could also do instead of just making this if we make this yellow here see it's oh yeah we have to actually drag the material onto the coin itself even though it doesn't look like a coin we've got a little dot here now a yellow dot but see that it's still applying shadow to it you could do this you could take emission here and do yellow so you can play around with that a little bit as well to get the look that you want so it nearly looks like a coin now even though it's a ball but let's just leave it like that for now maybe we move it a little bit to the side here so you don't collect a coin straight away and then we'll also rename it here so it's not called sphere let's call it coin and we should also move it into this uh level so we can hide it away because it's really part of a level this coin but it's still visible here and and everything like that and remember that we created layers before and that will come in really handy again in the layer here let's do add another layer and let's call this one coin and then we'll go back to our little coin game object and remember we have to change this layer here again to coin and now this is on a separate layer here but it still looks pretty much the same i can control d this one move it to here ctrl d let's put one in the air up here hold the ctrl key to snap it ctrl d again let's put it here okay so what would happen if we press play now let's try if i come running here and then oh i bump into the coins i don't really collect them but they're more like part of the level so to fix that what we could do is uh we select a coin here and we want to change the collider here to a trigger instead is trigger and now we didn't use the power of prefabs unfortunately see so that's why we should have really done this as prefabs instead and you should try to do that as early on because what i'll do now is i'll delete these and then i'll select this one again and let's make this one a prefab so we go to the project folder here prefabs and then drag this coin into the prefabs and now we've created a prefab out of it now the prefab already has the correct layer here the coin layer so now when we duplicate this one it's going to be all of these will have the coin layer onto them so what we changed here which was really important is we set it to is trigger instead and that basically tells the physics engine that it should register a collision but it shouldn't make a physical interaction between those objects so it still won't collect them but at least it's they're not stopping me and in the code now we can detect this trigger remember that we did on collision enter before there is another one that's very related to that one another mono behavior called on trigger enter and if i press tab it'll auto complete it here and i'll say collider other and this is actually the collider that we're colliding with remember that this player component is running on our player so the other collider in this case will be the trigger that we're colliding with and that's going to be the coin trigger but still nothing is really happening yet in the code here so we need to check the layer of what we've just entered the trigger of now and to do that we do if other which is the other collider now but we actually need to reference the game object that that collider belongs to so we can type game object here and press tab and then we want to check the layer of that game object so it's everything is like basically a hierarchy of things that you can traverse up and down so we press tab there okay which layer number was it because this is a number if we hover here we see that it intuts us which is an integer and that's a whole number like one two three four five and if we alt tab back here we can see that the layer coin here is number nine so we can do if layer is nine equals equals remember the double equal sign open and close squiggly brackets so if it is nine here what should we do let's just destroy the coin destroy other but remember other is a collider so we need to reference the game object we want to destroy the game object game object control s so now we're saying okay i've i've entered the trigger of a collider and the collider we can access by this other variable or parameter and then we have here if the other collider and then we reference the parent game object that that collider is belonging to and then we're looking at the layer of that game object if that equals nine then we should destroy the other game object it's very important to have other dot game object because if i just wrote game object here i'll destroy myself and now you can see that we're actually collecting these coins now and here you could start adding stuff like adding a score for example maybe it's not maybe you want to have a coin collect thing and stuff like that you can just put here and have a variable increasing every time you destroy a coin could collect it maybe get a point a score but instead of adding a score keeper now let's try to do something different so maybe we'll give the player a super jump every time he collects a coin so let's define a new variable up here and this time it's going to be an int an integer which is a whole number like 1 2 3 4 5. and let's call that one super jumps remaining and by default this will have a value of 0 because we're not initializing with anything so it's going to be zero in the beginning so here when we have the ontrigger enter and we've checked here that we're colliding with a coin then we can do not only do we destroy in the coin object but we also do super and remember you can press tab and then we'll do plus plus and that's a strange thing as well but that means increment by one you could also have written plus equals one that's also a way to do it and if you wanted to increase it by two for example then you'd give it two super jumps this way let's just give it one for now so super jumps remaining plus plus means that we're incrementing this value by one and then we can actually use this value here so when we're doing the jump check here let's see jump key was pressed we've got something adding here then we can actually define a little temporary variable here and you can do that in here straight in in this method you can type float jump power equals let's default this one to five and you don't really have to type this f here if you don't want to it's only if you type a decimal point like 5.2 for example that you need the f so we can just set it to 5 now but internally it'll actually write it 5 f let's just keep it at 5f and then what we'll do is if super jumps remaining is greater than zero and here we'll do jump power and here we'll do jump power times equals two and that will take the jump power and multiply it by two and then we also have to do super jumps remaining minus minus which is a way to reduce it by one so what's happening now if we press the key it's setting always setting the jump power by default to five but if we have any super jumps it'll double the jump power and then it'll decrease the number of super jumps that we have remaining and we also have to replace this number down here to jump power because we had it to a fixed value of seven before but now it's actually going to be back to five so it's not going to be able to clear that those height jumps but if we have a super jump power then we can do it so let's go back to the game and press play let's move jump clear and now when i jump i should have a lot more power and every time i collect one of these coins i'll have that super power and here we should be able to clear it so now instead of adding a score counter which you could have also done we've actually given the player some super jump powers i should also mention that for the overlap sphere there is actually one called checksphere as well and that will return a bool that we already know true or false but i wanted to show you this overlap sphere as well because it's quite common that you want to access what which colliders you're colliding with and with that with this overlap sphere you have access to those but theoretically you could have done a check sphere here as well which would return a true or false to continue the level you could just keep selecting these tiles pressing control d to move them i usually hold the control key to get this nice snapping feature and you can also block select by just click and dragging here and then press ctrl d to duplicate the whole block and move them up and down and i press ctrl s to save this scene and i collect collapse this level and i can collapse this one as well and in the root here we just got it quite a nice tidy hierarchy now we've got the level and we've got the player and then we could if again if we wanted to duplicate these coins ctrl d make sure we put a super jump here ctrl d we need to put one here and then you want to play test your level to make sure that you can actually complete the levels as well okay we're going to finish off now this was a very detailed video that produced a really horrible looking game if you could even call it a game but that's beside the point a little bit because what you've gained now hopefully is a foundation that you can build upon i'm hoping that it's giving you a little bit of a confidence how you can navigate in the ui what the different things do what a game object is what a transform is and even how to create your first c sharp script and hopefully that won't scare you off too much there's so many more things i wanted to cover and i'm going to build a lot more unity tutorials but this one is going to be the most basic unity tutorial i will ever make i often get the question how did i get started with unity and how did i learn how to make games and this is exactly how i started i started with something really really primitive and then i built on that i asked myself questions over and over again i asked myself a lot of can i questions can i create a simple capsule that represents a player can i add gravity and make him jump can i make a move left to right can i add more objects for the level can i model the objects in blender instead to make them look nicer can i add some impact effects when the player lands can i make some sort of a suspension bridge by connecting joints and can i model a character instead and can i make the character walk through a walk animation and can i improve that animation and actually make a run animation instead can i model some weapons and put him in his hand maybe a sword and a shield or can i model a gun and have him shoot maybe even in the direction that he's aiming can i add some sound effects maybe to make it sound like he's shooting can i switch to a two-handed weapon maybe a shotgun can i switch to a laser weapon that maybe ricochets the bullets when it hits something can i switch to a minigun to make a lot more bullets fly all over the place can i have an aim over legend and have it pointing down can i make a grenade launcher maybe that fire stuff that will explode and send some shock waves can i add a background maybe like a gradient sky and can i add more details to the background maybe like mountains and can i have some sort of a parallax effect by including more layers of backgrounds can i add a waterfall maybe a low-poly style one can i add some clouds can i even add some cities in the background or spaceships can i add some blurry effect by applying some post-processing can i add some smoke or dust where he runs and can i even add maybe some lighting effects when he shoots like a muzzle flash can i have some impact effects as well where the bullets hit maybe then can i add some objects that he can actually hit maybe uh in the future i can also add some power-ups that he can collect can i make a reload effect can i make a ragdoll out of him if he dies maybe so ask yourself the question can i and take it from there behind all of these can i questions are hundreds and hundreds if not thousands of hours of googling trial and error prototyping looking in the documentation looking at youtube videos doing more trial and error youtubing you name it just that's how i learn i just repeat it over and over again and i learn little by little over time in a similar way to this i actually learned how to use blender about five years ago i started by trying to modify the default cube and by now i can actually do about 10 cars in 10 minutes like in this video or i could do five rigged characters in 10 minutes as well and that's just from practicing over and over and iterating and getting better over time so if you want to make games in unity my advice is start extremely simple allow it to look terrible repeat and iterate time over time and do the same thing over and over again and improve and have patience and i also have to say that i think it's really important that you enjoy making games if you find this to be quite boring tedious or if you want to just put it away and do something else then it's gonna be quite a struggle but if you enjoy it if you enjoy making small steps a little bit of progress and seeing what you can do next then you're on a great track i think you're gonna do just fine making games hit the thumbs up button if you like this video and don't forget to subscribe if you want to see some more game dev stuff about unity blender 3d low poly you name it i've also got a patreon page if you want to drop by and give that a little bit of extra support if you can and check out the description for some merch as well i've got some t-shirts and hoodies and stuff like that until next time have a great one and i'll see you then [Music] you
Info
Channel: Imphenzia
Views: 1,213,605
Rating: 4.9750428 out of 5
Keywords: learn unity, unity for beginners, unity tutorial, tutorial, c# script, user input, platform game, imphenzia, low poly, how to make a game in unity, how do I make a game in Unity?, teach unity, make game unity, unity games
Id: pwZpJzpE2lQ
Channel Id: undefined
Length: 124min 31sec (7471 seconds)
Published: Fri Jul 24 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.