Check If Object / Character Is Grounded - Unity 2D

This will be a very quick post on how to check if an object / character / player / car / vehicle or whatever kind of 2D object is grounded or not. To do this we use the OverlapCircle static function of the Physics2D class. Of course there are other functions, however, for the purpose of demonstration we use this function. You can follow the same steps to achieve the same using the other function.

To use the assets that we are using in this post, download the Sprites folder from here. Extract and add the sprites into your project.

1. Create a New Scene. Drag and drop the ground sprite onto the Hierarchy, this should create a ground object. Position it at (0, -4, 0). Set the Scale to (7, 3, 1). 

Ground Setup

2. Add a PolygonCollider2D component to this ground object.



3. Add a New Layer named Ground and set the layer of the ground object as Ground.

Add Ground Layer

4. Drag and drop the Wheel sprite onto the Hierarchy just like you did for the ground sprite. Now, you should have a Wheel object in the Scene. Add a CircleCollider2D component to it.

Add Wheel

5. Now, while the Wheel object is selected in the Hierarchy, create an Empty gameobject so that it is created at the same position as the Wheel object. If it is not created at the same position, change it's position the wheel's position. Rename the object to GroundHitCheck and make it the child of Wheel object.

Ground Hit Check
6. Create a new C# Script named GroundHitCheck and add the below code to it:

using UnityEngine;
using System.Collections;

public class GroundHitCheck : MonoBehaviour {

 //flag to find out if the object is grounded
 bool isGrounded = false;

 //Empty gameobject created to determine the bounds/center of the object
 public Transform GroundCheck1;
 //public Transform GroundCheck2; //uncomment this for OverlapArea

 //The layer for which the overlap has to be detected 
 public LayerMask ground_layers;

 //All the Physics related things to be done here
 void FixedUpdate(){
  //check if the empty object created overlaps with the 'ground_layers' within a radius of 1 units
  isGrounded = Physics2D.OverlapCircle(GroundCheck1.position, 1, ground_layers);

  //uncomment this and comment the above line, if you are using a rectangle or somewhat similar shaped object
  //isGrounded = Physics2D.OverlapArea(GroundCheck1.position, GroundCheck2.position, ground_layers); 

  Debug.Log("Grounded: "+isGrounded);

 }

}

The code above is very simple, however, I will line involving the OverlapCircle function
The Physics2D.OverlapCircle takes three parameters in the above function which are the empty object's position, which is (0, 0) in our case. Note that it is a Vector2. The second parameter is the radius to which the circle extends. It is set to 1 as the radius of the wheel is also 1.
(How do I get to know the radius of the wheel? Well, the radius of the component CircleCollider2d of the wheel is 1, which is apparently the radius of the wheel as well.)
The third parameter is the list of layers that we should check for overlap.
Not to forget, this function returns a boolean.

7. Attach the above script to the Wheel object. Drag and drop the GroundHitCheck object into the field Ground Check 1. Select the Ground layer from the ground_layers dropdown.

Attach the Script
We are done with the setup and coding. All that is left is to test and see if it works.
Click on the play button or hold ctrl+p to test the scene and check how the console displays true / false when the wheel is moved towards the ground and taken away from it.

Unity 2D Check If Grounded
This is how you check if the object is grounded for an object that is circle shaped, if you have a rectangle or a square shaped object you would want to use the OverlapArea function which accepts two Vector2 parameters (two Vector2 for the two diagonal ends of the square or rectangle) and a layer_mask parameter just like the OverlapCircle.

To use it you would simply create two empty gameobjects and place them at two diagonal ends of your object or place them at points where you want to raise a GroundHit flag when those position hit the ground.

See you around.




  
Share on Google+

About Sujit Horakeri

Sujit Horakeri is a game freak just like any other next door guy you would come across. He is a Web Developer by Profession, Game Developer by Choice.
Connect with him on:
    Blogger
    Facebook

0 comments:

Post a Comment