3 ways to do a Ground Check in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to learn how to test if the player is grounded being grounded means the players touching the ground which is essential for a platformer in order to test if the player should be able to jump we're going to cover three ways of doing that test and in the end we won't choose which one is best let's begin [Music] hello and welcome I'm your code monkey and this channel is all about helping you will learn how to make your own games with enough tutorials made by a professional indie game developer so if you find the video helpful consider subscribing so here is my player character I can move them left and right and I have also implemented jump with the space key so when I press space I can indeed jump so this seems correct however if I spam the space key just like that all of a sudden I can find so we need to make sure that we can only jump when the character is actually touching the ground so let's see how the code is set up here is the player class it's free simple all we're doing is doing some input tests so here on the fixed update we are doing our hand on movement if the left arrow is now we set the velocity to move to the left if the right arrow moved to the right if none of them we stopped and here if we press space we got our jump velocity which modifies the velocity pointing up so what happens is every time we press space we are doing this which is gonna make him jump so it's in here that we need to check if the player is touching the ground before we do the actual jump so let's make our function to test if the player is grounded so down here we make a private ball since this will be a question every rank on this is grounded as I said we're going to test out three different methods so let's start off with the simplest one which will be just a simple ray cast now in a ray cast in case you don't know is just a line going from point A to point B and the physic system tests if that line hits any colliders so we do a physics 2d dot ray cast so as you can see we need an origin a direction and a distance so for the origin it will be the center of our collider so we go into our box Collider reference and we grab the bounds dot center of our box Collider okay for the direction we want to point down so a simple vector Q dot down and finally for the distance since the origin is at the center of our box Collider we can use half the height of the collider as our distance so we go into the boxing either going to the pounds and half the total height is exactly what is stored on the collider extents so we can just use the extents that Y and that's our total distance so we have a line going from the center pointing down by half the height which perfectly matches the bottom of the collider however being that mathematically perfect could cause some issues so let's add a little extra so in here a extra height test let's just put a very small amount just enough so that it tests right outside of the player coin so in here we test by that distance pause that okay all right so all of this might seem complex but it's really very simple all we're doing is a simple test from point A to point B where Point a is the center of our box Collider and point B is the very bottom now we need this for this function to return whether the player is grounded or not so we are grounded if the raycast hits something and we are not if the raycast hit nothing now we can visualize the recast with a simple debug to draw right this takes a start point and a direction so the start will be the origin of our race so let's use this the direction also contains the distance so we simply take vector T dot down and then we multiply by our distance and that's our red then we can also add a color so let's add different colors to test if we are grounded or not so in here we define a color for the gray color and if we hit something we want the right to be in green if not we want to be in red so to get the result of this raycast as you can see it returns a raycast set to D so recast hit to D and we know if we hit something if the raycast e dot Collider if it is different from known then that means we have hit something so if we hit something let's put the break color with color green so we see green if we are grounded and if not let's put the red color as color not rep and finally at the end from this function we return if we are grounded so we just do a return if the recasted Euler is different from not okay so again what we are doing here is doing a recast and then drawing an array to make sure that we see the result of our recast so that's left is to go up here into our jump code and we're only going to jump if we press the spacebar and our character is grounded okay let's test okay so here we are now first of all in order to be able to see our debug array we need to go up here and enable gizmos and there you go you can now see the rights going from the center of the character pointing straight down and going outside by just a tiny bit you can see that it's in green since our character is indeed grounded and if I jump and as you can see we have an error the color is still green which means we are so grounded so if I swam space we can still jump so this isn't working exactly as intended so let's see what's happening to identify the issue that's going to our is grounded function and down here let's do a debug log of the rake acid Collider so we can test to see what our raycast is hitting let's test and yep right here on the console you can already see exactly what the issue is the issues that the raycast is hitting the player box Collider instead of hitting the platform now we obviously want to ignore the player from the ground check so let's fix that one extra parameter we can use in our recast is the use of a layer mask which means we can test to make sure that our break has only hits on certain layers so let's use layers to identify which ones are our platforms so here in the editor is select a platform go in here to the layer and add a new layer now in here on my channel layer for the platforms then we go back into the transfer select all of the platforms and set them all to the platform's layer okay so now we have a layer that we want to collide with so now let's go back into our code and here we need to know the layer mask so we can go up here and use something very useful from unity first at a serialize field so we can set it the knee very private and now we can use a layer mask let's call this the platform layer mask so let's in the editor how that looks here is the player script and here is the platform layer mask and as you can see we can select from the various layers and in this case we're only going to some like the platform layer and you have there you go only down okay now back into the code so down here we can outclass the platform layer mask as our layer mask so that means that this rake has will only collide with objects there are on this layer so let's test again so here we are and indeed you can see on the console that we are not hitting the player but rather we are hitting the platform so now we can try to jump and yet there you go the Ray turn into red and as you can see the console suddenly turn into no since there is no hit while he's jumping ok awesome so far so good so this is the first method for testing if our character is Grum now we do have several potential issues with this method the first issue we have is with regards to slopes so here on the left side I have a soft platform and if I go to it and yep you can see that the is grounded function is now returning false the reason why is because the player is touching the ground but only on this corner of his box Collider so we can pause this son like the player so we can visually see the box in leather and yep there you go so you can see that the box Collider is hitting right here on the corner but our rake house is not going far enough in order to actually collide with the platform since we're doing the raycast in the center we aren't doing enough extra distance in order to account for the slope so one solution to this is the simple increase the extra distance check so if you know the maximum slope this would be a potential fix so in here let's put the extra high test and let's say at 5 let's test again and as you can see the array is now much longer and now if we go into the slope we can indeed correctly identified that we are still theoretically grounded so this is one potential solution however let's see how we still have one issue over here on the right is an edge so we can jump into it go into the edge and I can still jump up here personally find everything worse I can spam and only jump when I'm down there okay however if I stand right on the edge there you go there's a red way again and we are no longer grounded despite the fact that is you can see the buxom weather is indeed correctly interacting with the platform so the issue once again is due to limitations of doing just one raycast which since the raycast is in the center it is not accounting for when we are only holding on from the edge so this brings us into our second method for the technical ground which is the Box cast here in the code the Box cast is extremely simple to change in the physics 2d class we have the array cast which tests with array and we also have a very simple box cast which tests with a box essentially imagine a virtual box Collider being moved in a certain direction so here the first parameter is exactly the same which is our origin however then instead of having direction we have the size so for the size of our box we can simply go into the box collide TD that bounce dot size then we need an angle which in this case we don't want the testing box to be rotated around so we don't even add zero then we need a direction which is indeed the same vector to point down and now for the distance since we are moving a box we don't need a large enough distance to account for slopes so we can simply go by distance of the extra height test and finally we have the layer mask which is the same as the platform layer mask so now the extra height s can be relatively small okay so that's it for switching from a recast into a podcast now down here in order to see what our code is doing instead of array we need for rays to draw the box okay so here we have our three raised only three since we don't actually care about the tongue right so let's do our test and see what things look like okay here we are and as you can see this is our virtual box it is correctly hitting the floor and if I jump there we go it turns into red and it plays the animation so if I spam it doesn't work okay great here we can go into the slope and as you can see works perfectly fine since we are using a ray cast so we are correctly hitting right at the corner there so the slopes work and finally let's go and see the problem that we had with the edge so all the way down here and if we go if we stand right on the edge there you go everything still works we can still jump even though we aren't right on the edge so as you can see this is working in every possible scenario so this method is the one I would suggest you use for making your own platform however just for educational purposes I won't cover one last method and this last method might seem like the best way for beginners since it's more visual but if you made it this far in the video I'm hoping you fully understand that the box aligner is without without the superior method so with the last method we're going to essentially just use a second hitbox so here we have our player character and inside let's make another empty game object this will be our ground check in here we had a box Collider and it is pretty much the same except shifted down so as you can see one of them the one on top is the player box Collider and the bottom one is the ground check in here let's make this a trigger so it doesn't collide with our physics okay now let's make a script for it so we go in here create a new C sharp script this won't be our ground check let's drag it onto our ground check game object now here in the code we can access the collision functions so we go into a private void on trigger state Udine so this function gets called whenever the collider is hitting something so in here we just need to do a simple test so we grab a boolean colic is grounded and it simply becomes true if the collider is not known so in here we do a public ball is round however the code just like this will have the same issue of colliding with the player so in here we also make a civilized film for the private layer mask for the platform layer mask so if the collider is not known and the collider matches the platform layer mask then we're going to be grounded and finally we also need a private void on trigger exit to dien set the is rounded back into false okay that should do it and here on the is grounded function for time being let's count out this code and instead we're going to grab the ground check component from channel so we do transform that point our ground check transform then we get the component for the ground check and we simply return the is grounded boom so return that ok so this is our third method and let's see if it works ok so my team here we are and if I jump there you go the jump still works perfectly fine and I cannot spam ok good if I go into the side let's see the slopes and you the slope is working perfectly fine I can jump and I stop ok and finally for the edge if we go all the way down here down to the edge and everything still works fine so the reason why beginners might do this method is because it seems simple since you can clearly see the first and the second box on whether and the code is pretty much only a few lines whereas the box cascode seems somewhat more complicated but I hope this video clearly explained how recasts work and how the bus caste works so you should be able to fully understand the box cast and why it is the Superion method so just like now we have three methods for testing if the player is grounded this is essential for any platformer in order to test if the player should be able to jump this video covered three methods but for most cases you should stick with e-box casma as always you can download the project files in utilities from E&T code monkey comm if you don't like the video subscribe to the channel for more ent tutorials post any questions you have in the comments and I'll do my best answer alright see you next time [Music] you
Info
Channel: Code Monkey
Views: 127,331
Rating: undefined out of 5
Keywords: unity ground check, unity ground check 2d, unity jump ground check, unity 2d platformer ground check, unity platformer ground check, unity 2d jump ground check, unity ground detection, unity check ground, unity boxcast, unity raycast, unity boxcast 2d, unity ground tutorial, code monkey, brackeys, unity tutorial, unity game tutorial, unity tutorial for beginners, unity 2d tutorial, unity 3d, unity 3d tutorials, unity tutorial 2d, unity2d, unity3d, unity
Id: c3iEl5AwUF8
Channel Id: undefined
Length: 15min 57sec (957 seconds)
Published: Wed Sep 11 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.