How to make a Video Game in Unity - PROGRAMMING (E02)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Hey Asbjørn!

As someone brand new and trying to learn to code, I love you videos! One question. Visual studio doesn't seem to auto-complete unity code with intelli-sense for me. How did you get it to do that?

👍︎︎ 2 👤︎︎ u/Kagetakai 📅︎︎ Feb 01 2017 🗫︎ replies
Captions
today we'll get started programming in unity I did a poll asking what language you guys would like to see and the results were well conclusive so today we'll be creating our first script we'll have a look at how you can change properties and even add forces to an object so I'm really excited let's just jump right in turn so whenever we want to program in unity we create what is called a script to do this we click on the object that we wanted to sit on in our case the player and we hit add component then we go down here select new script and we can give a name to our script in our case it's going to be something like player movement but you can really choose anything you'd like and we select the language in our case that's going to be C sharp so it create an ad and you can see that unity adds a new component and that's because scripts and unity are pretty much just custom components that we get to tell what to do it's also created a script file down here in the project panel and that means that we can add it to other objects as well so if we now select our ground we can drag our player movement onto that in order to make it affect the ground as well but we don't want to do that so that's just right click on it and hit remove component so if we go back to a player and double click on our player movement script it's going to open up in your script editor if you're on Windows that's either going to be visual studio which is what I'm using or monodevelop which is also what you're going to be using on the Mac it's really up to personal preference and you're going to be able to follow along just fine with either so this is what a script looks like just when we created that's a lot of scary stuff going on here the thing about learning to program is that at first you're just going to have to ignore most of the stuff that's going on to clean it up a little bit we can go ahead and delete the tooth using tags up here and really the only thing that you should focus on right now is these two functions where it says void start and void update and these are created by unity automatically so let's have a look at the start function up here the neat thing about the start function is that it will run when your game starts that means that whatever code you put between this curly bracket and this curly brackets that means in here is going to be executed when you start the game do note that in c-sharp it doesn't matter how many spaces you have you can really space it out any way you want you can even put it on separate lines as long as you don't split up a void like this let's just try and see if this is working by displaying a message to ourselves to do that we write debug the log and make sure to have capital letters the same places that I do because c-sharp is cap sensitive then we open up a parenthesis and you can see that it automatically closes one as well then in quotation marks we get to write a message in our case we'll just write something like hello world make sure you end this line with a semicolon you're going to be doing that a lot whenever you write a command you're going to end it with a semicolon if we now save this by hitting ctrl s you can see that it saves up here hit back into unity and hit play nothing is really going to happen in our game view however if we go ahead and look at the console here you can see that it now says hello world if you don't already have the console open you can go window and then console so the console here is used by us to debug our code it allows us to write messages by using debug deadlock followed by our message and it also displays warnings and errors about the code to rewrite so we really have a love-hate relationship to the console if something isn't working in your game make sure to check the console and see if there's any errors of warnings they might help you troubleshoot the problem all right so writing hello world in the console is a bit boring let's have a look at what else we can do well in fact we can do pretty much anything in unity we can modify values we can add forces to objects we can add explosions create game timers and menus the possibilities are pretty much endless and that's also kind of what makes programming scary is that you have a lot of functionality available to you but let's try and focus on a specific problem so let's say that we want to go into a rigidbody and disable gravity for that object to do that we need first of all to reference this rigidbody component and unity makes that really easy if we go into our script and go above our functions on this empty space here create some new lines and then write public then the name of a component our component is called rigidbody then the name of the component that we want to modify in our case rigidbody and now we need to give this reference a name that will use internally so we can call it something like RB for rigidbody if we save that and hit back into unity we should now see an empty slot here with the name of RB we can then take our rigidbody and drag it into that slot so now whenever we write RB in our script it's going to refer to this particular rigid body that means that if we go back into visual studio and instead of writing debug that lock here write something like our B dot used we can set that equal to either true or false in our case we're going to set it to false and then of course end with a semicolon so say that that's it in a unity and let's see if this value isn't going to change when we have play so let's hit play and indeed the use gravity changes to false we can see that our cube here is hovering in the air and if we were to duplicate him so that the two would collide you can see just how cool of an effect that already gives us but this is just editing a tiny little property we can also make our rigidbody do different things if we quit playing go back into visual studio we can actually add a force to that rigidbody so instead of writing our BTUs gravity let's write RB add force and again we open a parenthesis close the parentheses and then end with the semicolon however you can currently see that this is displaying red and the reason why is that we need to specify a few things these are called function arguments the add force method wants to know how much force it should add in the different directions so we could start with 0 on the x-axis we could do something like 200 on the Y and maybe 500 on the Z notice how I separate each number with a comma we can then hit save go back into unity and when we now play we should see a force being applied to this object awesome so let's stop playing head back into visual studio and what we want to do is not only add a force in the beginning of the game we want to add a constant force in order to move our object forward to do that we can't use the start method remember this is only called once right when we start the game however our update function is called once per frame that means that whenever the computer draws a new image and it does this multiple times a second this function is going to run so let's again put this on a new line to make things a bit easier to see and we can then write RB dot add force just like we did up here we can input 0 on the x 0 on the y let's try 200 on the Z again remember to close it with a semicolon it's also take our entire start function up here and remove it so now we hit save go back into unity hit play and we should see our cube just launching into the distance and indeed we do however even though our game currently runs just fine and we get no errors in the console we do actually have an error in our code if we stop playing head back into visual studio we can see that everything looks just fine but that's one and that is if this method is called once per frame and we add a fixed amount of force each frame how much force we add over a second is actually going to totally depend on our frame rate that means if you have a really good computer that is drawing a bunch of frames a second the cube is going to travel faster than on a slower computer that doesn't run as many frames per second and the frame rate can even vary on the same computer if a bunch of stuff is going on on the screen the computer usually slows down a bit in order to handle everything or if something is happening in the background that might also change your frame rate so in order to even out these differences what we do is we multiply this value the 200 here with what is called time.deltatime this is a bit confusing but basically what time delta time is is the amount of seconds since the computer drew the last frame so if the update is running 10 times per second this value is going to be 0.1 and if it's running 20 times a second this value is going to be point zero five so the higher the frame rate the lower this value is going to get and therefore it's going to even out the advantage that you would get if you were running on a good computer if you don't get this right away that's totally understandable I sure didn't the first time just right after me for now so if we now save this and go back into unity we should actually see a pretty big difference and that is our cube is not moving now at all and that's because in my system it's drawing a lot of frames and so overall the value gets a lot smaller than it was before the workaround for this is super simple we just need to go into our script and make this value bigger I'm just gonna add another zero save that going to unity hit play and we should see him just storming off here it doesn't look like much of a difference but now we could at least know that it looks the same way on all systems the final thing we're going to do is yet another one of those it doesn't look like it's wrong but it's actually a little wrong kind of things and that is that unity really likes it if we use what is called fixed update instead of update for calculating physics and in our case we are using the unity physics system so just make sure that whenever you do physics stuff if you're adding forces or changing velocity do it inside the fixedupdate unity likes that a lot better and it's going to make everything look a lot smoother when you collide with stuff also if you find all this confusing I totally get it one thing that you can do is use the fact that we can make comments inside of our code so you can see this line here is grayed out that's because it's marked with these two slashes whenever you create two slashes and follow it up with some text that text is not going to be in fluted in your code and so it allows you to save stuff like this so just go through your code and add some comments to help you out you can even add comments at the end of a line also see a lot of beginners using spaces to indent that code I find it a lot easier to just use tap but it's something programmers have argued about for decades so let's just save our code a final time and enjoy the result until the next video you can of course play around with this by adding more objects into the game that's add another cube here for example and this already has a box Collider so it should actually collide let's pretty much it for this video I hope you liked it if you did make sure to subscribe so you don't miss out on future episodes I upload on Sundays and Wednesdays also if you're fan of the series you can always support me over at patreon patreon allows you to donate a monthly amount of your choosing totally cancelable at any time it's really awesome so if you want to see more content like this or just buy me a cup of coffee you're very welcome over at patreon.com slash practice also if you found this programming episode really really difficult you can definitely check out the C sharp beginner course it really focuses on teaching the c-sharp language so thanks for watching and I will see you in the next video thanks to of the awesome people who donated in January and especially thanks to Derek heaps Kirk Chaisson Merrifield James Calhoun Robert Barnum Peter Locke and Jason dirty doh if you want to become a patron yourself you can do so aperture intercom / prayer keys thanks a lot guys
Info
Channel: Brackeys
Views: 3,173,330
Rating: undefined out of 5
Keywords: brackeys, unity, unity3d, beginner, easy, how, to, learn, course, series, tutorial, tutorials, game, development, develop, games, programming, coding, basic, basics, C#, script, force, add, addforce, movement, player, moving, start, update, intro, introduction, get started
Id: 9ZEu_I-ido4
Channel Id: undefined
Length: 10min 11sec (611 seconds)
Published: Wed Feb 01 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.