2D Movement in Unity (Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
one of the most essential parts of creating a 2-d game is of course player movement and even though it's one of those basic mechanics that we often take for granted creating a really good player controller can actually be quite challenging in fact it's something that even experienced programmers can spend weeks or even months of development time getting to feel just right of course everything depends on what you want your character to do can you jump can you be controlled in air can you crouch can he wall run does he slide down ledges what about animation and so on now that might sound a bit overwhelming but as always with game development there are ways that we can cheat a little bit if you're just starting out I really recommend finding a good character controller online simply because you don't want to be stuck doing movement when there are so many other aspects of game development that you should explore also this video is sponsored by skill share skill shares an online learning community with more than 21,000 classes on game development tech and more and since this is a 2d video I thought I would introduce you guys to an awesome course on creating a boned faced 2d character in unity this beginner-friendly course is made by game developer vladimir lomachenko and it comes to three downloadable characters to help you get started a premium Skillshare membership gives you unlimited access to all classes for less than $10 a month so to get started simply click the link in the description and the first 500 people will receive their first two months for free now a character controller is basically just a script that takes care of all the more advanced physics calculations that go into making your character move so by using one of these we can focus on writing a much simpler script that will just tell the character controller what to do for the purpose of this video I've of course gone ahead and prepared a character controller that we can use as a base but enough talking about it let's give our player some moves yeah so we're here in unity and I've gone ahead and set up a simple 2d scene using this Sunnyland asset from the unity asset store of course have a link for that in the description there's not much in here there's currently just a player which has nothing on him other than a sprite renderer as well as a main camera of course some different tiles making up our level and I've gone ahead and placed colliders on the platforms as well as the ground and then we have a few background elements so since we're going to be using a character controller to help us with our movement of course first have to get it into unity - right here on the desktop of course have a link to where you can download the script as well as the entire project so I'm just gonna take this and drag it into unity and right away that select our player and drag a script on there if we have a look at the different variables that are in a script we can see that we can adjust a bunch of different things that has to do with our character first of all we have the jump force I'm just gonna set this to a value of 700 we then have the Crouch speed which we can adjust using a slider here and this is basically how much we want to slow down when crouching I'm just gonna set this to something like point four and so we're gonna be moving at 40% speed while we're crouching we can also adjust how much we want to smooth out our movement I'm just going to leave that as is whether or not we want to be able to control the character in air which I definitely do want to and then we have to define this what is ground variable and this basically just allows us to choose what layers we want to act as a walkable surface for our character I'm just gonna set this to everything and then go back in and remove the player layer because we don't want our player to be able to walk on top of himself I'm also going to go to the top and change this layer to player and then we have these three empty slots the first two of what we call checks and we use these to check whether or not we're currently standing on the ground so that we can determine whether or not we can jump and if we're crouching we want to know if there's a ceiling above our heads in order to determine if we can actually stand up again and to make things simple we're using these empty objects to determine where these locations are let's go ahead and create these objects that's right click on the player and let's hit create empty and I'm gonna switch to the move tool here by pressing W and now we can see where the empty object is I think this is pretty close to our ceiling check but I'm just going to go ahead and move it up a tiny bit something around here I'm then gonna duplicate it by pressing ctrl D and move this one down to the feet of our character I'm gonna rename this one to ground check and the other one to ceiling check just to stay organized we can then select our player and drag our ground check into the ground check slot and the same thing for this earring check the last variable here the Crouch disable Collider we'll talk about later but a character controller is now set up and should work just fine however there are a few more components that are required in order to make our player work the first one of these is of course a rigidbody because this will enable physics for our character so that's it a component and let's search for rigidbody 2d we can then open up this component and the first thing that we want to do in here is probably bump up our gravity scale in general it feels nicer if your character has a larger gravity than all the objects it makes stuff like jumping feel more snappy I'm gonna set mine to 3 but you can of course experiment we also don't want our character to be able to rotate at all we simply want him to move around the level but always have him facing upwards because of this we can go under constraints and freeze the rotation on the set axis and now he should stay the same at all times finally we need to go in here and add a Collider for a player because right now if we play the game we can see that he simply just falls down but it can actually be a really good idea to use multiple colliders when you're working with a player this can help smooth out collisions or make them feel more realistic one fairly simple setup that I really like is using two colliders it's circle Collider and a box Collider let's start by creating the box Collider so we're going here and add a box Collider 2d let's open it up and we should now be able to see it however the background makes it pretty hard to see so I'm just gonna go under my main camera and disable the background for a second and now when we select the player we can clearly see the green bounding box I'm gonna hit F to focus on a player and then gonna press edit Collider and we can now scale our Collider to fit our player one thing that I recommend is holding down alt to scale on both axes well the reason for this is that we don't want any offset on the x axis because since we are flipping the character that's gonna make the collider behave really weird so I'm just gonna bring this in and I'm also going to bring it down on the Y and finally actually want to completely remove this Collider from the feed of our character and that's because I found it works much better to use a circle Collider here so I'm gonna stop editing this Collider and add a circle Collider a small circle Collider 2d let's open this up as well and we see right now it isn't really positioned in a way that we would like so we can of course go in and offset it on the Y here as well as press did Collider and kind of bring it in and I'm gonna go ahead and move it up a tiny bit more and if you slide on a value like this in the inspector while holding down all you'll be able to fine tune much more neatly something like that looks pretty good I'm actually gonna adjust the radius to be just a tad larger and bring it up a tiny bit more and finally I'm gonna take the box Collider and bring it down a tiny bit and in whit's a tiny bit there we go let's stop editing the collider and we now have these two overlapping colliders and you can already kind of see what the benefit of this is because say we want to move up a slope like this one if we were only using a box Collider it would take the edge of this Collider and kind of push it towards the slope however since we're using a much more rounded shape a movement is going to feel much cleaner when moving up the slope and that's actually another reason which has to do with crouching but we'll see that later so let's move our player back up and if we now hit play we can see that immediately our player falls to the ground and he's ready to start moving so let's stop playing let's rename the background here let's select the player I'm just gonna close all these components and let's add our player movement script let's hit a component search for player movement select new script create an ad and it's double click it to open it up in visual studio and the script that we're gonna be creating here is fairly simple and the very first thing that we want to do is get a reference from a player movement to our character controller to D so that we can tell this script to move our character to do that at the very top of our script will create a public character controller 2d variable and we'll simply call it something like controller if we then save this and go into unity we can see that we now get this empty slot called controller where we can drag in our character controller script and now every time we write controller in our movement script it's referencing this particular script so if we were to go in here and write controller dot move well this would go ahead and move our character well of course we can't just tell it to move we need to specify where we want to move both in what direction and add what speed so to do that we need to get some input from the player and every time we get input from the player we use the input class so I'm gonna write input dot and then we're going to use the function called get access raw and the access that we want to get here is the horizontal axis now what this is is basically a value between minus 1 and 1 that is going to change depending on the user input if we press the left arrow key or the a key on our keyboard this is going to be minus 1 if you press the right arrow key or the D key on a keyboard this is going to be 1 and similarly if you're using a controller and press the thumbstick all the way to the left it's going to be minus 1 and to the right of course it's going to be 1 so this simply gives us a value that determines where we want to move on the horizontal axis to kind of show you this happening I'm just gonna use a symbol function called debug deadlock and this will print out a message to the console in other words we can simply display our input here inside the editor so if we just write out this piece of code here and save it and then hit play we can see that in the console here we get a bunch of messages saying 0 however if I then press my left arrow key we can see that it says minus 1 and if I press my right arrow key we can see that it says 1 so now we have a way of detecting input I'm gonna get rid of the debug deadlock here and we could use this input to move our character right here but it's not recommended to move a character inside of the update method instead we want to use another function that is dedicated for physics this function is called fixed update and it works in much the same way as update however instead of being called once every time the computer draws a frame on the screen it's called a fixed amount of times per second hence fixed update so let's just go ahead and create this function I'm gonna write void fixed update let's open and close two parentheses and some curly brackets and this is where we want to move our character so in general when it comes to movement we can say that we use the update method to get input from our player and we use the fixed update method to apply that input to our character we're not really using the start function here so I'm just gonna remove that but we of course need to get this input from one function to the other and to do that we need a variable so at the top here I'm gonna ride float let's create a very called something like horizontal move and by default we'll just set that to zero then inside of our update method we can set horizontal move equal to the result of our input and we'll close that off with the semicolon then when we want to actually move our character down here we can write controller dot move and this of course takes in a float determining how much we want to move and we want to move according to our horizontal move variable and let's then close that off with the semicolon however our move function actually also takes in two other variables the first one determines whether or not we should crouch and the second one determines whether or not we should jump right now we'll just say no to both of these so we'll write comma and then false saying that we don't want a crouch and comma false saying that we don't want to jump but we'll of course implement both of these in a sec also right now we're moving our character in the right direction but we don't have any control over the speed so let's just go to the top here and create another public float and we call this one the run speed and set it to something like 40 by default then when we get the input from our player we'll go ahead and multiply that input with a run speed so that if the player wants to move to the left the horizontal move will be minus 40 and to the right will be 40 and the final thing that we need to do in order for this to work correctly is whenever we're telling our character to move we actually need to multiply that movement speed with time fixed Delta time now time that fixed Delta time is the amount of time that elapsed since the last time this function was called in other words multiplying with time that fixed Delta time is going to ensure that we move the same amount no matter how often this function is called so that if this function is called 60 or a hundred times per second doesn't matter our character speed is still going to be consistent across all systems and platforms and now this should work so let's go ahead and save this and inside unity we can now see our run speed which we can adjust here if we want you let's hit play and we can now move our character back and forth inside of our level you'll also notice that every time our player Falls he kind of shifts into the ground a little bit to get rid of this we can go under rigidbody and change the collision detection for discreet - continuous and this should make sure that he never kind of goes into the ground in any way so now we're ready to make our character jump and this is actually really easy compared to normal movement all we have to do is check whether or not we press the jump key so we'll write if input dot get button-down so if we press down a certain button and the button that we want to check for is jump well then we want to go ahead and jump and of course again we want to jump inside of a fixed update functions so we have to kind of transfer this information down here and again the best way to do that is using a variable so up here will now create a boolean and a boolean can of course only be true or false and we'll call this one jump and we'll set it to false by default and then if we press the jump button we're gonna set jump to true and then down in our fixedupdate instead of saying always false we never want to jump we can now just pass in our jump variable so that if we press this button and jump becomes true we go ahead and tell the controller that we want to jump and of course right after that we want jump to become false again so that we don't keep on jumping forever and if we say that anko into unity and hit play we should now be able to move around and whenever we press space voila our character actually starts jumping super cool also there's currently a quick problem that we need to fix that is whenever we try and jump and then move towards a platform we can kind of get stuck on it of course this is not something that we would like we want to simply slide off and then fall down to get rid of this issue is luckily super easy all we need to do is create a physics material to do that we'll go to our project and hit create and will then select physics material 2d and we want to create a really slippery material so that we will simply slide off so let's just call it slippery and in here we can adjust to settings the friction and the bounciness we want the bouncing s to remain at zero and we'll also set the friction to zero as well then if we used to vector player we take this the pre physics material and drag it into the material on a box Collider and now when we play the game we can see that we slide right off platforms we can still get stuck on the very corner of this circle if this happens on earth is going to depend on the way you've set up your colliders in our case we can simply go ahead and add the slippery material to there as well and you can now see that we fold right off of platforms and at this point some of you might be wondering well we just wrote jump why does this jump variable correlate to space and why does horizontal mean a and D and left and right well this is actually something that we can configure visually because inside of unity if we go to edit project settings input we see the input manager here we have the horizontal axis you can see it's called horizontal here and we can see the different buttons here that is left and right arrow key as well as the a and D key so you can go in here and change it if you want to and there's also one in here called jump which currently has space as the positive button if you wanted to jump on say W we could simply write W here instead in fact let's try right clicking on this jump entry here and hit duplicate array element this is gonna go ahead and create a new button configuration with the same settings let's rename this one to crouch and instead of W we'll use the S key of course if people want to control the character using the arrow keys we can set that up as well we'll just create an alternative positive button so here it will be up for jumping and here it will be down for crouching so now we've set up all of our input and we're ready to make our character crouch and doing this is almost as simple as jumping we go into our update we write if input get button down and the button that we want to look for now is the Crouch that we just created and if this button is pressed well then we want to go ahead and crouch so let's create a variable flat up here it's also gonna be a pole let's call it crouch and let's set it to false by default then if we get the button down we'll set crouch to true and now down in our fixed update instead of just passing in false we'll put in our Crouch valve of course crouching doesn't work in the same way as jumping we don't want to just quickly crouch and then stop instead we want to stop crouching when we release the Crouch button in other words we only want to crouch while holding down Crouch luckily we can also register whenever we release a button to do this let's go to the update function and that's right else if if we don't get a button down we might get a button up so we'll go in put get button up and again the button we're talking about here is the Crouch button and if we get a button up here we'll set Crouch to false so now if we save this and go into unity and hit play we should actually be able to press down in order to crouch however you will really not see anything happen here the reason why is that crouching is of course something that we display with an animation and we don't have any animations on our character however we should see that if we move and then try and crouch and move we move at a much slower pace so it is indeed working we can go between crouching and normal movement and that works just fine however one thing that we also need to have been when crouching is of course the ability to duck under your obstacles for example this platform here if I currently select the player and we make sure that this mouse is enabled at the top here of our game view let's open up our circle and box colliders we can actually see them here if you then try and move onto this platform we can see that we're currently too tall to get under it that's because of this box Collider right here what I've gone ahead and done is under the character controller 2d I've added this Crouch disabled Collider so that if you set up your kaleidos in the same way that I have we can simply take the box Collider and drag it in there and now whenever we crouch the box Collider is going to be disabled and so we're only going to be using this circle Collider in fact we can kind of see this happening if I now press Crouch we can see that the Box disappears and if I try and Crouch my way under the platform it works we can also see that if I try and stop crouching so now I'm gonna release the button while under the platform it's not going to work and that's because we've set up our ceiling check correctly so it's registering that there's something above us and so we don't want to be able to stand up until we get to a point here whether it's no longer something above us and so we'll stop crouching that's super cool of course this currently looks a bit silly since we don't have the Crouch animation but you can see that behind the scenes everything is working as it should and that pretty much includes this first every character controller from here it's completely up to you to try and expand upon it and edit different settings to get the controller feeling the way that you would like it to of course this character could really do with some animation an animation is something that's really fun and simple to add so if that's something that you guys want to see definitely leave a comment down below yeh that's pretty much it for this video if you enjoyed it make sure to subscribe and bring that notification bells so you don't miss the next one also if you haven't joined already make sure to check out the brackish discord channel it was created by some awesome guys from the forum and it's a great place to talk to other developers get feedback or just hang out you can join now by using the link in the description also make sure to check out Skillshare simply click the link in the description to get started on that thanks for watching and I will see you in the next video thanks of the awesome patreon supporters who donated in June and a special thanks to Michael Neely Clintonville Skua andrew kelly Nemco cheetah 3d Alexandra Blair Sybok mummy Jeff Paul sentence Sullivan Superman the great Faisal Merrifield Derek Eames Kirk thanks so long Leela sets Timnath hold up our patron a swinger James Rogers Rob friend Gregory Pierce Nokia vaisakhi Larry tweed The Rasmus joins CAD a breezy car Jackson Chris Wilczewski James P and Robert bond you guys Rock
Info
Channel: Brackeys
Views: 2,239,022
Rating: 4.8618569 out of 5
Keywords: brackeys, unity, unity3d, asset, assets, beginner, easy, how, to, howto, learn, course, series, tutorial, tutorials, game, development, develop, games, programming, coding, basic, basics, C#, move, movement, moving, character, controller, character controller, control, your, player
Id: dwcT-Dch0bA
Channel Id: undefined
Length: 21min 8sec (1268 seconds)
Published: Sun Jul 15 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.