# Checking if player is grounded using tilemap collision detection

beginner here just trying to learn the basics. I was following this tutorial but I ran into some trouble. Heres a screenshot of unity

So the tutorial calls for tagging the terrain with "Grounded" and essentially checks if the 2D collision box for GroundCheck collides with anything with a "Grounded" tag using the Move2D and Grounded scripts below. Note that the Move2D script is a component of the Player gameobject.

Move2D

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Move2D : MonoBehaviour
{
public float movespeed = 5f;
public float jumpforce = 10f;
public bool isGrounded = false;
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
Jump();
Vector3 movement = new Vector3(Input.GetAxis("Horizontal"),0f, 0f);
transform.position += movement * Time.deltaTime * movespeed;
}
void Jump(){
if (Input.GetButtonDown("Jump") && isGrounded == true){
gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 5f), ForceMode2D.Impulse);
}
}
}



Grounded.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Grounded : MonoBehaviour
{
public GameObject Player;
// Start is called before the first frame update
void Start()
{
Player = gameObject.transform.parent.gameObject;
}

// Update is called once per frame
void Update()
{

}

private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.collider.tag == "Ground")
{
Player.GetComponent<Move2D>().isGrounded = true;
Debug.Log("test");
print("GROUNDED");
}
}

private void OnCollisionExit2D(Collision2D collision)
{
if(collision.collider.tag == "Ground")
{
Player.GetComponent<Move2D>().isGrounded = false;
}
}
}



The error I'm running into is that it appears unity isn't recognizing that the player has collided with the tilemap collision box, so the bool isGrounded is just perpetually false and the player is unable to jump. Any help would be appreciated!

EDIT: below are screenshots of configs of Grid, gound, and GroundCheck in that order

• You could first move out the Debug.Log("test") from the condition and check if it is a collider problem or a condition problem. At 0:45sec in the video it marks the ground with a tag but it is not possible to see if you did the same with your ground from the screenshot Sep 30, 2020 at 8:29
• Is your GroundCheck collider set as "IsTrigger"? Please make sure you show us the configuration of both objects that are supposed to be involved in the collision. Sep 30, 2020 at 9:27
• @Zibelas I've added more screenshots, I did tag ground with "Ground".
– jeb2
Sep 30, 2020 at 9:33
– jeb2
Sep 30, 2020 at 9:34
• Alright, there are still a few questions. From the code of Grounded, it looks like it should go to the GroundCheck child gameobject of your player, but you have only the collider there (at least the start method would suggest that). What is your actual ground gameobject under the grid? since you have the tilemap on the grid directly. @DMGregory is IsTrigger needed when he uses OnCollisionEnter2D? Sep 30, 2020 at 9:57

3. It should work now but the character will hover over the ground. Your groundCheck should be marked isTrigger and your code should change from OnCollisionEnter2D(Collision2D collision) to OnTriggerEnter2D(Collider2D other). collider can be deleted in the condition to if(collision.tag == "Ground")