HOLD JUMP KEY TO JUMP HIGHER - 2D PLATFORMER CONTROLLER - UNITY TUTORIAL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back to black thorn prod I'm Noah now a few months ago I made a tutorial on how to create a simple platformer controller the player with this controller could jump even double and triple jump however the player didn't have any control over how high or low his character could actually jump and so in this video I'll show you how to make a jump that's depending on how long the player holds down space in midair look at our character jumping higher or lower this is very similar to how a jump works in a Mario game or Halle Knights for example here I'll hit the space key and almost immediately releases and so my character will do a quick little jump and here a hold down space for a long time and my character as a result will jump very high just before starting I wanted to give a big thanks to wisp for supporting me and my channel by a patreon awesome with that said let's get started so here I have an extremely basic scene set up with a platform and my Kubik player character via this platform controller working my player will need a 2d box Collider and a 2d rigidbody set to dynamic this way you will be affected by gravity and will once having jumped to fall back on the grounds also make sure the platform has a 2d box Collider or the carrot will fall right through it I'll create a new C sharp script called clear controller add it to my player and open it up so first of all I'll get my player moving left and right so using the left and right arrow keys to do so I'll need to grab a reference to my 2d rigidbody so we'll be using physics to moving around I'll call my a rigidbody 2d variable are B and set equal to the rigidbody component attached to my player I also make a public float speed variable they'll be used to measure how fast our character moves left and right okay right under this I'll make a private floor variable and call it move inputs you use this to detect whether the player is holding down the left or right arrow keys okay in the fixed update function which I remind you is used to manipulate all physics related aspects of your game I'll set move inputs equal to input Y axis rod horizontal ensure if you hold down the left arrow key move input will be equal to minus 1 and if you hold down the right arrow key moving to it will be equal to 1 next up I'll set my rigid bodies velocity in other words speed equal to a new vector 2 for the x-axis I'll use that move input value and multiply it with speed and I'll just leave the players Y position has is and so now my player can move left and right as you'll see if you jump into unity type in a value for speed and test the game notes that for a less snappy movement you can get rid of raw right here okay it's now time to get down to the meat of the video making the jump after by making a bull variable called is grounded we use this to detect whether the player is indeed on the ground if you then will let him jump if not he can't jump to the text this will basically be casting an invisible circle by the players feet and if that circle detects grounds and the player is due to jump so I'll make a public transform variable called feet pose and a public float variable called check radius in my update function I can now set is grounded equal to physics to the overlap circle and in the parentheses make the central position of this circle which in my case will be the player's feet and a radius so check radius also very important a layer mask this will get the invisible circle detecting some objects and ignore others for example we may very well not once the player being able to jump but if he is a knee deep in water or some sticky surface so I'll make a public layer mask variable called wise grounds and plug that in right there so to recap is grounded will only be equal to true if this invisible circle collides with some grounds and so if that is the case and my player hits the jump button which in my case will be the space key of course you can use every like the player should jump sol-type RB dot velocity and this time set to equal to vector to dot but multiplied with jump force which is a float variable I'll add up right here right now so let's test this out in unity I'll create a new empty game object called feet pose give it a little gizmos we can easily see it in the scene view and parented to my player making sure to place it nicely by his feet and then drag and dropping it inside of this empty slots no need to make the invisible circle huge I'll just give it a radius of 0.3 and then type out some value for jump force 10 for example I also crank up my character's gravity if we quickly fold back to the grounds and as a float like a balloon slowly downwards lastly make a new layer called grounds set the platform to that layer and make what is ground equal to this newly made ground layer this way all objects with this layer will be considered as places where the character can jump off and hitting play you'll see that my character can move left and right and jump or awesome of course if your jump doesn't feel right keep playing around with the jump force and gravity values now that that's working I'll make sure that my player can jump even higher if he holds down long enough so I'll make another if statement and this time I'm checking whether the players holding down the space key using impede or get key instead of impose get key down if so they'll copy/paste this same line of code right in here of course if we leave things like this the player can guess character jumping higher and higher ad infinitum so we need to put into place some timer that limits how high he can actually jump so I'll make two new flow variables one private called jump time counter the other public names jump time' and so in this second if statements I'll make another checking whether a jump time counter is greater than zero if it is then the player can keep going higher and higher and of course will gradually decrease the jump time counter value so that at some point in this if statements will no longer return true however length like this the player can hit space start falling and then in midair hit space again sort of double jump which is it what we want so I'll make another bull variable cult is jumping and only allow the player to keep going higher it is jumping is equal to true if the player stops holding down space then that means he is no longer jumping as well set is jumping equal to false I also set is jumping equal to false when the player runs out of juice in other words when jumped I'm counter is less or equal to zero and so now as the player could no longer jump fall a little and then do another jump in midair of course ml sex is jumping back equal to true when the player hits space and is on the grounds also make sure to reset jump time counter by setting it equal to the jump time value you will set in the inspector and there we go I can now head back into unity and decide how long I'll allow the player to hold down the space key to jump higher or eventually falling back down I feel like a value of zero point thirty five seconds is great again this is really up to you and how you want your game to feel and play so take your time tweaking these various settings until you're satisfied with the results lastly a very simple way to get your character of flipping left and right depending on the direction in which is moving by rotating them of 180 degrees along the y axis so if move input is greater than 0 that means the player is moving right so I'll leave his rotation with a value of 0 on all three axes if however a move input is less than 0 I'll set the Y value to 180 degrees and you'll see that works really smoothly some people did mention simply using the flipped option in the sprite renderer to do so which is great if your creature is made up of one single sprite so baby was animated using a bone based method meaning if he's made up of more than one sprites then using the sprite renderer slip option becomes a little more tricky and that marks the end of this platformer tutorial I hope it was helpful and that you didn't get completely lost along the way yeah if however you did definitely ask for help in the comment section down below with that said don't forget to hit the like and subscribe buttons it's a really appreciated and so encouraging also consider supporting me financially by a peach Joan like these top supporters even just a dollar is absolutely wonderful okay thanks so much for watching stay tunes [Music] [Applause] [Music]
Info
Channel: Blackthornprod
Views: 248,791
Rating: undefined out of 5
Keywords: gameDev, unity, tutorial, noaCalice, c#, art, animation, blackthornprod, blackthorn, how to, 2D, platformer tutorial, how to make a platformer controller, 2d platformer controller, HOLD JUMP KEY TO JUMP HIGHER - 2D PLATFORMER CONTROLLER - UNITY TUTORIAL, hold jump key to jump higher, mario jump, move left and right, rigidbody, 2D physics, game character, programming, coding, indie game dev, charged jump, high jump, double jump, easy, beginner, layer masks, platformer controller, jumping
Id: j111eKN8sJw
Channel Id: undefined
Length: 9min 0sec (540 seconds)
Published: Mon Jul 09 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.