Creating UI bounding boxes/target boxes around objects

Anyone have any idea on the proper way to do this in VR? I’m making an x-wing style game with target boxes around the enemies.

I wrote bounding box code that draws sprites on a Screenspace overlay UI canvas, but this code does not work in VR because it seems that cam.WorldToScreenPoint and cam.WorldToViewport don’t seem to work consistently in VR.

I also thought about attaching a canvas to every object that needs a bounding box, but there are downsides to this (performance, objects occluding the billboarded canvases)

What would be the best solution for this use case?

Oh hey, recognize the name, looking forward to trying your game!

Did you ever find a good solution? I’ve tried a couple of things but have yet to find something that works. I can put a sprite at the location of the object, but it tends to scale a little funny and not look good at a distance. Though maybe I can just clamp the size.

I tried having a Canvas in Camera Space, but that tends to get all wonky.

I’m pulling my hair out a bit trying to get this to work in VR.

EDIT: I’m half tempted to move to a outline shader instead.

So I have something, but I don't love it:

  var vecToTarget = currentTarget.transform.position - Camera.main.transform.position;
  targetBoxSprite.transform.SetPositionAndRotation(
      vecToTarget.normalized * 3.0f + Camera.main.transform.position,
      Quaternion.LookRotation(Vector3.ProjectOnPlane(Camera.main.transform.forward, Vector3.up), Vector3.up));

  var localRect = GUIRectFromBounds(targetBounds);
  float scaleX = localRect.width * ogSpriteSize.x;
  float scaleY = localRect.height * ogSpriteSize.y;

  var finalRect = new Vector2(scaleX, scaleY) / ((currentTarget.transform.position - targetBoxSprite.transform.position).magnitude * 0.5f);
  targetBoxSprite.size = new Vector2(
       Mathf.Max(minTargetBoxSize, finalRect.x),
       Mathf.Max(minTargetBoxSize, finalRect.y));



readonly Vector2[] extentPoints = new Vector2[8];
private Rect GUIRectFromBounds(Bounds bounds)
{
    Vector3 cen = bounds.center;
    Vector3 ext = bounds.extents;

    var mtx = Camera.main.worldToCameraMatrix;
    extentPoints[0] = mtx.MultiplyPoint(new Vector3(cen.x - ext.x, cen.y - ext.y, cen.z - ext.z));
    extentPoints[1] = mtx.MultiplyPoint(new Vector3(cen.x + ext.x, cen.y - ext.y, cen.z - ext.z));
    extentPoints[2] = mtx.MultiplyPoint(new Vector3(cen.x - ext.x, cen.y - ext.y, cen.z + ext.z));
    extentPoints[3] = mtx.MultiplyPoint(new Vector3(cen.x + ext.x, cen.y - ext.y, cen.z + ext.z));

    extentPoints[4] = mtx.MultiplyPoint(new Vector3(cen.x - ext.x, cen.y + ext.y, cen.z - ext.z));
    extentPoints[5] = mtx.MultiplyPoint(new Vector3(cen.x + ext.x, cen.y + ext.y, cen.z - ext.z));
    extentPoints[6] = mtx.MultiplyPoint(new Vector3(cen.x - ext.x, cen.y + ext.y, cen.z + ext.z));
    extentPoints[7] = mtx.MultiplyPoint(new Vector3(cen.x + ext.x, cen.y + ext.y, cen.z + ext.z));

    Vector2 min = extentPoints[0];
    Vector2 max = extentPoints[0];

    foreach (Vector2 v in extentPoints)
    {
        min = Vector2.Min(min, v);
        max = Vector2.Max(max, v);
    }

    return new Rect(min.x, min.y, max.x - min.x, max.y - min.y);
}

There's obviously some magic numbers involved, and the rectangle seems to change in size as my head moves, which I'm not a fan of. Maybe I should take the average position?

My targetBoxSprite is a single sprite w/ no parent in the scene, it's setup to use a sliced sprite.

I think a shader or GL Line draws might serve me better, hard to tell.