Movement, Jump, and Animation - 2D Platformer Player Controller - Part 2 [Unity 2018.3.10f]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay now we can start setting up the player so let's start by closing the towel palette and then zooming in on our scene choose somewhere where we want to put the character and let's just go to our idle animation spreadsheet and it's just just drag in one of these sprites okay as you can see it created a game object here on the Left I'm just gonna rename this to player and as you can see we currently can't see him that's because he's being rendered behind the background so let's go to sorting layer and set this to player there we go now we can see him okay so in order to get the player moving we need to attach a rich body 2d component to him and then you'll see if we play the game now the character will fall so the camera is all the way down here for now let's just bring that up but the character isn't colliding with the map yet to do that what we need to do is put a Collider on the character so let's say add component and we'll search for box Collider 2d and we'll attach this this is way too big for us though so we're just gonna edit it a bit and just bring it in nice and close to the character or one collisions to happen I actually have some numbers written down just to make this a bit more exact so we're going to change the size to 0.6 on the X and 126 on the Y and the offset is going to be 0 on the X and negative 0.19 on the Y perfect the character still won't be colliding with the map yet because there are platforms themselves don't have any colliders on that yet so let's go back to our platforms game object let's add a component and there is a Collider 2d and as you can see all the blocks now have box colliders on them but just to make this a little bit better we're also going to add a composite Collider 2d that's going to make make it just one big Collider instead of lots of small individual colliders to get this working we need to come to the town map collider 2d and say used by composite as you can see now I just have one Collider outlining all of the platforms perfect now we just need to make sure that on the platform rigidbody 2d that's automatically added when we add at the time of Collider 2d we need to set the body type from dynamic static so that when we play the game the platform's themselves aren't affected by gravity and the platform's won't go falling with the player so if we play this now we'll see that the character can stand on on the map perfect now let's get the player moving let's go to our assets folder and create a new folder and we'll call this scripts this is where we're going to solo store all the scripts that we write and then in here just create a new C sharp script I'm gonna call this player polar let's click on our player game object and let's just drag the script on to it underneath the renderer renderer we can double click over here to open up the script in Visual Studio okay so we got the character moving by attaching the rigidbody 2d component to it so in order to move the character in script we're gonna have to get a reference to this rigid body so we come back to the script over here let's start off by declaring a private rigidbody 2d RB and then in start we're going to say RB you get component of type rigidbody2d so what this is going to do is we're going to store a reference to the rigidbody on the player and then start this is just going to go look for the first component on our current game object that has the type rigidbody 2d perfect now we need to detect whether the player is giving any input to the character so to do this I'm going to declare a private float and I'm going to call this movement movement input direction in this we're going to store what direction the player is currently trying to move in now to check the input from the player we're going to write another function and we're gonna call this private void check input just like that so this function is going to get called to check for any kind of input that we might be expecting from the player so for now the only input we're going to have is whether or not the player is pressing a or D in order to do this we're going to say movement input directions Direction equals input dot get axis raw and the name of the axis is horizontal oh I can type what this does is horizontal is currently reference to the a and D keys so if I'm holding down a this is going to return negative one and if I hold in D it's going to return 1 if I were to just say get access without saying get access raw it would return a number between 0 and negative 1 and 0 and 1 and like the longer you hold it the higher the number goes and it becomes 1 but for us we want quick snappy movements so we're just gonna use get access raw now we need to do next is based on this input a plot of apply the movement to the character and for this we're going to write a another function first we have to come up to update then we just call check input here now we're going to write another function and we're gonna call this private void apply movement and this is also gonna get called from update but not from this update it's going to get called from Boyd fixed update here we're gonna call apply movement just like that ok so now in order to apply the movement all we're going to do is directly set the velocity of the character's rigid body so we do this by saying R be using that reference dot velocity equals new vector2 and now we have to specify the velocity in here so let's really go back up here to our variables we need to declare another variable of type float but this time it's going to be public and this is going to hold the speed of which at which the character moves so we'll just say we'll call this movement speed and motorists by default set it to something like 10 come back down here to where are beat up velocity and on the x-axis will say movement speed x movement input direction and then we're just gonna keep the Y velocity whatever it was so we've x moving in two directions so we can move left and right so if we save this and go back to our project it would help if I did this right that has to be a capital so now if we go back here and play will see if we push Andi the character moves as you can see the character fell over though so let's stop this in order to stop this we never want the character rotating so we'll come over here to the rigidbody open up the constraints and say freeze rotations and other character will no longer rotate when we play now we want the character to face the direction in which it's moving this is very simple to do let's start off by declaring a private boolean that's going to distort whether or not the character is facing right or not so we'll just call it is facing the right and the character is always gonna spawn facing right so we'll initialize this to true by default now we need to create a function that is going to check whether or not the character should be flipped okay so let's just say private void check movement direction and this function is going to get cold from update' and all this function is going to do is it's going to check whether or not the player is facing right and moving right and if it's if if it's not the same it's going to flip the character so it's very simple to do we're going to say yeah it's facing right and movement input Direction is less than zero we want to flip the character so if the character is facing right and the user input is negative or meaning is trying to move left the character should be flipped else if is not facing right and movement interaction is greater than zero we want to flip the character again oh not gonna flip the character and now we're just need to go and write this flip function which is also going to be private void flip and this function is super simple all we need to do is say it's facing right equals not it's facing right so if it's true to midnight or it's gonna be false and if it's false it's gonna be true and they've actually rotate the character which is just transform rotate zero on the X 180 degrees on the on the Y and 0 on the set perfect so now if we go back to our game let me run it you should see we move right the character faces right and when we move left the character faces left perfect okay so now we can get the character jumping this is also very simple to do we just have to again check for user input and then set the velocity so in our handy check input function we're going to say if input dot get button-down it's the jump button we're going to call a function called jump okay now in order for the character to jump we're going to have to set the velocity and the y-axis to something so let's come back up here and let's declare another public float we're gonna call this jump force and by default I'm just going to set it to 16 now let's write the jump function so we're going to call this private void jump just like that when the jump function is cold we're just going to set the upwards velocity super simple so we'll say RB dot velocity equals new vector2 we're gonna keep the same X velocity and then the Y velocity we're going to set to jump force now if we go back to our game and run it we'll see you in the character jumps now the jump force is quite high they card he jumps really high but I set it to this because I want the character like I said I want that movement to be snappy and fast so we're gonna come to the rigidbody 2d I'm just gonna set the gravity scale to 4 so this way the characters gonna move up and down a little more and this jump is gonna feel a bit better that's more that's what I'd like to see ok perfect now that we've got the character jumping let's get the camera following the player so we can just move move around a bit better to do this we're going to use a unity component called set you can make your own custom camera script if you want but I find submission just be a very easy way to get a camera setup so to do this that's come to window package manager go to show preview packages why should I don't know if it's a preview package but we want Senna machine and just press install over there and we just wait for it to do its thing and then I'll show you how to use it okay now that Senna machine has been added to our project you'll see we have a submission button up here that we can press and we can create different types of cameras so let's start off by just creating a new empty game object and we're going to call this cameras should I get up here let's put the main camera under cameras click on cameras click on the center machine and say create 2d camera you can drag this into the cameras folder right into our cameras game object as well and I'm just gonna call this camera player play a camera cuz this is the camera it's going to be tracking our player as you can see our main camera has a new component now called cinema sheen brain if you come back to our player camera super simple to use all we need to do just to get working is drag our player game object into this follow thing over here and this will set it so that the camera tracks our player as you'll see when we run it now the camera follows the player perfect now just another little thing because we're using pixel graphics it's sometimes it looks a bit weird the pixels don't show up the way they should and there's there's lots of different reasons behind that that I don't understand yet completely I'm quite new to this but in order to use cinema sheen we can't use the pixel perfect camera component that the unity has as well so what we can do is if we come to lens we can set our off or orthographic size to 8 point four three seven five now this number was determined mathematically there's some formula that I found online that chooses the right orthographic size for the screen size that I want so depending on how many blocks you want to fit into your screen this number is gonna change and there's a formula that I'll post they'll put in the description that you can use to figure out how what the orthographic cameras I should be in order to get a decently pixel perfect camera working so if we play this now it looks nice okay okay so now we also just need to come to our main camera look at our submission brain and want to change update method to fixedupdate it just fixes some jitter that happens sometimes perfect now let's animate the character to do this go to project or main assets folder and let's create a new folder called animation inside the animations folder let's create two new folders one called animations and one called animation controllers let's go to our player game object add component and we'll add the animator component come to our animator can or animation controllers and we'll say create new animator controller and I'm just gonna call this player and I make a controller come back to our player game object and drag this animator controller or this player yeah play your animation controller on to the animator in the controller section now if we come up to our animator tab at the top if this is not open you should go to find it over by the window button animation animate or click on that to open it okay so come over here click on our player this is our animation window animator window for our player we can also open up our animation window here at the bottom which again if you don't have this it should also be by window and if we have our player selected we can create a new clip let's save this in our animations folder I'm gonna start off by creating the idle animation you're gonna drag this up here and now if we go to our sprites just collapse a lot of these find our idle animation select all these sprites and just drag them up over here I'm gonna check change the sample rate to 12 and I'm going to move all these keyframes to more apart so you'll see that the ival animation state was automatically created in the animator because we have two player selected and if we go to scene and we play this we can see our character doing these little idle animation perfect now let's go through and create the walk animation as well by just clicking on this drop down over here saying create a new clip call this walk let's first change the sample rate back to 12 open our walk sprites select all of them and drag them back in here for this one we don't have to move everything around as you can see there our player is walking if you go back to the animator you'll see that the walk animation state was created automatically as well now as you can see the idle animation is orange and the walk one is not that means idle is our default animation state indicated by the orange
Info
Channel: Bardent
Views: 44,169
Rating: undefined out of 5
Keywords: Unity., 2018, tutorial, player, 2D, platformer, 2018.3.10f, walljumping, wall sliding, jumping, Unity, Animation
Id: bZVqz_3_NmQ
Channel Id: undefined
Length: 20min 38sec (1238 seconds)
Published: Tue Apr 02 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.