how to find contact points with a trigger collider?

(At first sorry for the english… i’m french :-p )
Hi, i’m a very beginner coder and i’m trying to do my first game. It’s a platformer.
I use 2dtoolkit and unity 2d physic. It’s great but i have a lot of problems to solve :-p
One of them is when i have to walk on a ground and slide against a wall on a same tile:-p …
frictions are different according the character position.
To do it, i choosed first to split my blocks into different groups (grounds, walls and… corners…)
I use also a ground checker (as trigger) in the hierachy of my character.

  • When it collide a ground a onGround flag is trigged (no problem… or almost…)
  • With walls nothing append… normal…
  • but with corners…(ouch…)
    I try to change the block material physic on fly according the ground checker position and the contact points…
    but nothing works :frowning:
    this is how i tried to proceed (it’s a part of the ground check script and a schema)

void OnTriggerEnter2D(Collider2D collision)
    {

        if (checkColParent(collision))                                      // check if collided element have a parent and store it in parentName
        {
            if (parentName == "corners")                                   
            {
                currentGround = collision.transform;                        // store current collider transform

                collision.sharedMaterial = pMatGround;                      // set default physic (ground) stored in public variable
                bool ground = true;

                // set max contact points
                EdgeCollider2D[] edgesCol = collision.transform.GetComponents<EdgeCollider2D>(); // corners have egdeCollider2D (to be sure i take all of them if there are severals)
                int maxContact = 0; foreach (EdgeCollider2D egdes in edgesCol) maxContact += egdes.pointCount;  //add all point counts in maxContact

                // get contact points into contacts
                ContactPoint2D[] contacts = new ContactPoint2D[maxContact];
                collision.GetContacts(contacts);
                Debug.Log(contacts[0].collider);            // returns always NULL!!!!! (ᗒᗣᗕ)՞  contacts seems to not receive anything from GetContact ლ(ಠ_ಠლ)...?

                // search a point above the character ground level
                foreach (ContactPoint2D point in contacts) if (point.point.y > transform.position.y) { ground = false; return;} // stop the loop if a point above is found

                if (!ground) collision.sharedMaterial = pMatWall;           // turn friction into zero
                else onGround = true;                                       // if not active onGround flag
            }

            if ((parentName == "plateform" || parentName == "plateformMobile" || parentName == "grounds" ))       // si l'objet collidé a pour parent wall ou plateform (décor ou mobile)
            {
                currentGround = collision.transform;                        // store current collider transform
                onGround = true; delayedOnGround = false;                   // active onGround flag disable delayed jump flag
            }
        }
    }

if somebody can help me… THX

OK… finally i have removed all the script in replacing by this line

onGround = Physics2D.Linecast(transform.position,playerRigid.transform.position, 1 << LayerMask.NameToLayer("grounds")| 1 << LayerMask.NameToLayer("platform"));

i still don’t know what’s wrong with GetContacts function…
By the way, thank you to the unity team for their very usefull project examples :-p I should spend more time on them…
I think re-use linecast for walls :slight_smile:

1 Like