Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

health bars

Discussion in 'General Discussion' started by Trackpants, Mar 3, 2012.

  1. Trackpants

    Trackpants

    Joined:
    Jan 5, 2012
    Posts:
    217
    When making health bars, is there a specific reason that people don't just use cubes with textures set at a certain distance from the main camera?

    Are the cubes more taxing then a GUI or do other people just find it easier to work with GUI? Because I've just found it easier to work with cubes on screen is all.

    Discuss!
     
  2. n0mad

    n0mad

    Joined:
    Jan 27, 2009
    Posts:
    3,732
    If you want this kind of health bar :



    then yes, a cube or a plane is ok.

    But as soon as you try to achieve non rectangular bars, such as this example, or even just non-rectangular bar (which is kind of a standard nowadays), you have to go with a sprite + mask :)
     
  3. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,816
    I use a set of planes for my UI stuff. I don't use Unity's GUI system.
    You may as well texture a plane, it will have marginally better performance than a cube :)
     
  4. Trackpants

    Trackpants

    Joined:
    Jan 5, 2012
    Posts:
    217
    @n0mad
    Yeah, that's pretty much what I figured, but after looking at the example you showed me, it seems like you'd be better off going with the alpha mask, even if it is a bit more effort, due to presentation. Hmmm, perhaps I shall have to look into it :)

    EDIT:
    @Meltdown
    How did you achieve the gas and nos bars in your monster truck game using planes, if you don't mind me asking
     
  5. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,816
    I have a second camera in my scene that is orthographic. Pointing at all my UI elements.

    I then used his most esteemed Sir Michael Garforth's CreatePlane script from the wiki to create a bottom anchored plane. Thus when you scale it, it scales from the bottom, not the middle, so the bar resizes.. well like a progress bar.

    For the planes I obviously don't use Unity's default plane, you can use the same script to create a 1x1 (two triangle plane mesh) for better performance.

    Then just scale the plane height based on your value and a percentage of the plane at it's max height.

    This setup works on all resolutions.. but if the screen aspect ratio changes, you'll need to do some manual adjustment of the plane positions. But other than that it will work on all resolutions and aspects/screen resizing you throw at it, which is another reason I preferred it to UnityGUI.
     
    Last edited: Mar 3, 2012
  6. Trackpants

    Trackpants

    Joined:
    Jan 5, 2012
    Posts:
    217
    Ah, that does make sense! thank's for the info. For some reason changing the anchor point never occurred to me haha
     
  7. TehWut

    TehWut

    Joined:
    Jun 18, 2011
    Posts:
    1,577
    Yeah I just use textured planes. Don't care much for Unity's GUI system.
     
  8. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,816
    Believe it or not I spent a couple of hours trying to manually move and resize a regular plane to make it look bottom anchored lol then eventually started bashing my head against the keyboard. then remembered the anchor function in that script and said.... "No.. it couldn't be.." lol :D