# How big can/should wall colliders be?

Following my previous question, I now have my wall collider building tool and built many of them:

Bird-view, about 17 walls selected, on the right, one is selected:

Here, wall and barriers form a collider so the car will hit the barrier:

Here you can climb on that little slope:

Question:

Having either a single, large collider or tons of small ones doesn't sound good to me.

What's a good metric in deciding how many (and how big) colliders should there be?

Also, should they be as simple as possible, e.g. less points for straight lines?

• Let's try not to make premature judgements based on how things "sound". Try running a test. Do you observe any measurable problems with either all your colliders in one big object or all separate objects? Once you've identified the specific problem, we can help you solve it. Without this step, we might solve the wrong problem, optimizing for a metric that's actually already fine for your needs, and in doing so introducing a trade-off that turns out to be worse for your game on the whole. Jun 11, 2021 at 14:25
• @DMGregory Makes sense, going to test first then update my question, thanks!
– aybe
Jun 11, 2021 at 14:42
• And have as well a backup plan if your player somehow manages to go over them or clip through Jun 11, 2021 at 18:25
• A box collider with some thickness will be more clip-resistant than a mesh that's paper-thin, if that proves to be an issue here. Jun 11, 2021 at 20:11
• You guys are right, I'm already starting to experience issues not even while playing (not there yet) but at edit time, e.g. [Physics.PhysX] cleaning the mesh failed because some are too thin.
– aybe
Jun 13, 2021 at 17:27

Not only how tall and big walls should be, how thick is also very important as I started seeing this:

[Physics.PhysX] cleaning the mesh failed

Wall model is the following:

• a wall is made of points, at least two, in the end only p0 is needed for a wall segment
• p1 is next segment, p2 and p3 are simply p0 + Vector3.up

How were thick walls in gray built in a easy way?

Generate a mesh then a collider out of it. That was long, painful and not even right!

Again, by leveraging Unity's stuff:

• compute wall segment center out of p0/p1/p2/p3
• compute normal from plane of p0/p1/p2, re-orient to world up
• compute forward from p1 - p0

At this point we have all we need to create/scale/orient child colliders!

The code is as simple as that:

private void RebuildColliders()
{
// start clean, destroy child box colliders

while (transform.childCount > 0)
DestroyImmediate(transform.GetChild(0).gameObject);

// if we don't want colliders then return now

if (!RebuildCollidersEnabled)
return;

// create child box colliders

var vertices = GetComponent<MeshFilter>().sharedMesh.vertices;

var quads = vertices.Length / 4;

for (var i = 0; i < quads; i++)
{
var p0 = vertices[i * 4 + 0];
var p1 = vertices[i * 4 + 1];
var p2 = vertices[i * 4 + 2];
var p3 = vertices[i * 4 + 3];

var pf = Flipped ? (p1 - p0) : (p0 - p1); // Z-axis

var pg = GameObject.CreatePrimitive(PrimitiveType.Cube);
pg.name = \$"CUBE {i}";
GameObjectUtility.SetParentAndAlign(pg, gameObject);

// NOTE all the following changes do adjust the collider automatically

var pt = pg.transform; // re-position and re-orient the collider

pt.position = (p0 + p1 + p2 + p3) / 4.0f; // they're all on same plane so this is fine

pt.rotation = Quaternion.LookRotation(pf.normalized, new Plane(p0, p1, p2).normal) *
Quaternion.AngleAxis(90.0f, Vector3.forward); // normal was X-axis, make it Y-axis

var ww = WallWidth * WallScale;
var wh = WallHeight * WallScale;
var wl = pf.magnitude * WallScale;

pt.localScale = new Vector3(ww, wh, wl); // size the collider to fit wall

pt.Translate(-ww * 0.5f, 0.0f, 0.0f, Space.Self); // collider is centered, should be on edge instead
}
}


Conclusion:

Of a line segment, I've been able to generate thick walls quite easily, this is really cool.

Walls should definitely be thick somehow else PhysX might choke in generating colliders for them and as @DMGregory suggested, it's a good insurance for the collisions.

For their length, I suppose they're fine for now, I've sliced them to what made sense to me though I may have to edit them for some reason I don't yet know.