- Home /
health bar that scale to the zoom
I am using GUI.Box to draw health bar and it works when i use fixed width and height: GUI.Box(new Rect(healthBarScreenPos.x, healthBarScreenPos.y, 2, 0.5, "Health");
However players can zoom in and zoom out and the health bar's size doesn't change because GUI doesn't shrink or enlarge based on camera zoom.
How would i translate my pixel values to screen that takes zoom level into account? I feel like GUI is not the right solution here. I am forcing it to shrink/enlarge programatically. Would using normal game object and changing their size work better? I am writing 2d game..
Answer by Nodgez · Feb 18, 2014 at 10:08 PM
You can set another camera to display the GUI. Make a new layer called "gui" and then make a sprite or quad(you're preference) that is set to the "gui" layer.
Then give this 2nd gui Camera the same scrolling method/script as the other camera and set it's culling mask to just be "gui".
you'll need to position the gui camera depending on where and how you want to draw the gui.
i don't think that would work. the problem is that i am creating a tank game where each tank has its own health bar. I am not sure if adding camera for each health bar is a good idea. (there can be up to 9 tanks). really all i need is a GUI element that i can scale based on the camera size
i guess better question would have been, how do i translate length in pixel to screen size?
Sorry, $$anonymous$$aybe I wasn't clear enough in my explanation.
You don't need 1 camera for each health bar. you just need to set each health bar's layer to "gui" and then you're 2nd camera will display everything that is on the layer "gui". so in total you will have 2 camera's. 1 as you're main to draw everything except the "gui" layer and then the other that draws only the "gui" layer.
I recommend having a look at the video if you haven't. The is a small part on GUI and camera's in it
Hm... now i have GUI.Box in "gui" layer and i created another camera that only shows "guy" layer and i set its projection as orthographic because it is 2d game. When I zoom in/out, my script also change this 2nd camera's size but because GUI.Box is gui element, it's size doesn't change regardless of whether i put it in a different camera or within the main camera. How does putting it into a different camera make GUI stuff scale automatically? Or are you suggesting using non-GUI stuff like sprite? if that is the case, 2nd camera isn't necessary anyway? i am confused...
Yes you would use the sprite to display the health bar. the reason for the 2nd camera is so that the health bar will be displayed like a GUI i.e. on top of everything else in the scene using the the depth value of the camera. if you use the same camera for both the GUI and the tanks then there is a chance that some of you particle effects or other tanks will draw over the health bars blocking them from view.
Your answer
![](https://koobas.hobune.stream/wayback/20220613133938im_/https://answers.unity.com/themes/thub/images/avi.jpg)