2D Movement in Unity / 2021 (Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

That's great! Helpful Resource!

๐Ÿ‘๏ธŽ︎ 2 ๐Ÿ‘ค๏ธŽ︎ u/void1gaming ๐Ÿ“…๏ธŽ︎ Apr 03 2021 ๐Ÿ—ซ︎ replies

Good for beginners.

๐Ÿ‘๏ธŽ︎ 2 ๐Ÿ‘ค๏ธŽ︎ u/ArcaneDiwa ๐Ÿ“…๏ธŽ︎ Apr 03 2021 ๐Ÿ—ซ︎ replies
Captions
hello and welcome to my channel in this video i'm hoping to show you how to create a simple 2d platformer player controller script and in my scene i have already set up i have a player game object which i attached a rigidbody2d object to i made a special material called slippery bounce which if you look at it you can see the friction is just really low and a little bit of bounciness and is added to make this material all you have to do is right click and go to create and physics material 2d and then you can enter in the same values i have here to get the exact same result along with that i set the collision detection to continuous and i froze the z rotation under the constraints i also have a sprite with this test sprite i made and a collider object with a box collider so we don't fall through the floor hey everyone this is uh editor michael here and i have one more thing to add i have on my collider object for my player i have it tagged as player collider to do this you just have to go here to add tag click the plus button and you just can call it player collider then once that's created go back to your collider and set the tag for the ground i went ahead and created a simple tile map added a tilemap collider added a rigidbody2d set it to static added our slippery bounce material and added a composite collider 2d and checked the use by composite tile map collider 2d option now with all that set up all we have to do is select our player click add component and we can add a new player movement script and then i'm just double clicking it to open it in visual studio now that we have our script open i'm going to go ahead and remove all of the update and start functions we might need those later and we'll bring them back if we do the first thing we're going to want to add is a private rigidbody2d i'm just calling that rb and to make sure we get our rigidbody i am going to add the awake function and inside awake i'm just setting rb equal to get component of type rigidbody2d and this will get the rigidbody component that we have on our game object that has this script next thing i want to do is i will add a private boolean and i will call it can jump and i'm just going to set it equal to true by default then i have a couple lines where we're going to want to serialize the field go private float movement speed i'm just going to copy this line and i will rename this jumpspeed the reason i serialized the fields is because uh since they're private that means that other scripts can't access them but if we serialize the field then we can view them inside of unity another way to do this is to just make them public and then that way other scripts have access to them but i just do this to make it a little neater all right now i'm going to go ahead and add our update and inside of here i'm going to go ahead and add a function call called movement and we'll go ahead and create that and go private void movement and then i'm going to do all our coding for making the player move inside of this function which will be called every time update runs so first to get the player input i'm going to go ahead make an if statement and check if input dot get axis raw and we'll be checking a horizontal axis because it is a 2d platformer so i'm really only checking if the player is moving left and right and we'll check if that's greater than zero then i will change the rigidbody's velocity and set that equal to a new vector2 and in this vector 2 the x value will be the movement speed multiplied by time dot fix delta time and the y value will be just the y value of velocity at the moment the next thing i'll want to do is add an else if statement so else if input dot get x is raw of the horizontal axis is less than zero and we can just copy our line from before and just add a negative sign before the movement speed and then of course we'll want to go else and this is if no key is being pressed then we don't want our player to keep moving so we'll go actually we can just copy this line and set that equal to zero add an r and i believe that should be it for the horizontal movement we can go ahead and save what we have go back to our game and let's see what's a good value i'll try 150. i'm going to go ahead and run if pressing the right key i go right pressing the left key i go left and currently we do not have any jumping so let's go ahead and add that one other thing i wanted to touch on the reason i'm using get access raw here is because if you use just get axis then there is a slight delay between the time when you release the key and when the player stops moving but if you use get x's raw then for whatever reason uh when you let go of the key then the player immediately stops moving so now we're gonna go ahead and add jumping so in our update i'm just gonna go ahead and add a jump function call we can go ahead and create a jump function so i'm going private void jump so before we get started adding stuff to the jump function i'm going to define a few more things up top uh for this jumping uh i am going to use a raycast or a couple raycasts to determine whether or not we are actually on the ground so i'm going to add a few more variables up top to define that so i'm going to serialize the field private float call it ray length do that again serialize the field private blue ray position offset and what these two variables are going to do is uh the first one ray length is going to determine how long of a ray we should cast and ray position offset is going to be used to offset the three rays that we're going to use to determine if we are on the ground i'm going to also set up double vector 3 is here or the ray position so i'm just going to call it reposition center i'll copy that two more times to reposition left and right position right i'm going to define a recast hit 2d array and call that ground hits center and what we can do for that is also just copy and paste it and we can call this ground hits left ground hits right and then we can do another raycast hit 2d array but i'm making an array of of the arrays and just grouping them all together and calling it all raycast hits i'm setting that equal to a new recast hit 2d setting the length to three and what that will hold is just all of these lists so i can iterate through all of them in a later function so now that we have everything defined up here i can go back down to jump and go ahead and set up ray position center to be equal to transform position plus a new vector 3 with the zero in the x position the rate length i'm just multiplying that by 0.5 just so the ray moves up a little bit and then 0 or the z and i'm gonna go ahead and copy that line and paste it two more times then i can do left and right and what i want to do is change the x so it is minus the ray position offset and then for the right i am just going to enter the ray position offset but positive so that way those two rays are equidistant from the center ray and the reason i'm setting it up like this is because if your character is a little wider and they're standing on a ledge that center raycast could be over the ledge and even though your player is on the ground technically they wouldn't be able to jump because that ray won't be touching the ground so that's why i have these two other raycasts next up i'm going to want to go ahead and define our ground hits center and to do that i am going to set it equal to physics 2d dot ray cast all at ray position center negative vector 2 dot up so it's pointing down and i'm going to set the length to be the ray length and like we've done before we can copy that line and paste it two more times go ahead and call this ground hits left and ground hits right let's make sure we update our position to the reposition left and array position right next i'll need to combine all of these raycast hits into our double array for all raycast hits so just so i don't over comp complicate it for myself i'm just going to say all raycast hits at 0 will equal ground hits center then i can copy and paste that line two more times all right cast hits one and two be equal to the left and right then what i'm going to say is can jump is equal to a new function we'll have to create called ground check and for ground check i am going to pass all raycast hits and it will return a boolean determining whether or not we can jump so let's go ahead and make that ground check all right so i'll go ahead and make a private bool function called groundcheck and we are passing in a raycast it 2d array array and i will call that ground hits so first thing i will want to do is to loop through each object in ground hits so i am going for each raycast hit 2d array i'll call it hit list in ground hits and then we'll want to do another for loop so we can go for each raycast hit 2d hit in hit list so now we're actually looping through each time a rate hits something if hit dot collider is not equal to null and then inside that i'm going to do another if statement if hit dot collider dot tag is not equal to player collider then we'll return true if we get through all of the hits for all of the raycasts and they're not touching anything aside from the player collider then we will return false because that means they that the player is still in the air so now our grand check will return a bull either true or false telling us if we can jump and we're storing that in this boolean called can jump um one more thing i want to add to the jump function it's not entirely necessary but it might help you see what we're actually doing so i'll go ahead and write it out for you so i'm doing debug.drawray and i will just copy this part of our ground hits center and i'm going to multiply our negative vector 2 up by our ray length and i'm gonna color this red so we can see it and i'll just do this for the other two as well for our other two raycasts left and right and what this does is it'll show you in the game where we're drawing those rays so you can see what it's actually interacting with so if we want i'll just save this and we can go back to our game and for our ray length go ahead and make it one our offset i'll make it .45 jump speed um i'll make it 12.5 i think although we don't have any code to jump so that won't work right now but this will just show you how the raycasts are working so if we run our game and we click on gizmos you can see underneath the player we have these three rays and that shows you where we're checking for the ground so array length is actually pretty long goes down pretty far so i'll actually go ahead and make that like .33 i think is ok i'll go ahead and change that and now we can go back to our code and finish up our jumping mechanic so i actually want to go back to our movement function and add an if statement and we can add if input dot get key keycode dot space and can jump so this is checking to see if you hit the spacebar and you are able to jump based on what we did before with our groundcheck function then i am going to take our velocity make a new 2 i will set the x value to our x velocity and zero out our y that way if uh if we're falling then our y velocity gets cancelled out before we actually add some jump speed so next line we can do our b dot velocity equals new vector 2 and the x for the x value we will set it to rb.velocity.x comma jumpspeed and we should be able to save this and go and jump using our character by pressing the spacebar right i'm just going to turn our gizmos off for a second we can move left we can move right when we jump we go up and we come back down so something to note here is i actually have the gravity scale set to 2 so the player comes down a little further or faster and the reason i did that is because typically in games like uh super mario brothers you'll see it referenced uh mario actually falls faster than he jumps up and the worst thing you could have for your game is probably a floaty jump right now the jump is a little floaty so let's see we can play with these values and get it to a place where we actually like it so i'm going to set the gravity scale to 5 mass to 2 and a half i think the jump speed to 20 and that should give us a little more heft and we fall down a little more and the way we program this if you hold down the space key you actually jump you can see we're not sticking anything and this code could easily be modified to add some coyote time so like if you're in the air you can jump a little bit afterwards after you left the ground or you could add in a count system for how many jumps you want the player to be able to have before they don't have any more that's really up to you if you would like to see me go more in depth in this leave a comment but other than that you should now be all set up with your simple 2d platformer player controller thank you for watching and if you like this kind of video leave a like and a comment telling me so and feel free to subscribe
Info
Channel: MichaelsGameLab
Views: 6,531
Rating: undefined out of 5
Keywords: MichaelsGameLab, unity, unity3d, asset, beginner, easy, how, to, how to, howto, learn, course, series, tutorial, 2D, GameDev, unity jumping, unity2d, unity2d jumping, unity movement, game developement, tutorials, game, developement, develop, games, programming, coding, basics, how to jump unity, code, unity 3d, unity 2d, C#, c#, character, controller, character controller, control, your, player
Id: hauVevcZj6k
Channel Id: undefined
Length: 25min 4sec (1504 seconds)
Published: Fri Apr 02 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.