Size of a GameObject on screen ?

I want to display unit hp bar when only it takes up certain amount of screen space, wondering are there any functions i can make use of ?

Cannot simply compare the distance from camera, lets say i have many types of units, some of them are much bigger than the other, and i want the hp bar to have a minimum size, so i only want the hp bar to be visible when the unit takes up more than 1% of the screen space or something like that. Anyway to do that ?

I would start by converting the bounds of the object to screen space. The camera class has a bunch of useful methods for this.

Then it’s just some simple math comparing it to the points to the screen size.

1 Like

how to do that ? i can get the gameobject’s position, how to get its bounds ?

Okay, this is starting earlier then I expected. An objects position can be accessed via its Transform. Bounds can be accessed via Renderer.

Through the Renderer component as mentioned. Just keep in mind that it is a bounding box.

https://docs.unity3d.com/ScriptReference/Bounds.html

Bounds b = this.GetComponent<Renderer>().bounds;

By the way that document entry is literally the first result on Google for “Unity bounds”. Many of the simpler solutions we present to people are literally just us Googling and choosing one of the results on the first page. :wink:

1 Like