OnCollision2D triggers even if not touching

Hello All,

I hope you can help me out as this problem is driving me crazy.
I just started a project and for now I am trying to make basics and fix all bugs.
I have player object and some blocks. Each block has 4 child box colliders (this is to determine what side of the block is colliding). Now I have 2 problems:

  1. Player is not touching ground at all! - Note that the sizes of the player and block are very small
  2. OnCollision2D triggers functions even if player does not touch any block (it’s constantly floating above the ground?)

See screenshots and piece of code:

private void OnCollisionEnter2D(Collision2D collision)
    {
        Debug.Log("Collision!" + collision.gameObject.tag);
        //collision.rigidbody.isKinematic = true;
        //able_To_Move = false;

        if (collision.gameObject.tag == "Dead")
        {
            hp_Points = 0;
            is_Jumping = false;
            game_Controller.Game_Over();
        }
        else if (collision.gameObject.tag == "Damage")
        {
            this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y, this.transform.position.z);
            hp_Points -= 10;
            is_Jumping = false;
            game_Controller.Game_Over();
        }
        else if (collision.gameObject.tag == "Block_Top")
        {
            { is_Jumping = false; }
        }
        else if (collision.gameObject.tag == "Block_Left" || collision.gameObject.tag == "Block_Right")
        {
            able_To_Move = false;
        }
       
    }


Thank You!

Well, is there some code that prints out the name of the objects in something you didn’t post here?
Or did you alter the code before posting?
I don’t see the word “Collision!” before the names, so maybe it’s not that posted code that’s calling these Debug.Logs.

Second row in my code…
Debug.Log(“Collision!” + collision.gameObject.tag);

No, no, you misunderstood what I meant. I don’t see the words from the second row of your code in the console output! :slight_smile: Omgoodness lol Now, I do… it’s only on the last 1 and happened 6 times. Hm, sorry I dunno. :slight_smile:

Pardon me for asking the obvious, but are you sure that your box colliders on both the player and on the top of the blocks (since this is the one the debug shows the player triggered a collision with) aren’t larger than the block/player? (i.e the box collider on top of each block is only up to the top of the block? The box collider on the player only goes as low as the feet of the player and not below at all?)

It really doesn’t matter what size the player or the blocks are, it matters what size the box colliders are.

I don’t know what level you’re at with this stuff, so that’s not meant to be insulting your intelligence or anything. I just asked because you were talking about how they graphically don’t touch each other, and really that’s not how collision detectors work (I mean, they’re not tied to graphics specifically, but to bounding areas of some sort that you set up… which may or may not match your graphics).

Hi cstooch,

Thanks for your reply. No hard feelings:)

If you look on my screenshot I’ve posted you can see the box colliders (Green borders around the sprites) an you can clearly see that they do not touch at all.
As I mentioned before the sizes of each sprites are very small (0.3 unity unit per box ). Actually I have tested that on the bigger sprites and I had the same results…
Anyone knows when the colliders trigger ? When they actually “touch” or a fraction of a unit before?