How to make a Video Game in Unity - MOVEMENT (E03)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

RemindMe! 3 days

👍︎︎ 1 👤︎︎ u/[deleted] 📅︎︎ Feb 09 2017 🗫︎ replies
Captions
hello everyone in this video we'll make up play a move to do that we'll have a look at variables if statements and player input so if you want to make games in unity this video is pretty much essential I'm ready are you ready let's go before we get started I just want to mention that you can now download the project files and the scripts from each video over at dev assets so if we go to dev assets comm this is a website I created where you can download really high quality 3d models such as this Western props pack we have a modern weapons bundle some cool-looking sci-fi vehicles and a lot more if we go to browse scroll down to the bottom here and click how to make a game we get to this page where you can download all of this stuff I've seen the love tutorial makers make their project files paid but I don't really think that's fair to you guys instead as with everything on the site here I've made it pay what you want so you can of course go ahead and grab it for free or choose to support me with some amount you can just click the one that you can afford here and then hit the blue button and you're good to go so back to the video in the last one we made our cube fly forward by adding a force right now a primary issue with this is that our player doesn't stay on the ground he just starts rotating and jumping and goes crazy one way to fix this is to go to the player find the rigidbody and add some constraints we could for example freeze the rotation on the x-axis and that should result in smooth movement however we do want our cube to be able to rotate so that when he collides with something it's going to there cold let's uncheck that and instead let's create a physics material the reason for the cube jumping around is that by default that's friction between the cube and the ground by using a physics material we can choose how much friction we want let's right click in the project go create and then select physic material we can rename this to something like slippery and we can set both the dynamic and the static friction to zero then we can drag this on torque round and you can't see it being applied but if we go ahead and select our ground we can see that the material under the box Collider is now set to slippery that means that if we hit play now there's no friction between the cube in the ground and he's just going to slide off and that's without restraining his rotation so if we now select the player and double-click on a player movement script on this line we're adding a forward force of 2,000 and then of course multiplied with time.deltatime however it would be really really neat if we were able to modify this value in the inspector instead of just hard coding this into the line to do that we use variables in c-sharp I like to think of variables like containers just like you can store items in a box you can store data in a variable in the beginning we focus on storing symbol values like numbers or text but variables can be used to store way more complicated data each variable is given a name this pretty much just means slapping a label on the container so that each time we use that name it refers to the data of the variable we also have to specify what type of data we want to store for status you should learn and memorize the four fundamental data types integer we just write int this store is a whole number it can also be negative float this stores the number with decimals string this stores a piece of text and boolean we just write full and this is the simplest of all the data types because it can only have two values true or false so creating a variable looks like this unity also allows us to edit a variable in the inspector to do that we mark it as public however this variable is currently unassigned it has no value we can assign the variable a default value by simply typing equals and then the value to get the value of the variable in code we simply write its name like here where we are printing the value 10 to the console we can of course also edit the value of variable here we set it equal to 5 and here we add one on to the current value making it 6 so if we want to turn this number into a variable that we can edit in the inspector we'll go up here and create some space will write public float because we want a number that we can fine-tune with decimal places then we need a name in our case we could do something like forward force and we can set a default value in our case we'll just set that to the same 2,000 then we can substitute this number with forward force so we'll just take the forward force here and paste it down there if we now say go back into unity we can see that we are now able to adjust the forward force in the inspector we could set it to 4,000 or to a thousand if you click and drag on it you can even find cheerin like this and you can even edit it while playing so now of course we need to change this comment here instead we'll change it to something like add a forward force and it's time to have a look at doing some player input now the fundamental thing to understand when doing play input is the if statement if you make some room beneath our add force method here we can write if open a close the parentheses and then open and close some curly brackets this is the basic structure of an if statement if statements allow you to the execute code if a certain condition is met this is where you put your code and insert these two parentheses is where you put your condition sometimes it's really simple say comparing two numbers we could write if 3 is bigger than 2 of course this is always going to be true and so this will always be called we can also say if 3 is equal to 2 and this is of course false so this code will not be called we can also write other stuff say if 1 is equal to 2 minus 1 and this is of course true or we could put not equal to you and now it's false but this is only the stuff we can do with numbers we can also put other stuff in here if we want to check if the user is pressing a certain key on the keyboard we'll go input get key and here we'll also open in closer parentheses and in here in quotation marks we write the key we want so in our case we want D to go to the right and now everything we put inside of these two curly brackets it's only called when the user presses D so we could of course just go ahead and add some more forces here let's say our be done add force and in this case we don't want to add a force on the z axis we want to do it on the X meaning right and left so we can go in here and add some numbers say 500 will then do C row on the Y and Sewer on the Z remember to close it up with the semicolon of course we want to multiply a 500 with time that out of time just like we do up here if you now save this I just set the forward force to something pretty low just so our cube doesn't go flying off we can hit play and we should see that when we hold down D our cube will indeed move so we can go back and add this to the other direction as well to do this we simply copy our code paste it right beneath instead of checking for the D key we'll check for a and we still want to add a force on the x-axis but we want to do so in the opposite direction so we'll just input a minus here again we can save this play the game and see that we can now move in both directions in fact why don't we go ahead and make our movement speed adjustable in the inspector it the same way we did with forward force to do that we just add a new public float up here and we'll call this something like sideways force let's just default that to 500 by the way when we're dealing with float variables you will notice me writing an F at the end of the number this just signifies that we're talking about a float number in some cases we can leave it out like here but other times unity will complain so it's a good idea to get in the habit of just doing it every time again this only applies to floats not integers so now we can take our sideways force and paste it instead of the 500 here and instead of this 500 here as well you'll notice that it's still fine putting a minus here when we run the game this is just going to be equal to 500 or what we said in the inspector let's save this go back into unity and we can now see that we can adjust the sideways force as well let's try setting the forward force to something like a thousand and the sideways force to something like 600 and it already starts to feel like a game if you were able to follow along with this really good job programming the movement of the player is definitely not the easiest thing that you can do in unity and so it's always annoying that it's really the first thing that you have to do in most cases but I hope you learned something and remember you don't have to understand and definitely not remember all of this the first time I will just mention that the way that we're handling player movement here is not the most efficient way but no one expects you to write perfect code right off the bat I just want to let you know how this could be improved there's two main problems here the first one is how we're getting player input now using input dot get key here is totally fine and in our case it works perfectly but as soon as we want to do stuff like smoothing player input supporting alternative keys or maybe even a controller unity has some much better ways to handle the player input so you can definitely look into that if you want to improve it also we're currently checking for input inside the fixed update well it's fine in our case because what we're doing is movement and that's usually a fairly smooth thing it's definitely not a good idea to do for stuff like jumping or the one-off events the reason for this is that input is updated in the update method and if the fixed update runs slower you might have a situation where you actually miss some player input first that would just mean that the input gets slightly delayed and that's why I think it's fine here but the recommended thing to do is check for an input in the update method then store that input in some sort of variable up here in our case that could just be two boolean called something like move right and move left that you'd set it true if the keys pressed and false if the keys released and then check if those variables are true or false down here to add a force but it really wouldn't worry too much about that right now so let's pretty much it for this video if you enjoyed it make sure to subscribe so you won't miss the next one also if there's anything you would really like me to include in this mini series definitely leave a comment and I'll see what I can do so thanks for watching and I will see you in the next video thanks to have the awesome people who donated in January and especially thanks to Derek heaps Kirk face O'Meara Phi James Calhoun Robert Vaughn and Peter Locke and Jason dirt-eater if you want to come a picture in yourself you can do so a pitch intercom / brookey's thanks about guys
Info
Channel: Brackeys
Views: 2,059,073
Rating: undefined out of 5
Keywords: brackeys, unity, unity3d, asset, assets, model, texture, beginner, easy, how, to, howto, learn, course, series, tutorial, tutorials, tip, game, development, develop, games, programming, coding, basic, basics, C#, player movement, playermovement, first
Id: Au8oX5pu5u4
Channel Id: undefined
Length: 8min 57sec (537 seconds)
Published: Wed Feb 08 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.