Check if 2D Character is grounded

To do this i looked a lot on the internet. I tried to check the velocity, but checking 2d velocity doesn’t seem to work for some reason. But what i found was i should probably try linecast. But all the things online were all C# which i do not want. Is there like a simple string of code i can put into a script to check if the attached gameobject is grounded? Easiest method please haha

I think you will want to use Physics2D.OverlapArea rather than a raycast at a single point. I can guarantee a raycast would end up with you sitting on the corner of your collider and it doesn’t say you’re grounded. Basically you can check a rectangular area for collisions and use that to determine if you’re grounded.

//Corners of the rectangle to check
public var top_left : Transform;
public var bottom_right : Transform;
public var ground_layers : LayerMask;
var grounded : bool;

function FixedUpdate(){
    grounded = Physics2D.OverlapArea(top_left.position, bottom_right.position, ground_layers);	

Depending on the shape of your characters collider you might want to use Physics2D.OverlapCircle instead.

I think you want to raycast. Try adding a raycast pointing down to check if something is below it.


If it shows up as C#, there should be a drop down near the top left that says “Scripting Reference Using-dropdown

function FixedUpdate() {
	// Cast a ray straight down.
	var hit: RaycastHit2D = Physics2D.Raycast(transform.position, -Vector2.up, 0.1);
	// If it hits something...
	if (hit != null) {
        isOnGround = true;

Hope this helps.

Basically if you are developing character based game then you must have to view this video.
As per my consideration you definitely find your all doubts clear.

2D Character Controller

Add a character controller and check the .isgrounded property of it.

Here is the Unityscript reference

Here is JavaScript

function Update () {
		var controller : CharacterController = GetComponent(CharacterController);
		if (controller.isGrounded)
			print("We are grounded");

NOTE: Must add a charactercontroller to your object for this to work

If raycast does not work! use this.

private void OnCollisionEnter2D(Collision2D other)
if (other.gameObject.tag == “platForm”)
isOnGround = true;

private void OnCollisionExit2D(Collision2D col)
    isOnGround = false;