Voxel game player collision?

I’m making a voxel (cube) game, and I have a tricky problem with the collision side of things. If you ever played most of the popular voxel games, like Minecraft, Cube World, Trove, etc. You will notice that if you walk into a corner (or up against a wall) and start turning, you don’t get pushed away from the wall (and the edges of the player sink into the blocks). If the player had a box shaped collider, the corners would cause the player to get pushed back as he’s turning.

But also, if you try to walk off a block, you fall off instantly, not sink away as if the bottom part of the player was that of a capsule collider.

Essentially what I’m saying is, the player collision feels like both a capsule and box collider at at the same time. And I’m having real trouble implementing that. I’ve gotten close, by making the main collider a capsule, and putting a box collider at the feet of the player. I have made the box collider less wide than the capsule, so that walls can collide with the capsule, while the blocks that the player is standing on top of, will collide with the box.

So far, after many iterations, it seems there’s no middle ground in the size for the box collider. As the player will either very slightly get pushed in a curve while falling off a block (due to the capsule collider), or push itself away from walls when turning (due to the box collider).

I tried freezing the rotation of a regular box collider (covering the whole player), but the collider would always update faster than it was being frozen, which caused very slight movements while turning. These movements were enough to be very noticeable.

I have tried searching, but this is so specific, that I just had no idea what to search. And searching things like “minecraft type player collision” definitely didn’t give any useful results (as expected).

Any ideas?

I found this video demonstration, which seems to do exactly what other voxel games do. The problem is, neither it nor the guide site it links in the description, talk about how this was done. It also wasn’t made in Unity.

P.S. The reason this thread is in Scripting, and not Physics, is because I’m 101% sure this will need to involve coding, and will not be possible otherwise.

P.P.S. I could just make a cylinder in Blender, and use it as a mesh collider, but the bottom of the player needs to act as a square shape.

I just made a cylinder model in Blender, set it so it’s no bigger than the player, and I think I’ll actually go with it. Since the cylinder has a radius, I will always fall off at the edge of blocks at the same time as the player physically fully steps off them.

I did some testing, and it is insanely hard to walk off a block in such a way, that the corner of the player sinks through. I had to actually comment out the code doing the rotation of the player, to pull it off and see how much the corners would sink.

Besides, if I don’t mind the player’s sides sinking into blocks, I don’t really see why I originally minded the bottom sinking. Other than forgetting that a circle is the same length in any direction from the centre.

Sorry to anyone who read that gigantic wall of text.

Though it would still be interesting to hear/see how voxel games handle collision of this type.

I wouldn’t use a mesh collider, personally, especially for a collider that’s not static. I would probably use two or three primitive colliders instead- one or two small box colliders for the “feet” whose sole job is to deal with the collisions with the ground, and one capsule collider for the body whose job is to deal with collisions with everything else. If you make it so that the box colliders barely fit in the capsule collider’s body area (about 70% of the capsule’s diameter), that should be about as close as you can get to falling off of edges all at once without much sliding.

To increase the quality of that effect, two box colliders (one turned 45 degrees) will be even more effective. Here’s what the result might look like:

Even with three primitive colliders as a compound collider, instead of 1 simple mesh made in a 3D app, I think you’ll find the performance a lot better, and of course you can use only 1 (or zero) box colliders at the base for enemies, if you don’t care if they slide off of the edges of blocks so much as you do the one that’s player-controlled.

1 Like