- Home /
prevent 3d objects from overlapping in screen space
I want to be able to spawn TextMesh objects in a certain boundary in front of a camera with restricted movement, such that all spawned objects are always visible. That's no problem, but I'd also like to be able to move any pair of TextMesh objects which, to the camera, appear to overlap.
I've tried this:
1) - Create a BoxCollider around the TextMesh, sized relative to Renderer.bounds
2) - Cast rays from 4 useful corners of that box into the camera
3) - If any other TextMesh's Collider is hit, move both objects away from each other in XY space (camera is always facing +Z within a few degrees.
There is too much room for error here, because two TextMesh's CAN overlap relative to the camera in some scenarios without being caught by this check.
A sphere cast to do the same thing isn't precise enough, and dramatically limits the number of TextObjects I can have without them fighting each other for position.
I'm about to try creating screen-relative points from these 4 bounding box corners and building new rects or colliders on a plane in front of the camera to see how well that works. If these overlap, I move their representative objects apart by some factor.
Any suggestions for superior alternatives? Thanks a bunch!