Procedural level generation with colliders

I’m about to start working on procedural level generation for a 2D platformer. First I’m creating the tile prefabs for the generator to use. So I have multiple tiles in the scene, each with their own box collider.

The problem is, when my character (uses its own box collider) walks over the tiles, it’s kinda glitchy. His feet will glitch out (because the character thinks he’s in the air), and sometime just stops and isn’t able to move.

Here’s a gif demonstrating: Imgur

Any advice on what to do?

I don’t want to have to write an algorithm at runtime that’ll find all continuous chains of tiles and create a box collider that’ll wrap that whole thing. Though I can, it’s just a fair bit of work.

Look at this:

The FixedUpdate is called before any OnCollider event so on FixedUpdate set playerIsInAir = true;
and in your OnColliderStay set playerIsInAir = false; if the collider tag is the one you are looking for. This sould give you the rigth value at the time the Update function gets called.

bool playerIsInAir;

void FixedUpdate()
{
	// player is in the air
	playerIsInAir = true;
}

void OnCollisionStay(Collision collision) {
	if (collision.collider.tag == "Solid") {
		// I'm touching the floor or the wall
		playerIsInAir = false;
	}
}

Using OnCollider to tell if the player is on the ground is not a very good solution
because if the player is touching a wall but not a the ground your script will think
its on solid ground.

Try instead using the Unity 2D Plataformer example (Unity Asset Store - The Best Assets for Game Making)
they do a raycast to the ground that go far as about 1 pixel bellow the player an if thay ray cast finds
a collider with “Solid” tag if will tell player is in the ground.

Its some like this:

void Update ()
{
	RaycastHit hitInfo;
	playerIsInAir = true;
	if (Physics.Raycast(transform.position, Vector3.down, out hitInfo, playerHalfHeigth + 1)) {
		if (hitInfo.collider.tag == "Solid") {
			// I'm touching the floor (dont care about walls)
			playerIsInAir = false;
		}
	}
}

It works with Physics2D as well.