Unity Full Beginner Tutorial 2020 | Make your first game!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi and welcome to this full on beginner unity tutorial I decided to film this intro outside because why not this tutorial is meant for people who are just starting out with unity or don't even know what unity is and want to start out in game development or if you already know some of unity and want to learn a couple of extra tips and tricks this video is for you so in this video I'm going to be covering the very basics of unity such as downloading unity placing basic objects in your scene scene setup materials rigid bodies colliders coding and at the very end we're going to make a full-on game so I hope you stick around also these are the timestamps in case you're interested in just one or two things and I'll see you in the tutorial here we go so if you have an installed unity yet we're going to start off by going to unity 3d comm slash get dashed unity slash downloads so this URL right here once you are at that URL we're going to click on download unity hub and the reason why we're downloading unity hub as opposed to just unity is because unity hub allows you to manage different versions of unity so it basically allows you to update unity without any issues further down the road I always use unity hub as opposed to just a regular version of unity so that's what I would suggest so we're going to click on that and then we're going to save this file next up we're going to want to locate that file it's called unity hub setup we're going to double click on it go in the hit yes and it's going to bring up this interface for installing unity I'm going to click on I agree and then install it because I already have it installed I'm not going to click this but once you do that it might prompt you to sign in or create an account and then once you get through that it's going to bring you to this interface this is where all of your projects are located if you already have some projects they will show up here so what we want to do to make a new project is we're going to bring our mouse over to the top right hand corner and click on new once we do that a new interface will show up showing us our options so we can even make a 2d project 3d project Preedy with extras high-definition render pipeline lightweight render pipeline VR lightweight render pipeline so I'm going to stick with basic 3d if we were the hit 3d with extras that will give us a couple of more assets but because I want to start from scratch I'm going to hit 3d we can name our project here so I will call this video projects just because this is for a video you can name it anything you want and then this is the file location here you can select whatever file path you want you can open file explorer by clicking on the three dots here so I'm going to click on create so it might take a few minutes for unity to actually make the project but once the project is created it will load up the editor and now the magic can begin so you might be wondering what are what's everything that's going on here so there are a bunch of windows here and there is the scene view here you can scroll around to move around in it the objects that are in your hierarchy which is this window on your left it's called the hierarchy these contain the objects that are actually in the scene so we have a main camera and we have a directional light so down here we have the project file these are all of your files that are saved as as objects within your scene or or within your entire project so we have our scenes folder and we have our sample scene this is our sample scene right here we can make new scenes at any point but if I were to go back to assets right here if I were to click on assets we can make new assets to be put into our scene which would be in the hierarchy so if I were to click on for example main camera it will bring up all of its properties in the inspector here so the inspector is on the right-hand side of your screen so it shows all of the inspector properties which we'll get to that in a bit and if I were to click on directional light then it will bring up it's inspector properties this is basically where we can change all of the properties of any object there are all so other windows such as the game window when when you run the game so by clicking play if you were to run the game it will bring everything up in the game window but we'll get to that later as well and then there's also the asset store but that's a more advanced topic the asset store is where you can buy and download assets made by other people now you might be wondering what is an asset an asset is just in objects of any sort it can be an image a 3d model a sound it's basically anything that you put in your scene or in the project view so next up we're going to talk about creating objects so primitive objects and basically creating things to put into our scene first though I want to show you how we can organize some of these windows here because I personally don't like how the windows here in unity are organized so what you can do is you can actually click and drag any window you want so I'm dragging the hierarchy right here if I want to put it over here for instance on this side let's say I wanted to put it here now the hierarchy window is over here and the scene window is over here I'm going to drag the project window and I'm going to move it in between the hierarchy and the inspector and now now this looks a little bit weird I'm going to click on the three bars right here and I'm going to switch it to a one column layout and that just makes it so that I can easily see all of my folders and assets and packages without it taking too much space down here and I'm just going to leave the console down here just because it's the console isn't as important until you begin coding of course now something weirds going on here oh the console is actually floating right now so I'm just going to bring it there there we go and now actually what I'm going to do is I'm going to click on the game view here the game window I'm going to drag that as well I'm going to put that right underneath the scene window so that we can see both the game view and the scene view all right that's really cool now this will go back to its default view if you close unity and then open it again I think so we can actually save this window layout right here by going to the top right here if we were to click on default we can actually save this layout so as you can see I actually have a few late layout saved I'm going to click save layout and I'm going to call this tutorial layouts now that that's saved it will bring this up every time I load unity unless I were to bring up a different layout here all right on to making primitive objects so first I must answer this question which is what is a primitive object well I'm glad you asked a primitive object is any 3d or 2d shape that doesn't need any additional 3d modeling to be made so for example a cube is a primitive object whereas a fidgets spinner is not a primitive object so we're going to be adding primitive objects to our scene right here and we can do that by right underneath hierarchy we can click on create and then a whole list of objects come up we can create an empty empty child we're going to focus on 3d objects for now we're going to make a cube now when we click on cube as you can see a cube shows up in our scene now here's the thing we want to position this cube somewhere wherever we want it to be so this is where the transform property comes in with the inspector I want to bring the cube to the exact center of my scene so right now the position is a bit weird we have negative 0.7 etcetera etc I'm just going to click on the gear icon and then click on resets and now the cube disappeared so because I have the cube selected in the hierarchy I clicked on it if I were to hit F the F key that would bring my focus back to the cube okay so now the transform has been reset I want to make an primitive objects so I'm going to click on create and let's create a ground to go underneath our cube so I'm going to click on plane now what plane is just a 2d square and it's two-dimensional because if we were to look underneath it it doesn't show anymore so light basically shines on one side and the other side is just completely transparent now what I want to do is is I want to scale this plane so that it's a little bit bigger so we have our editing tools up here if you look at the top left hand side you'll see that we have the hands tool the hand tool lets you pan around the view the move tool if if I were to for example click on this cube we have the three arrows here so I can click on the y-axis to move it up and I can click on all these arrows or if I want to move it along two axes I can click on these squares here that would move it along the Y and the x-axis and the same goes for the other two we can click on the rotate tool this will enable me to rotate anything and of course we have the axes that you can rotate things on as you can see right here now one thing that I should point out that's interesting is that there's local and global axes so for example let's say that we have Center and global here well actually we'll focus on Center in the second for global as you can see no matter what happens if I were to click this back you can see that that the that the rotation circle sphere doesn't align with the actual rotation of the cube if I were to click from global to local that would actually align the rotation circle I'm sorry not circle the rotation sphere with the actual rotation of it and that also goes with with movement so as you can see the arrows are now adjusted to what its rotation is and that also goes with scale and that's the next thing that we're going to talk about so if I were to click on the scale tool right here we can see that there are now these little cubes here so if I were to click and drag the middle white cube I can scale it up and down on every axis or I can scale on one axis so the red is the x axis or I could do it on the z axis or the y axis there's really a lot of freedom to what you do with this and then also we can also adjust the scale the rotation and the position in the transform property right here in the inspector alright cool so now I'm going to select my plane and I'm just going to scale it up a little bit just so that we have some more room and let's create a couple more objects so I'll create a capsule and I'm going to just you know move it up put it into position I'll make a little Museum for my 3d objects let's make a cylinder as well there we go so we have a cylinder and this just shows how many simple objects you can make now most of your game if you're making more of a complex 3d game it won't be just simple objects like these it's going to be more complex objects these objects are mostly for testing or if you're making a low poly game you can use these objects as well there are also 2d objects this is for making a 2-d game there's also effects which is a bit more complicated we have particle systems force fields all that type of stuff we have lights now here's the thing with lights there are different types of lights we'll get into this later but currently the only light that's here is a directional light and if I were to uncheck this as you can see there would be no lights in the scene the only reason why it's what why it's highlighting anything right now is because there was a skybox which is basically the sky and the sky box is emitting air is on emitting lights onto these objects but there's no shadows at the moment okay so that is how you place and do basic positioning on primitive objects next up I will talk about colliders so we've placed our primitive objects what happens if we want to have them move around be affected by physics and basically bump into each other well you need two things you need a Collider and you need a rigidbody to start off if we were to click on any of these objects for example this cube right here you'll see on the right in the inspector you'll see that the cube has something called a box Collider and you can see this box Collider by if you look really closely you'll see a small green outline here this is your box Collider and this green outline shows the boundaries that is on your box Collider and here's the thing with colliders it can either be a trigger or it cannot be a trigger it's currently not a trigger which means that objects cannot go inside of it if they have another Collider we will get into triggers later but if I were to check this you would basically need code for this to work so if it's a trigger you can write code and have something happen if an object enters the trigger but we'll get to that later I'm going to leave this unchecked so as you can see we have different types of colliders if I were to click on the capsule here it will have a capsule Collider if I were to click on the plane it will have something called a mesh Collider Nach colliders are a bit more complex if I were to type in Collider we have a whole array of or we have a whole list of the colliders that we can choose from we have a box Collider a bunch of 2d colliders but I'll ignore those for now we have capsule Collider mesh Collider terrain Collider sphere Collider so if I'm in a wheel Collider as well so we can choose which Collider we want our objects if it's a more complex object that's when we would go with a mesh Collider but as you can see our cylinder also as a capsule Collider so if I were to click maximize on play down here and then our to play the scene by clicking on the triangle here what we could do is well actually we don't want to maximize on play because I want to manipulate the objects in the scene so I'm going to click on the triangle again to exit the play mode I'm going to unclick maximize on play kind of drag this window up a little bit and then click play and then I'll show you what happens if I were to try to drag objects into each other so right now colliders don't work because the objects don't have a rigidbody or they don't have rigid bodies attached to them so colliders don't really they're not effective without a rigidbody because a rigidbody adds physics to your objects so we're going to unclick or not unclick we're going to on play the scene and I'm going to add rigid bodies to these objects so I'm going to start off by adding a rigidbody to the capsule now how do you add anything well you would click on the object so my capsule and you would click on add components so I'm going to type rigidbody now you need a Collider for a rigidbody to work so these two work hand-in-hand this is where physics comes into play so here we have a bunch of properties I'm just going to collapse all of these just to keep it you know focus on the rigidbody we can adjust the mass the drag angular drag really the thing that we're focused on is use use of gravity so if I so if if gravity is checked here and I were to click play as you can see the capsule is going to fall now the reason why it actually hits the plane and didn't go through is because the plane has a mesh Collider and the mesh Collider prevented the capsule from going through it all right so clicking on play so now if I were to uncheck gravity and then click play the capsule will just stay in this place there however I believe if I were to move this cube here switch it from local to global and I were to try to Ram it into this capsule it would still go through it mainly because the cube doesn't have a rigidbody either so if I were to add a rigidbody to this cube and then I were to reset the transform so that it goes back to regular cube I'll scale it a little bit now what's going to happen is in order to uncheck use gravity now what's going to happen is so the cube actually started in inside the collider of the plane so that's why the cube kind of exploded there I can move this cube down now the cube is still gonna go up because it is not affected by gravity if I were to change the drag to something like 10 then the cube will just kind of stay in place there if I were to change the angular drag to something like 10 then it will stop rotating that basically means that it will take a lot of something very heavy to make it make it fly through the air again now if I were to ran this into the capsule it will actually move the capsule now as you can see now the capsule is just going to go flying into space so both objects need a Collider and the rigidbody to be affected by movements basically okay so now what if we don't want them to move at all what if we want them to collide but just but just have one of them not move well I'm going to click on is kinematic and this basically means that it's still affected by collision but it's not going to move at all within your scene it's it's just kind of bound to that position so if I were to hit play well actually first I'm going to add gravity to the capsule if I were to hit play as you can see the cube stays in this place the capsule however does not want to go inside the cube so even though it's colliding with the cube it's not it's not going to move the cube in any way I can still move the cube in the scene view ah now as you can see the capsule is flying into the abyss because it went off of the plane but anyway the only reason why this cube is moving is because I'm I'm moving it in the scene view this isn't the actual game view this down here is the game view but well gets that later so it can't so it won't be affected by physics if it is kinematic basically alright so that's everything on colliders and rigid bodies that's all of the basic stuff now we're going to get on to actually modifying the size of our colliders modifying the size and the position of it so it's it's pretty different for each for each Collider that you use for example a cube Collider or a box Collider will have its Center and its size so we can we can change the center and as you can see it moves that green outline up and down so I'm affecting it on the y axis I can also affect it on the x axis I can affect it on any axis the same thing goes with the size I can affect how big it is and so on and so forth and the same thing can be done with a capsule Collider we can change the radius and the height and we can change it on multiple axes here now for a mesh Collider a mesh Collider is a bit different because mesh colliders are mainly meant for objects that are not primitive so for example if I had a rocket ship for instance I would want to put a mesh Collider on it because something like a box Collider or a capsule Collider wouldn't really fit exactly the shape of a rocket ship so a mesh Collider actually draws the boundaries around the object itself and I don't have any examples here but we can get to that if I have another not primitive example to show you so that's how you modify colliders now what happens if I wanted it to be a trigger so for example let's say let's say this is my player the capsule right here and I wanted something to happen if the capsule went inside of this cube now this is where we get into trigger zones and trigger zones occur if your box Collider or any type of Collider is a trigger I'm going to select his trigger and also I'm going to uncheck the mesh renderer this this gives the 3d objects its look so if I were to uncheck it as you can see it does it does not show anymore the only thing that you see is the box Collider actually we don't see the box Collider where is that box oh okay there we go so so the Box Collider wasn't showing because this wasn't expanded here so yes so now what happens if this is a trigger okay so what would happen is things can things can go through it right so it's basically as if nothing is there but this is kind of an invisible trigger zone where we can run code if something enters this so this is our introduction to writing code so now what we want to do is run some code when the capsule enters this cube right here so I'm going to once again line check the mesh render so that we only see the trigger zone here now once again we have is trigger activated so what we want to do now is add a c-sharp script component and what we're going to do now is click on add component and there is no way to just add a c-sharp script we're going to type in the name of c-sharp script here so if I were to type in let's say Enter trigger now you can name your c-sharp script anything you want but when I click on something that it doesn't recognize it would bring up something called new script and I'm going to click on create and add and that adds a new c-sharp script called enter trigger when it's done compiling it will bring up the script here now you might see that it actually creates the script in the project folder here and that's actually really good because now we can we can we can modify the scripts from our project folder this is now on your computer so the project window are all of the files that are on your computer I'm actually going to make a new folder right now by right-clicking going to create and then folder and I'm just going to make a new folder to hold all of our scripts I'm gonna call it scripts hit enter and then I'm going to click and drag enter trigger into our scripts folder and once that's done compiling you can see that enter trigger is in the scripts I'm going to double click on this - to open whatever editor I'm using I'm currently using Visual Studio code 2019 sorry not Visual Studio code just Visual Studio Visual Studio 2019 you can choose whatever you want to edit your code by going to edit and then preferences and as you can see I'm using Visual Studio 2019 and you can browse this and you can basically use whatever you want I think unity starts you off with something called monodevelop there might be a bit outdated though I'm not sure if it starts you off with it anymore but if you have Visual Studio then you should be just fine so I'm going to double click on enter trigger it's going to load up in Visual Studio and it might take a couple of minutes to actually start this depending on what code editor you're using so we're just going to wait for that to load and once this so what so so as this is loading I can tell you what we're going to do we're basically going to create a method of code that will run when anything when something enters in the trigger okay so here's what we're going to do this is assuming that you have very basic coding knowledge so but if you don't have basic coding knowledge at all this this is also a good way to start I won't go over the super basic basics of it but but I'll guide you through on on how to make this at least so we have two two methods here we have void start and void update void starts this runs once at the very start of your game so it's called it's called before the first frame update so before the first frame finishes it it calls start so let's say I wanted something to happen at the very start if I were to type in prints and then hi or hello this will print the word hello to the console once when the program or when the game runs in the other so I'm going to click na click do ctrl s to save it and when I go back into my scene in unity and I click play what's going to happen is in the console down here we're going to get a hello and hello to you I'm going to unplayed us and I'm going to go back into visual studio and I'll just get rid of this because we don't need it actually we don't need the start method at all so I'm just going to delete it now here's the deal with update update is called every single frame so if you want to run something every single frame then you would put that in the update method I'm going to get rid of this method as well because we don't need it the method that we need we need to name this method very specifically and this going to be called on trigger enter so basically this method is wrong whenever something enters the trigger I'll never wait a second this is on particle trigger whoops on trigger enter okay there we go that one's the right one so private void ontriggerenter collide or other so what's going on here is it's taking in the collider of the other object that is entering the trigger so it needs the collider in this case so basically the capsule here it's collider is going to be the capsule Collider so it's going to do it's going to detect this and it's going to pass it in as an argument to ontriggerenter so now what we need to do is we need to write some code to see what happens when something answers it so let's say let's just print something out entered there we go so it's going to print entered to the console whenever something enters its trigger so I'm going to hit play let's see what happens now I pretty much know what's what's going to happen already it said entered once and that's because the trigger is currently inside this plane which has a mesh Collider if I were to move this out and then back in you'll see that it runs entered again so basically every time something enters it enters that enters the trigger it will call that method once so if I were to do that it'll call the third time a fourth time the fifth time six time all that stuff what happens if we want to check for a certain objects to enter the trigger well we would do that by assigning tags now whether tags tags well it's pretty self-explanatory a tag is I guess a tag that you can put on a certain object and you can assign tags by going right here so its current so our capsules is currently untagged if I were to click on that there are some default tags that we could put on it but I'm just going to add a tag and I'll call it no not player because it's not a player yet I'm going to just type in capsule yes capsule so if I were to type in save and I were to assign the capsule tag to our capsule our capsule is now tagged as capsule now you can name there or you can name tags whatever you want it it's up to you but the capsule now has the tag capsule so what we need to do now is if we want to detect if this specific object enters the trigger we need to detect what its tag is so how you would do that is you would write an if statement if other dot compare tag so capsule all right so what this if statement does is it goes into the collider and then it basically goes up and we're running the compare tag method and we're passing in the name of the tag now this is case sensetive which means I had to capitalize the C so if other duh compare tag capsule we're going to print by capsule so so whenever that specific capsule enters in the the trigger we're going to run this so if I were to now hit play as you can see it's yeah so it's still going to say entered I'm actually not sure why maybe because I didn't save uh-huh yeah so if you don't save your code it's not going to update in the editor okay so I'm going to hit play now and it should clear the console great clear the console if I were to move my capsule in it's going to enter the trigger and then exit I like I'll enter it again so because I entered this trigger twice it printed out twice hi capsule and it's only going to do it with an object that is tagged as capsule now if I were to add the tag to any other object for example the cylinder and I were to enter the trigger then it would print it again so it's basically any object that has a tag associated with it so this is really powerful because because that means that you could have a player let's say for example you're making some sort of game where a player enters a shop and when the player enters the shop something runs up to the player you can do this by by putting a trigger at the door so that when the player goes through the trigger that some specific code will run so triggers are really powerful so so basically colliders have have have two super important functions it it's either not a trigger or it is and both of them are really powerful depending on how you use it also something I I once mission is you don't need a rigidbody when you're calling a trigger so I can actually get rid of this and and the whole thing will still work all right so that's rigid bodies colliders and triggers so now on to materials so everything right now looks just plain white there's no color to it we want to add something called a material to give it either just a plain color or a texture so what we're going to do is we need to make these materials from scratch and it's pretty easy so in our project folder we are going to create a new folder and I'm going to call this materials and inside this folder I'm going to make a new material so I'm going to right click in materials create and then select material and I'll name is whatever I want I'll just name it red because I guess we will make it the color red ok so we have a few things here we have the albedo which this is just the color this is just the flyout color so I'm going to make this red okay so and then there is how much of a metal it is and then the smoothness but right now right now we'll just make it red so to assign a material to something an object I'm going to drag the material over to the object and as you can see it it gets applied to any material here so I will drag it on to this this capsule here and now what I could do is I can determine how metallic I wanted to be in how smooth I want to be if I were to drag it all the way over to the right as you can see now it gives it more of a metallic look basically if it's metallic it it would reflect lights in the same color that it is so it's going to reflect a red light versus if it were not metallic it would reflect more of a white light which is being displayed by the directional light and also sky box we can also adjust the smoothness so if it's super smooth and it it looks super smooth it looks like it has a glossy finish to it versus if it was not smooth at all it just looks super I guess rough but I'll leave that in the middle now usually with things being metallic we would we would want it to either be at the beginning or at the end from zero to one there's really not much of a situation where it would be partly metallic partly not metallic so yeah that that is basic materials now we can add a texture to this the albedo normal map I'm actually going to make a couple more materials I'll make a green one there we go green so I'm going to apply this to the ground I guess it's the ground now and I will make a blue one okay so here we go here's a blue one okay beautiful so now so now we can control these materials individually we can change the color of it however we want so yeah so that is materials at its most basic state there's a whole bunch of other things when it comes to materials but for this very basic tutorial I'm just going to cover the basic colors of a material actually I'll make this a metal trash can so I will make this fully metallic and I'll also make it super smooth too so now it's a trash can for whatever reason and this is a capsule there we go there's materials so we've dealt with a whole bunch of things in unity including coding so now what if we wanted to actually move the player around the environment that's going to require a bit more code and it's going to require us to use input so for the sake of this tutorial I'm actually going to delete this capsule and instead I'm going to create a sphere it's easier to move a sphere so basically I'm going to show you how to use the WASD key to roll the sphere basically so I'm just going to scale this a little bit and I will apply the red material to it so there we go so now what I'm going to do is I'm going to make a new unity script so I'm going to click on my sphere and actually I'm going to name this player so if you have something that's selected you can click on it again to rename it so that's how I'm able to rename it so now to make a new script once again we are going to click on add components and I'm going to make this I'll call this rolling ball hit enter hit enter again for create an ad going to wait for that to compile there we go so now I'm going to click on rolling ball and I'm going to drag it into my scripts folder it's compiling again if you basically for things that are compiling you'll see a little spinny wheel on the bottom right hand corner I'll show you what it looks like in just a second so I'm going to double click on this open visual studio for me it's visual studio and actually I'll show you I'll show you what compiling looks like so let's say I made a little change here if I were to click Save and then quickly went back as you can see there's a little spinning wheel there you can see that that's compiling now for an error if there is an error so this code is going to give us an error as you can see there's going to be a syntax error here and it's going to say invalid token blah blah blah I'm just gonna say something but more importantly it's going to show what line the error the error is on so in this case it says it's line 14 line 14 oh well okay so the error is actually this it thinks that void is weird so I'm going to get rid of this hit the save key and as you can see the wheel is now spinning so it's compiling and this error is going to go away as soon as this wheel stops spinning which sometimes it takes a few seconds for the wheels stop spinning once I have the wheel spinning for like ten minutes that was absolutely crazy okay so apart from that we are going to create the input for rolling this ball now here's the thing for anything regarding movement when it comes to physics we need to have a rigidbody so as you hopefully remember we need to add a rigidbody to this so I'm going to type in rigidbody and looks like it's all set up so now what we need to do is within our code we need to access the rigidbody through our code now how do we do that now for for for me for any beginner for anyone along any skills path along with unity we always refer to the unity documentation so for example if I didn't know how a rigidbody works through code I can literally just Google unity rigidbody and what's going to happen is it's going to bring up a a unity documentation so it's going to show a whole bunch of things here we have rigidbody all of the properties the public methods all the stuff but really what what matters is the manual so if we were to go down here no actually oh okay okay so the manual just shows a bunch more properties what we're looking for is so for example let's try this okay so so what do we need to do we need to add force to our rigidbody based on the WASD keys so I would I would instinctively want to click on add force here so add force is a public method and this is what we're looking for we're looking for code so how do we get a rigidbody well it says right here we need to make a variable and the variable is going to be of type rigidbody so we have public rigidbody and then B is the name for it that's what they decided to have it be so I'm going to go back into my code and type blick rigidbody RB and this could be anything I'll I could call it rigid or any other type of name just as long as it's not the same name as the variable type and also note that you need the using unity engine library for any of this to work okay so we have public rigid body rigid now is currently no if I were to save this and then go back into the scene you'll see that we have this rigid body thing called rigid but it says there's nothing in there now we could manually drag the rigid body into this but what if we forget to do that it's much easier to do it in code so in the start method which is which is run before the very first frame we would type rigid equals get components rigid body and whatever component you have in here it will it will find that component assuming that there's one of them and it will assign that component to whatever variable that you have so for example yeah if I wanted to get the collider I can I can make I can make a public Collider variable and then assign it this way but this way we always have the rigidbody at the start of the game so now that we have the rigidbody everything is good but how do we get input from our WASD keys or or the arrow keys well unity has something that's very useful called input.getaxis so we need to make two variables for this we need to make a so basically two floats and a float is a number with a decimal to say it super simply we would make these two up here so we would say public floats horizontal and then public floats vertical now we're not going to be modifying these within the scene we're going to be modifying this in code so basically we need to update these these variables every time we press down WASD etc etc or if we're not pressing it down we want it to be zero because we want to have zero force in the horizontal or the vertical so it's actually pretty easy to to collect these values if we were to type in horizontal equals input dot get axis and then we would type in horizontal so basically if I were to type in a or D it would it would set it a value I believe between negative one and one we can figure that out when we go into the scene so now if we type in vertical equals input get axis vertical and I'm going to save this and I'll show you what happens when this is here so okay so we're going to play this scene now here's the thing this this script is running because there is something in the scene that has the script on it if this if this were to go away then the scripts wouldn't do anything in here because there's because there's no instance of it in your hierarchy but I'll bring it back and here we go we have the script again so what if I were to type the a key okay so the a key whoops there we go so okay so so I just typed the a key and the a key no this over here on the right horizontal is is quickly going from zero to negative one pressing the a key means I'm going to move the ball left so I'm moving it left which means that the horizontal force is going to transition to negative one if I were to hold the D key it would go to one if I were to hit the W key it would bring my vertical from 0 to 1 if I were to hit the S key it would bring my vertical from 0 to negative 1 so that's how we're going to get the force okay I'm going to go back into my code so how do we convert these values into actually giving a force to our ball so remember back in the documentation I clicked on the add force method so this is in the documentation let's see how they do it okay so yes we have this thing called fixed update fix update is very similar to update but there are some there are some differences I won't get into it in this tutorial but we have our B add force ok so we need to call our variable RB RB add force and then we need to do something where we take in something called transform forward actually we don't want to do it this way yeah actually I'll show you how we do it so we need to make a variable called a vector3 and basically a vector3 is a list of three values okay so take it as in XY and z axis so kind of like if you're moving something the vector three would contain those values so for example our transform the position is a vector 3 the rotation in this case is a vector three rotations a bit odd but yeah and then scale as a vector 3 so I will show you how to do this so we want to make our vector 3 inside the update method so vector 3 and then we will call this direction equals new vector 3 so this is going to make a new vector3 and it's going to pass in what we have okay so horizontal and then there's going there's going to be nothing on the y-axis because we don't want the ball to move up and down so we're going to say zero point zero f and then vertical now notice that I put this line here before these two lines that's actually a mistake I'm going to cut and paste this line below this line because we have to assign horizontal and vertical before we create direction and pass those three those three arguments in so now so now we have our direction we know exactly which way we want our ball to go based on the input keys now we need to call Ridgid add force okay so what is this taken add force takes in a vector three so let's see so yep so we have vector three-fourths force mode mode we're not going to worry about that yeah we're just going to take in force here so I'll show you what happens so if I were to type in direction there we go so it basically takes in the direction if we were to save this and then hit play and as I move my WASD Keys you can see that the ball is ever so slowly moving and if I were to hit the S key would ever so slowly move it back we can go diagonal a little bit now this is great and all but it's going really slow isn't it we need to change that because because the ball going really slow is not very fun so I'm going to go back into my code how can we make it so that Direction goes faster we need to give it a multiplier so I'm going to make a new float so public float speed and I can assign this to a default value I'll make it three three F and what we would do is we would type in Direction x speed okay so if I were to save that and then go back what's going to happen now is we have our speed here and I could change it to anything I want but we'll leave it at three let's see what three does for now so if I were to hit play let's see okay so three makes it a little bit quicker let's do you want to make it quicker than that I'll change it to something like twenty now this is gonna go really fast zum-zum yeah now let's see what happens if it knocks into that yeah so it knocked into our trash can actually what what's on the trash can yeah so um it actually didn't apply physics to the trash can because the trash can does not have a rigid body on it but just for fun if I were to add a rigid body to it I can actually collide into the trash can and here we go boom so yeah so now I can interact with the world around me yeah that's actually pretty fun so that is how you inputs a force and roll a ball with it next up we are going to make a full-on game using this mechanic so I real quickly put together this little obstacle course which as you can see I put a ton of effort making so we have a whole bunch of walls here which are purple and I actually organize the walls I put them inside a walls folder here now I did that by right-clicking and then selecting create empty and this just creates an empty game object and you can put anything you want inside of it so the objective of the player is going to be to navigate the obstacle course and get to the yummy cheese cube without hitting any of the trash cans if the player rolls into any of these trash cans you lose so we want to turn this into a full-blown game so how are we going to get started well we're first well first first off we created the code for rolling the ball already so we have that settled down but nothing happens if you roll into one of these trash cans so how are we going to fix this well we need to turn all of these trash cans into a trigger Collider so I'm just going to select all of them by hitting by basically selecting the top cylinder or trashcan and then hitting shift and then clicking the bottom one and that selects all of them I'm going to click is trigger right here and I'm going to add a new component I'm going to make a new script and I'm going to call this trash can there we go so this is going to be the script that enables the trashcan to make the player lose if the player rolls into any of them so I'm going to drag this into my scripts there we go so I'm going to double click to open visual studio and what do I need to do here so as we did previously I'm going to make a on trigger enter cloud or other and once again we need to do things based on tags so I'm going to go back into the back into the enter trigger script and I'm going to basically grab this code here copy and then go back into trash can there you go so we have ontriggerenter collide or other now we're going to want to do this based on tags so we have capsule I'll switch this over to player because because in this case the rollerball is actually a player so we need to make sure that the ball actually has the player tag on it so I'm going to select the default player tag so now the player has a tag so I'm just going to print you lose so let's see what happens now in the code all right I'm going to hit play and when I whoops well all of the cylinders just went into the ground so we actually need to get rid of the rigid bodies on these because we don't want gravity anymore they reel so now I'm going to hit play and watch watch for the console here because let's see when I roll into it yep so it says you lose so now basically anytime the player runs into a trash can we can we can run any code we want great so I'm going to hit on the play again now we want to make it set the player wins when the player runs into the yummy cheese cube so I'm going to make another script I'll just call it cheese cube whoops I have cap caps lock turned on cheese cube there we go new script create an ADD and there we go select it move it into scripts okay we are ready to work on that now so open it in Visual Studio I'm I'm once again going to copy this code put it into cheese cube and there we go and we actually don't need the update or start methods in this case I'm going to get rid of those in the trashcan method as well now these two scripts are essentially identical now except this time we're just going to add in a test message you win there you go so so now technically when when we when we run into the cheese cube we should win and we just have to make sure to click on is trigger for the yummy cheese cube to make sure that it all works so okay great so we have print messages for whenever you run into stuff except when you run into a trash can there's really no consequence for running into it except for it telling you you lose okay so let's have a B so that it teleports the player back to the beginning if you if you hit a trash can so so how do we do that well we need to go into the trashcan script okay so if the player runs into a trashcan it goes into this code so we need to somehow teleport the player back to its original spot so we need to go into other other is the collider of the player in this case that is pass in so we need to type other dot transform so what do we do we need to we need to modify the position of the transform and remember the transform is this component up here it's its position rotation scale so we need to we need to change its position okay so so we need to say transform position and then and then as I stated earlier the position is a vector3 okay so yes so so since the position is vector three we need to pass in the vector three so I'll say new vector 3 and now we can pass in the x y&z argument so x y&z whoops yeah XYZ but these actually have to be numbers or also give you errors so what is the position that we want to bring the ball back to so it seems like we have I'll make these clean numbers I'll make these three let's see three the Y will just be let's see yeah the Y I'll just keep let's say I'll make it one point five and then and then the Z can be oh that's the X Z can be something like like negative yeah like negative fifteen okay no no no I don't like that let's make it negative negative eighteen so we're going to teleport the ball back to this exact position when it hits a trashcan so 3 1 point 5 negative 18 3 around 0.5 negative 18 now as you can see 1.5 is giving us an error we need to put an F at the end of that to tell to tell unity that this is a float okay so let's see what happens so I'm going to click play now and here we go so there we go so when I roll into the trashcan it will teleport me back now as you can see the ball is is continuously rolling with with force so we can fix that we just need to go into the balls rigidbody and change its force to zero so we would type in other get nope no not that duh get components and remember we're looking for the rigidbody okay so now we have the rigidbody of the ball so now we have to type in let's see velocity velocity is how fast is going so equals vector 3.0 that's just going to change its velocity to zero so whoops we don't need the parentheses there yeah so vector 3.0 basically just means zero zero zero so that's what vector 3.0 means so now if we were to hit play and we were to enter the trashcan it should yep so now our force is zero and it just jostled it forward a little bit because I I kept holding on to the W key there we go so now we now we have the code that does there that teleports the player back when the player loses now we just have to have some sort of a winning thing when when the player rolls into the yummy cheese cube so let's see what do we want what should what should happen when the player enters the yummy cheese cube currently we just have you win so let me think about this we can have the yummy cheese cube disappear because because the player is eating the yummy cheese cube so let's do that so we'll just do this we will say we will say game object dot destroy is that right it's destroy let's see let's see what they give us an error yes it will got destroyed No maybe it's this maybe it's destroy whoops there we go destroy game object here we go so basically destroy is a method where it'll get rid of the instance of any object so if I were to type in destroy game object game object this is the same as this dot game object accepts Unity knows that it it basically knows that you meant to put in this so we don't need that so this is going to destroy the game object so it seems like our game is it seems like the minimum viable product is fully functioning apart from the camera stuff but we will get to that let's see if I can navigate this now so here we go rolling rolling I'm gonna roll into the wall here it's a good thing I didn't add any bouncing that's a whole nother thing okay so gotta be careful alright okay I got a straight shot for it boom I have eaten the yummy cube so now oh no I hit a trash can so so the minimum viable product which means that the minimum functionality needed for the game to work is done now there is one more thing that I wanted to implement and that is making making it so that the camera actually follows the ball because right now if I were to enter the game view like I just did the camera doesn't really do anything so we need to change that so here our camera right here or if you click on main camera in the hierarchy you can activate here now we can move this up and down notice how the camera preview is changing notice how the game view is changing we want to in this case I'm going to just rotate it down like this so I'm going to bring the game view up a little bit and I want it to be just behind the ball because I want the ball to be visible in the scene there we go now here's what we can do actually this actually no we can't do this but but I'll show you what happens if we do do it if you were to drag this inside of the player this would mean that the position and the rotation and the scale will the relative position rotation and scale will mimic that of the player the main camera is now a child of the player if I already hit play you'll see that I'm rolling the ball but you'll see what happens is the camera also rolls with it and we we obviously don't want that because then then it wouldn't work so we need to add a script to this main camera that will basically follow the player so I'm going to add a new component called camera move and once again this could be called anything you want so here we go so we've worked with positions before we did this with teleporting the ball back to its original spot we're basically going to do that with the camera now so here's what I'm going to do I'm going to let's see I'm going to get rid of the start method because we don't need to do anything on the first frame we want to update the cameras position every frame so I'm going to type transform dot position equals now what is it equal to we need to have we need to have the script know what the ball is because we would say ball no no yeah well I'll get back to that in a second so we need to have the transform of the ball we need the position of it so I'm going to say public transform player because the ball is the player is the ball I'm going to comment that out so that I know it's not going to give me any errors and this is where we make good use of assigning public variables so if I click on the main camera we have the camera move script here as you can see we have player we have none I'm going to drag the player into this and when I drag the player into the main camera its player variable it gets the transform just the transform of it so now we have the transform so now what I could do is I can basically just say player dot transform dot position and this is wrong but I'll show you why this basically teleports the camera to the ball and I'll show you why that's wrong because I'll know the camera is now just inside the ball and it keeps the rotation which is good but we're just kind of stuck in the ball now which is not good we don't want that so how do we fix this well we need to create an offset so this gets a little bit tricky but I'm going to say new vector3 so what do we want here we want to have some sort of an offset so what type of offset do we want well we want it to be slightly behind the player we want it to be at the same x-axis as its see because we want the player to be in the middle of the view here and we want it to be a little bit above the player so we want the Z position to be a little bit behind the x position to be the same and the Y position to be a little bit greater so vector3 goes X Y Z so we're going to say we're going to say player dot transform duck position because we want the X to be the same as the player's X we want okay so now for the Y we want to be a little bit above the players we're going to say player dot transform that position plus let's say five for now we'll just test it out and then for the Z will say player got transformed up position will say let's say minus five for now okay so let's see what's going on here cannot convert vector three to floats let's see oh yes okay we need player that transform that position dot X there we go dot y and dot C this basically goes into the players position and it grabs just the x position there so it does the same here grabs the Y position and it grabs the Z position yeah that's actually a good thing if I were one good tip is if you have an error you would just you would just hover over the red squiggly and it shows you what the error is so that's actually very useful that's how I D bug things all the time so I can save that and at this point I'm I'm just making it so that it's guessing check at the moment so let's see all right so it does move however I want it to be a little bit higher up so I'm going to change this let's say eight I'll change that let's see what happens now so I'm going to hit play and there we go yeah I think that looks pretty good that doesn't look all too bad so there we go let's bring this in to maximize on play and see what happens beautiful so this is a very basic rollerball obstacle course game and let's see if I could do this one more time no don't hit the trash can oh and haha there we go I ate the cheese so beautiful so we just went from basically not knowing what unity is to downloading it to working with everything ooh that was close to learn colliders rigid bodies learning how the code to making a full-on game so and that was I mean I'm not sure how long the video is at this point but I don't think it was too long here's how to build your game you would type you would click on file and then build settings so building your game basically converts your game to an executable file which means that you can run it outside of unity you would have to so yeah so so you would click on file build settings and then you would have to add the scenes that you want to the build so you just click Add open scenes and it would add our sample scene and you can do it for PC Mac Linux Universal Windows platform but there's a whole bunch of things here but if you were to click on build it'll open file explorer to select which which folder you want your build to be in and then when you click build it'll build it and then you can run it outside of unity so that's basically it now there's a lot of more advanced things that I didn't cover here but this is the very basics of unity and this gets you a really good jump start to making games and also with the unity documentation you now have some really good resources to help you learn something that helped me when I went past the state was I just kept googling things or if you're not using Google just searching things up in the search engine and the most of the times the unity documentation would show up and it would teach you exactly what to do with the code wow you made it through the entire video Congrats I hope you learned a lot if you did please leave a comment below explaining what you learned I really want to know what you got out of this tutorial and please like this video if you enjoyed it and if you got a lot out of it and be sure to subscribe to the sorcerer channel for more content like this and as always I'll see you in the next video bye
Info
Channel: Sorcerer
Views: 84,339
Rating: undefined out of 5
Keywords: Sorcerer, Studios, Unity, Blender, game, design, development, unity, unity5, dev, video, games, modeling, model, coding, unity3d, blender3d, indie, programming, code, lore, dungeon, dungeongame, unity tutorial beginner, unity tutorial 2020, unity beginner 2020, unity basic tutorial 2020, unity what is new, unity game engine, unity full beginner tutorial, sorcerer build, game development, unity beginner tutorial, make a game, unity3d tutorial, unity tutorial, sorcerer studios, unity 3d
Id: VqqefF1M1SQ
Channel Id: undefined
Length: 73min 15sec (4395 seconds)
Published: Sun May 10 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.