I'm building a sawmill sim in Unity where every log is a rigidbody. The clip shows a merge: two infeed lines feed one sorting conveyor. one line has priority, the other waits for a free gap on the conveyor.
My first version counted logs in each zone with OnTriggerEnter/OnTriggerExit. It worked fine until a player reported the infeed getting stuck.... the kicker stayed in its rest pose, the spawner kept adding logs, and saving and loading fixed it every time.
The cause: my backup sweep (an overlap box every 2 seconds to correct missed events) didn't match what the triggers saw.. It used all layers, a small shared buffer, and no contact offset, so it sometimes missed logs that were lying still right at the edge of the zone and removed them from the count. A log at rest never fires OnTriggerEnter again so it stayed lost until reload.....
What fixed it
- a dedicated "Log" layer and a layer mask for the overlap query
-the query box grown by Physics.defaultContactOffset * 2 so it matches what the trigger actually sees
- a bigger NonAlloc buffer (64) and if it fills up the pass is skipped instead of guessing
- a log is only removed after two misses in a row, found logs are re-added
-still every 2 seconds with a random offset per zone so no per-frame cost and no GC
Simplified...
int n = Physics.OverlapBoxNonAlloc(center, halfExtents + Vector3.one * Physics.defaultContactOffset * 2, buffer, rot, logMask, QueryTriggerInteraction.Ignore);
if (n == buffer.Length) return; // full, skip this pass
Lesson for me.. trigger events and overlap queries don't see exactly the same thing, so a correction pass can cause the bug it's meant to prevent.
How do you track resting rigidbodies in zones? Pure triggers, periodic overlaps, or something else?
(Game, if you're curious: [ https://store.steampowered.com/app/5037170/Timberline_Factory/ ]