2D DOUBLE / TRIPLE JUMP PLATFORMER CONTROLLER - EASY UNITY TUTORIAL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone I'm Noah and this is black thorn prod with another unity and c-sharp tutorial today we will dive headfirst into the world of 2d platforming by making a simple yet effective and highly useful 2d platformer playercontroller by the end of the video you will not only have a character that can move length and rights but also double triple even quadruple jump if you like what's more the character will not get stuck to walls in this quirky way with that said let's begin so I have a very simple unity scene setup with this little black cube that will act as our player and a couple platforms all these assets have no components for now except for a sprites render so first things first let's create a new C sharp script call its player controller and open up so obviously we'll want a public float speed variable that will dictate how fast our character moves around the environments I'll also want a public float variable called jump force which has the name implies will let us tweak how high our player can jump next up I'll create a privates float variable named move inputs we will use this variable to detect whether or not our player actually has the left or right keys pressed and move correspondingly lastly I'll creat a private variable of type rigidbody 2d I'll call RB and in our start function I'll set R be equal to get components rigidbody 2d this way we can tweak and use our players rigidbody of yet scripts let's now get our character moving left and right to do so I'll create my void fixedupdate function which I remind you is used to manage all physics related aspects of your game inside this function I'll set my move in put a variable we created earlier equal to impute see axis horizontal now note that this is a built-in unity input field which basically is the equivalent to holding down the left or right arrow keys so to recap if I press the right arrow key moves input will be equal to 1 and if I press the left arrow key move input will be equal to minus 1 also keep in mind that if you want your player to move in a more snappy and responsive way instead of simply typing get access type get access wrong of course this will knock our character moving we must now set our rigid bodies velocity in other words speed equal to move inputs multiplied by speed on the X and we don't want to influence the y axis of our rigid body with this line of codes so we will simply type RB dot velocity dot Y heading back into unity I will start by adding the player controller script to our player and type in some value for speed of course I also need to add a 2d rigidbody so our character can actually move under 2d Collider so he doesn't just fall through the platforms and talking about platforms those also need a box Collider with default settings all right hitting play you'll now see that our character can move left and right awesome however we would like our character to face the left when moving left and the right when moving right so instead of swapping sprites for our character when moving left and right which could of course get very time-consuming we will use the famous and very easy flipping technique so I'll begin by making a private blue variable called facing rights they'll set to true at default I'll then make a function called flip I'll want this function to first of all set facing right equal to not facing rights in other words if facing right is equal to false then when this line of code runs it will be equal to true and you guessed it if it's true then it will be equal to false I'll now make a vector3 variable called scalar they'll set equal to the player's local scale in other words the players XY and Z scale values I'll then take the x value of this scalar variable and multiply it with minus 1 lastly I'll set my players local scale equal to this scalar so to recap if my player has say an X scale of 4 then once this function runs he would have an X scale of minus 4 of course we must now call this function somewhere in our script so I'll make an if statement and check whether facing right is equal to false if it is and move input is greater than 0 which means we are moving rights then we know our character isn't facing the right direction and so we will simply flip him if however facing right is equal to true and move input is smaller than 0 then we obviously need to flip our character because he is moving left while facing right which doesn't make much sense if we head back into unity and press play you will see that the characters smoothly flips to face the direction in which he is moving awesome let's now get the jumping working I'll start by making a bunch of variables first of all I'll create a private bool variable called is grounded that will detect whether or not our character is standing on the ground to get our is grounded returning true or false we will basically create a small circle via scripts near the player's feet if that circle collides with anything then we know our player is standing on the ground and if not we know he is in midair so I'll make a public transfer variable called grounds check a public float variable called check radius and a public layer mask called what is grounds all right let now put all these variables to good use in my fixed update function I'll set is grounded equal to physics overlap circle and in the parentheses state a position for my circle saw what my circle to generates by the player's feet so at the ground checks position I then must state a radius for the circle so check radius and lastly I could put a layer mask in here which I will do with what is grounds in unity and I'll create an empty game object called ground check give it a gizmo so we can clearly see it in the scene view and place it next to the player's feet our circle will generate at this position so select the player and drag and drop ground check inside of that empty slots also type in some value for check radius I'll give my circular radius of 0.5 Hazrat it we will state what is ground with this layer mask I've already gone ahead and made a ground layer and set all my platforms to that ground layer so for me what is ground are all objects with the ground later all platforms are characters that don't have this ground layer will be ignored by our invisible circle note that this layer mask is optional but I recommend you use it since it can give you some nice control over what the player can jump off oh this is cool but I'm afraid that won't get our character jumping now has promised I want to give my player the chance of double even triple jumping so I'll make a public int variable called extra jumps in my update function I'll create and if statements that will check if the player hits the up arrow key which is the key I will use for jumping this if statement will only return true if I have pressed the up arrow key and if extra jumps is greater than 0 if those two conditions our met then we will set our rigid bodies velocity equal to vector to dot up multiplied by force now our player can jump we must also decrease of one extra jumps so a player doesn't have an unlimited amount of jumps and of course we also need to reset extra jumps when the player hits the grounds I'll check with an if statement if is grounded is equal to true if that's the case then I'll set extra jumps equal to say two because I only want my clear double jumping it will be better however to not hardcore this value so I'll change extra jumps into a private variable and make a new public variable called extra jump value in my start function I'll set extra job equal to extra jump value and replace my to buy extra jump value also however if the player has no extra jumps and presses the up arrow key then we want our player to once again jump but this time there's no need to decrease our extra jump variable however also make sure in the if statements that the player is grounded or he will be able to continuously jump if i zoom back into unity type in let's say 1 for extra jump give my jump force a value of 100 and then hits play you'll see that my character cannot only jump but also double jump if I then type in true for extra jump my character can of course triple jump and so on now note that you'll probably have to tweak jump force and gravity scale for your character's jump to feel rights alright once that's done you'll notice that despite the cool jumping and movement our player easily get stuck to platforms like this we easy fix to this is adding a 2d physics material to the player's rigidbody so I'll create a 2d physics material call it player mats and sets friction to 0 I'll then drag and drop it inside the rigid bodies empty material slots and press play now we have a working playable to the platformer controller all right I hope you enjoyed the video and we'll put this controller to good use by making your own 2d platformer 2d platformers are a favored john ray of mine so this video won't be the last on the topic with for example a charged jump tutorial coming up in the near future with that said don't forget to hit the like and subscribe buttons if you enjoyed the video also consider following me on Twitter where I post character designs on a regular basis you can also join the blackthorn prod discord server to connect with a small you growing a passionate game deaf community ok thanks so much for watching have a great day Cheers [Music]
Info
Channel: Blackthornprod
Views: 409,882
Rating: 4.9167771 out of 5
Keywords: blackthornprod, unity, noa calice, tutorial, game dev, platformer, 2D, player, character, platforming, jump, double, triple, physics, material, not stuck, how to, create, C#, indie game dev, make, code, programming, jumping, controller, isGrounded, flip, scale, spelunky, super meat boy, mario, hop, 2D DOUBLE / TRIPLE JUMP PLATFORMER CONTROLLER - EASY UNITY TUTORIAL, easy, beginner
Id: QGDeafTx5ug
Channel Id: undefined
Length: 11min 56sec (716 seconds)
Published: Mon Feb 26 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.