I have a set of extremely annoying problems with my 2D platformer, and they all come down to problems with Unity’s collision.
The first problem is that if I have a ground surface that is made up of multiple colliders, my player will randomly get stuck on the flat surface.
For example, I have a simple, flat surface, made up of several repeating tiles, and each of those tiles has its own collider. Mathematically, this is just a flat surface. But somehow, my character can get snagged walking across such a surface. It’s random, and some days it seems fine and other days it gets snagged like crazy.
What’s happening here is that the box collider around my character is detecting a collision between the side of its collider and the side of the ground’s collider. This should never happen, but it does. I’ll be walking across a flat surface, every tile using the exact same size for its collider and all at the exact same location in Y, and then suddenly I’m stopped like I just walked into a wall.
The forces me to restrict how I create my levels. I have to use one collider for an entire ground surface. I can’t have a flat surface that has any kind of special tile in center or at the edge, or else the player might snag on it. And I can’t simply have a collider as part of the same sprite I’m using for a tile, so I have to build my level’s visuals separate from my level’s collision, which makes it much more time consuming to make changes to a level.
The only way to resolve this problem is to have a rounded-bottom collider for my character. But this is even more limiting, because then my character can’t stand on the edge of a platform, or else they will just slip off of the rounded bottom. And being able to stand on the edge of a platform is a pretty important facet of a platformer, or else you can’t have tricky jumps, and thus can’t have, well, platforming.
But this isn’t even the extend of Unity’s collision problems. Following the examples we are given, I added some empty objects to my character and use their transforms as points to check to see if there is ground underneath my character. To make sure I am checking the whole area that the player can stand on, I have one in the middle, and one on either edge. Thus I can tell if my character is grounded, even if they are only just barely on the edge of a platform.
Except that when I stand on the very edge of a platform, the very smallest sliver I can manage, my character does not think they are on ground, and they start their falling animation and can’t jump.
I have set up the amounts exactly for how bug of an area the groundcheck checks for ground, exactly where the ground check is, and exactly how big the character’s collision is. Despite being mathematically perfect, I still have the tiniest sliver where the ground checks fail to see ground but the character is still held up by the platform.
At this point I can move the groundcheck by the smallest fraction possible, and then it is checking past the character collision. When this happens, when I jump while standing next to a wall, the groundcheck continually registers that I am still grounded, and will let my character fly upwards for the entire height of the wall.
This is due to how I have to arrange my character’s motion; since I can’t get the player’s movements to work exactly how I need them with the default physics, all calculations for stuff like gravity is handled within the player script. And logically, we don’t want to push the player down if they are grounded. So if the groundcheck says that they are grounded when they stand next to a wall, the player has no gravity if they are next to a wall.
But I shouldn’t have that problem anyway; I have my groundchecks set mathematically exact, but there is no point where I can have the groundcheck exactly line up with the collision.
The collision in this engine just doesn’t quite handle its edges properly.