(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
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