How to find point just outside of box collider?

I think this one is pretty easy but I couldn’t find the answer on my own. I have a character, above which I want to display a game object. In this case it’s a health bar. Because of some other decisions about the design of the game I cannot parent the object with the character so I can’t pre-position the object above the character. I have a box collider around the character that I can use to determine the size of the character. How can I get a Vector3 point indicating a position directly above the character, just outside of the box collider?

Here is the code that I’ve tried to use. It’s very inconsistent though and unusable. For some characters the status display is positioned well, for others it is way off. I’m obviously doing something wrong.

BoxCollider collider = characterScript.gameObject.GetComponent<BoxCollider>();

if (collider != null)
    GameObject miniStatusDisplay = (GameObject)Instantiate(StatusDisplayPrefab, transform);

    miniStatusDisplay.transform.position = Camera.main.WorldToScreenPoint(new Vector3(collider.transform.position.x, collider.transform.position.y + collider.size.y, collider.transform.position.z));

Attached is a visual representation of what I’m trying to accomplish.


Appreciate any help.

Thanks so much for your help. I figured out the problem! When I scale the characters, the box collider size stayed the same, so I had to scale down the box collider height by the character scale. Now it’s working correctly!

Here’s the code

var statusRectTransform = current.Value.MiniStatusDisplayScript.GetComponent<RectTransform>();

BoxCollider collider = current.Value.CharacterScript.gameObject.GetComponent<BoxCollider>();

if (collider != null)
    var newPosition = Camera.main.WorldToScreenPoint(new Vector3(current.Value.CharacterScript.gameObject.transform.position.x, current.Value.CharacterScript.gameObject.transform.position.y + (collider.size.y * current.Value.CharacterScript.gameObject.transform.localScale.y), current.Value.CharacterScript.gameObject.transform.position.z));

    current.Value.MiniStatusDisplayScript.transform.position = new Vector3(newPosition.x - ((statusRectTransform.rect.width * statusRectTransform.localScale.x) / 4), newPosition.y + ((statusRectTransform.rect.height * statusRectTransform.localScale.y) / 2), newPosition.z);