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.
[Physics.PhysX] cleaning the mesh failed
because some are too thin. \$\endgroup\$