Search Unity

Create UI health markers like in World of Tanks

Discussion in 'UGUI & TextMesh Pro' started by DroidifyDevs, Sep 24, 2016.

Thread Status:
Not open for further replies.
  1. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    Hello!

    So currently I'd like to make HP bars for my players. Obviously, first thing that comes to mind is to make world-space canvases and put them on top of my players. Thing is, when looking at them from different angles, you won't be able to see them. OK, you could say "Well use transform.LookAt so they always face the camera", but that can get expensive on mobile. Here's what I'm going for:



    Or:


    Or:

    However I don't know how to make markers like this.

    Also worth noting is that most game's enemy markers can be seen even if your view of the enemy is blocked, making me think they aren't placed as children of the players.

    If you have any ideas, let me know. Thank you!
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @DroidifyDevs

    hi there, solution is quite simple;

    Have one canvas, or use existing one if you already have a screen space canvas; just add a grouper under which you can add/remove or enable/disable markers, then place them over your enemies by converting their world space position into screen space / canvas position and place them on 2d canvas space "above" your targets.

    This is the easiest way to do it IMO, if your markers are not going to be integrated into world space, i.e. they are just screen size markers, not floating above and transforming along objects they represent. This way they will also be always on top of everything else, if that is what you want.

    There are threads here in UI sub-forum IIRC about this topic, just do a search.
     
    Last edited: Sep 25, 2016
    Josh_706 and DroidifyDevs like this.
  3. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @DroidifyDevs

    actually I've got it in my notes - there are various ways to come to this result, this is one I've used I think:

    Code (csharp):
    1.  
    2.   // Offset position above object bbox (in world space)
    3.   float offsetPosY = target.transform.position.y + 1.5f;
    4.  
    5.   // Final position of marker above GO in world space
    6.   Vector3 offsetPos = new Vector3(target.transform.position.x, offsetPosY, target.transform.position.z);
    7.  
    8.   // Calculate *screen* position (note, not a canvas/recttransform position)
    9.   Vector2 canvasPos;
    10.   Vector2 screenPoint = Camera.main.WorldToScreenPoint(offsetPos);
    11.  
    12.   // Convert screen position to Canvas / RectTransform space <- leave camera null if Screen Space Overlay
    13.   RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, screenPoint, null, out canvasPos);
    14.  
    15.   // Set
    16.   markerRtra.localPosition = canvasPos;
    17.  
     
  4. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    Thanks for the ideas! I'll look into it as it seems like the right approach :)
     
  5. OlliIllustrator

    OlliIllustrator

    Joined:
    Nov 1, 2013
    Posts:
    71
    Thank you for posting this, helped me immensely.
     
  6. StormMuller

    StormMuller

    Joined:
    Jun 2, 2015
    Posts:
    17
    2 Years later and this still helped me out a lot, Thanks.
     
  7. Keepps65

    Keepps65

    Joined:
    Oct 26, 2017
    Posts:
    4
    Two and a half years later, and it helped me out too. Thanks man!
     
  8. mgcg12

    mgcg12

    Joined:
    May 28, 2018
    Posts:
    8
    2199, Thanos destroyed everything and still helped me out a lot too
     
    Perifanos likes this.
  9. Coverttrickster

    Coverttrickster

    Joined:
    Dec 22, 2014
    Posts:
    2
    Everything Thanos destroyed is now back and this still helped me out a lot.
     
  10. realkillerx

    realkillerx

    Joined:
    Nov 9, 2013
    Posts:
    10
    Valve finally announced a new Half-Life game after 12 years and this still helped me out a lot
     
    Last edited: Feb 7, 2021
    stuartdn likes this.
  11. Perillio

    Perillio

    Joined:
    Feb 4, 2019
    Posts:
    18
    The years passed along, no one knows which calendar we are using now, maybe the one from that other planet we found many ages ago. Humanity is spread around the universe and mixed with alot of alien races but this helped me out a lot too :)
     
    cosmicninja likes this.
  12. Razziel

    Razziel

    Joined:
    Nov 1, 2016
    Posts:
    7
    such help. many thanks.
     
  13. UnseenBeyond

    UnseenBeyond

    Joined:
    Jul 2, 2015
    Posts:
    2
    This is dumb, why wouldnt you just give it its own canvas?
     
  14. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @UnseenBeyond

    If you are talking about the OP concept - the reason is pretty clear why he asked... he even explained why and gave several examples. But it of course depends on what you are trying to do.
     
  15. willee2

    willee2

    Joined:
    Aug 2, 2014
    Posts:
    1
    The world is in pandemic and this still helped me out.
     
    AshBacchus likes this.
  16. williamwolseleycharles

    williamwolseleycharles

    Joined:
    Apr 27, 2018
    Posts:
    1
    Its 2077 and Cyberpunk 2077 still hasn't been released and this still helped me out.
     
  17. mattsteffey

    mattsteffey

    Joined:
    Nov 5, 2020
    Posts:
    2
    Cyberpunk got released and it helped me out.
     
  18. juanma_rs

    juanma_rs

    Joined:
    May 10, 2020
    Posts:
    3
    cyperpunk got removed from the psn and it continues to help me out.
     
  19. StephanWeber

    StephanWeber

    Joined:
    Feb 7, 2021
    Posts:
    1
    Trum is not President anymore and it still helped me out a lot, thx.
     
  20. vince_cab

    vince_cab

    Joined:
    Mar 20, 2021
    Posts:
    1
    Had to reset password and 3.23% of world population received at least one injection of Covid-19 vaccine, yet this still helped, thank you!
     
  21. Aenorya_Pro

    Aenorya_Pro

    Joined:
    Nov 26, 2018
    Posts:
    4
    I am on my 20th lockdown and it still helped me out. Thanks !
     
  22. TouchedMoon728

    TouchedMoon728

    Joined:
    Aug 1, 2019
    Posts:
    2
    Well, I guess Jesus is back now but he didnt even know the answer to this one. Thanks man
     
Thread Status:
Not open for further replies.