- Home /
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: http://i.imgur.com/qdRD3J6.gifv
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.
What do you use to find out whether the character is in the air/can move?
The character is considered in the air if the character's collider is not touching a collider with the 'Solid' tag, which all the tiles in the level have.
And I don't do anything to figure out if the character can move. That's just up to the collider.
Are u using OnColliderExit ? because between tiles it will be get called after OnColliderEnter and OnColliderStay so the player will think its in air for just a frame , when the OnColliderStay kics in again to tell its not.
$$anonymous$$akes sense?
Answer by lassade · Mar 25, 2016 at 08:05 PM
Look at this: http://docs.unity3d.com/Manual/ExecutionOrder.html
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 (https://www.assetstore.unity3d.com/en/#!/content/11228) 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.
I'm using the bounds of the collision to deter$$anonymous$$e if my player is colliding with the ground or a wall, so I'm not too worried about that. I'll look into setting the playerInAir
state in the FixedUpdate()
first to see how that pans out. If not too well, perhaps I'll try it with raycasts ins$$anonymous$$d.
I'll get back to you once I give it a try, thanks!
I went with the raycast, and that fixed the character thinking he's in the air, so the legs stopped glitching, but the character still occasionally gets stuck when walking: http://i.imgur.com/b$$anonymous$$OsYJq.gif
Some time ago i was reading a post about this problem (i can remember here) this may be caused if you are not using a capsule collider in your character and the tiles collider don't match perfectly (to the 9th decimal point kind of precision).
how lucky here it is http://www.iforce2d.net/b2dtut/ghost-vertices
Awesome, thanks I'll try that. Else I suppose I could have both a box collider for the top part of my character, and a circle collider for the bottom.
Raycasting seems like overkill, and can be pretty expensive. Why not simply use a Character Controller for the player? Then all you have to do is check if (playerController.isGrounded)
Alternatively you could emulate how .isGrounded works under the hood with collision flags.
Your answer
![](https://koobas.hobune.stream/wayback/20220612073214im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Large 2D polygon mesh levels 1 Answer
Moving BoxCollider2d with animation is not the right way? 2 Answers
Constant velocity when colliding - 2D 0 Answers
Make a gameobject Trigger Trigger Colliders but not Other Colliders 1 Answer
BoxCollider2D have an offset on collision,Collider2D has an collision offset? 0 Answers