2 Ways to do a Ground Check | Unity 2D Platformer Tutorial #2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys more Blakey here and welcome back to another tutorial video today is part two in our Unity tutorial series and we're going to be focusing on a ground check a way to ensure a player can only jump once by detecting when the player is grounded we're going to look at two main ways this can be done and how you can do both the full commented scripts from today's videos are available from our patreon alongside a ton of other benefits which can be found via the link in the description now let's get started so the first method we are going to use is a box collider check where we check for collision between the player and the ground and the second using a method known as raycasting let's start with a collision check so following on from the last video we have our player here with a boss collider already around it we still have this Square platform here which we can just rename the platform and now if we select our play out we have our player movement script from the last video and we're also going to handle our ground check from here so let's double click to open this up in visual studio so what we're going to do I'm going to use a new function called on collision enter let's just type in void on collision enter 2D let's change this parameter to other now in here we can check for collision with other objects and we can check for what tags they have and things like that now we're going to give our ground platform a tag when we head back into the editor and then we can check for this in this script so what we're going to do we're going to check if other.gameobjects dot compare tag and then brackets and then quotation marks and inside these marks we can just type in ground with a capital G then we can use curly brackets to open this up and now we need to create Boolean which we can set to true and false depending on if we are grounded or not so at the top here so we don't need this to be public so we can just do ball grounded now in here we can just set grounded equals to true now back in the editor let's create that tag for our platform so let's select our platform here go to the top of our inspector and select tag press add tag and then we just type in ground with a capital G in the same way we did in the script and then back to our platform and add this tag so to test if our ball is actually changing we have an issue here because we can't actually see it in our script because we haven't made it a public variable but what we can do for the time being is go to the top of our inspector here and there's these three little dots let's select this and change our mode from normal to debug now you're going to see a bunch of extra floats and values there we don't really care about at all but if we scroll to our script here you can see our grounded ball here as well as a few other things that are private in our script we don't want to see any of these we only care about our grounded and this is a nice quick way of checking if our booleans are being changed or not so what we can do here I'm going to drag my player to the top of the screen so we have enough time to see if it starts off as false and then if we press play here with our player selected you can see grounded was false that whole time but then the minute we touched the ground it turns to true but the issue right now is if we jump you can see it actually stays to true because what we're doing here is we are setting our grounded to true that there is no way of setting it to false so it's permanently grounded so back in our script we're going to do the opposite of this function which is on collision exit so let's do void on collision exit 2D change this parameter to other and what we can do is just copy this paste it in here and set grounded to force so when we collide with the ground we set it to true and when we leave the ground we set ground into false and now if we select our player again and scroll to our script you can see it starts off at false he is then set to truth but if we jump you can see it actually goes to false again which is great and our ball is working but at the moment we can still double jump and triple jump and as many as we want and that's because we're not actually applying our grounded Boolean anywhere so back in the script what we can do here is where we check for our jump input let's just add if grounded as well so if we press the jump button and the player is grounded then we can add that false so now I can move around and I can jump but if I try and spam jump you can see I actually can't anymore and that is our grounded Boolean coming into play now this seems great but this method does come with its issues if we quickly set our inspector mode from debug to normal if I select my platform here and just copy and paste this and I'm going to move it up here and I'm going to rotate it by pressing e on my keyboard if I press planer I can jump on this platform and right now grounded is actually set to true and you can see if I spam jump here I am very slowly jumping obviously this is not what we want so one thing we can do to combat this is check the direction of the Collision that our player is hitting so we can do this by creating a new Vector free so we can do Vector three and we're going to call this normal because we want to access the normal of our collider and then we're going to set this to other so we're accessing the other game objects which is our platform then we're going to do get contact and we're going to get the first Contact that the player is receiving so that is going to be zero then we're going to access the normal of this contact point now that we have this Vector let's check if normal is equal to vector3 dot up and if that is the case then we can set our grounded equals to true so let's just copy and paste this remove it from here and move it into our condition here so if the normal Vector of our collider is facing upwards then grounded is equal to true so let's test this out so with this second platform here that is at a right angle we can test this but before we do let's go to our player and make sure on our rigid body that freeze rotation on the z-axis is enabled as we don't want our player to be able to rotate left and right so now if we test this you can see on our normal path form I can indeed jump and grounded is enabled but if I go to our wall here ground it is no longer being enabled so that fixes that issue so in your case on collision enter could work great but another issue we could face is if your game has slopes like this you may want your player to be able to go up this and currently because the normal of this collider is not directly upwards grounded is not being said to true meaning we cannot jump so let's remove this platform and now we're going to look into our second ground check method which is Ray casting now raycasting in unity is a physics method in which we can cast a ray from An Origin point in a direction at a certain length against all the colliders in the game and what we're going to do is cast something known as a box cast underneath our player to check for Collision now the benefits of raycasting is that it tends to be more accurate and you also have a little bit more control over what you're checking for so in our scripter what I'm actually going to do is select all of our on collision enter functions and from here I'm going to press Ctrl K and then Ctrl C what this is going to do is comment out all of our code and this means none of this is going to run so we're just removing this code without actually having to get rid of it I'll alternatively if you just want to use raycasting we can just delete it like this so if I just give ourselves some space here outside of our update we're going to create a bolt function that we can place here instead of our grounded ball that we have here so I'm going to do is public pool is grounded then let's use normal brackets and then curly brackets after that it's now inside this function we're going to make an if statement and pass in our raycast let's do if physics which gives us access to our physics class and then we're going to add 2D onto the end of this and then we're just going to do Dot boxcast and from here we are given access to a bunch of different variables the first one being our origin which is going to be the middle of our player which is transform dot position then it's looking for the size of the Box cast so for this we can create a public variable let's create a public Vector 2 called box size so now we can pass in our box size here then it's looking for an angle our angle is going to be at zero the direction is going to be minus transform dot up because you can't use transform dot down that's just not a thing so we do minus transform dot to replicator next it's looking for a distance float so again let's create another variable we can just call this public float and we can just call this a cast distance and then the final variable we want is going to be a layer mask now layer masks work similarly to tags in the sense that we can give different game objects a layer and then we can check for that layer in script so again let's go to the top and use public layer mask round layer and in here we can just do ground layer and then we can use curly brackets to open this up and we can just do return true then we need to do the reverse and disable this ball if the player is not colliding so we can just use else return false but the only issue with this currently is that we can't visualize this in the editor but luckily there is a really good way we can do that and that's using a function called on draw gizmos so let's do a void on draw gizmos then inside here we're going to essentially replicate this code just change a few things so we can see exactly where our box cast is in the editor let's do gizmos dot draw wire cube with the origin BNR transform.position and the size B in our box size to make sure we can visualize these values here I'm actually going to take away minus transform dot up and multiply this by our cast distance so back in the editor we can't actually see a box cast right now that's because we have these values here that we have set to zero so firstly let's set the box size so we can just drag on the x value here or you can just set this to a value so I'm going to drag this out and then our y value here and you can see as I increase it you can actually see this box so what we need to do is change our cast distance value here so as you can see as I adjust this the position of our box goes up and down and we need to move this down to the player's feet where we want to check for a platform we also have this layer mask here which gives us access to all of the current layers but we need to add one more and assign it to our platform so let's go to our platform here and I'm going to go to layer I'm going to click on it and press add layer and just type in ground for our layer then go to our platform and add this new ground layer finally let's go to our player here and set this ground layout to ground so now the last thing we need to do go back into our script and remove this grounded and replace it with is grounded with some curly brackets just like that so this right here is referencing this public ball and now if we test this out on the editor I'm currently grounded and I can jump if I spam jump you can see it does not work as grounded is only set to True when our boxcast is returning to True which is when we are on the ground so there you go guys that is two ways we can do a ground check inside Unity feel free to use whatever one works for you I hope you enjoyed today's video make sure to go to the patreon if you want the script which includes both methods I also recently released a new game on the Play store called tapari which is also in the description if you want to give it a try but apart from that guys I hope you enjoyed and I will see you all in the next video bye
Info
Channel: MoreBBlakeyyy
Views: 16,878
Rating: undefined out of 5
Keywords: unity ground check 2d, unity 2d platformer ground check, unity, unity ground check, unity 2d jump ground check, unity 2d, unity 2d platformer, unity platformer ground check, unity 2d tutorial, unity tutorial 2d, unity platformer 2d, unity check ground, unity jump ground check, unity 2d ground check, unity platformer, platformer, unity on ground check, unity 2d platformer tutorial, unity 2d game, 2d platformer, unity grounding check, unity boxcast ground check
Id: P_6W-36QfLA
Channel Id: undefined
Length: 9min 27sec (567 seconds)
Published: Wed Apr 12 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.