Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Health Bars Following Enemies: Seems to "shake" when player moves & other such issues.

Discussion in 'UGUI & TextMesh Pro' started by infinitypbr, Oct 20, 2014.

  1. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    Hello! I have health bars just fine -- there's lots of questions and topics and tutorials on creating health bars. However, I'm having them follow the transform of the enemy objects, and while they generally do follow the enemies, they seem to shake a bit -- as if they're not following every frame, but delayed 3-4 frames or something.

    VIDEO: http://www.sfbaystudios.com/canvasHealth.mp4 (Sorry it's not on youtube)
    You can easily see the effect when the player is running back and forth.

    My canvas is set to have a reference resolution of 2048 x 1536, and the math on getting the proper position on that canvas is here:

    Code (csharp):
    1. targetScreenPos = playerCamera.WorldToScreenPoint (targetEnemy.transform.position);
    2. fixedPos.x = (targetScreenPos.x / screenSize.x) * canvasSize.x;
    3. fixedPos.y = -(canvasSize.y - (targetScreenPos.y / screenSize.y) * canvasSize.y);
    4. GetComponent(RectTransform).anchoredPosition = fixedPos;
    This happens if the new position is done in the LateUpdate() or regular Update(). With the older GUI system, almost the same concept for getting the position placement, it was always smooth, always at the right spot, so this is kind of confusing me.

    Any ideas? THANKS!
     
  2. Mikeysee

    Mikeysee

    Joined:
    Oct 14, 2013
    Posts:
    155
  3. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    Yep, looks like it -- I'm on screen space overlay. I'm wondering if maybe attaching it to a 2nd camera that only renders this, maybe even switching it to world space with that, would help. If it's related to the movement of the camera that may help, but I think it's related to simply moving the position of the element.
     
  4. Mikeysee

    Mikeysee

    Joined:
    Oct 14, 2013
    Posts:
    155
    Yep a second camera would do it. I may end up having to do that if they haven't fixed it by release time.
     
  5. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,181
    Hi, we have been looking into this and should have a resolution soon :)
     
    infinitypbr likes this.
  6. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
  7. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
  8. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    I haven't double checked yet, but a similar effect can be seen just by resizing the game window with any UI element that auto-resizes. It seems to have a delayed change, which I'm guessing is why the wobble occurs.
     
  9. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,870
    There are some delays in rendering when resizing windows in the Editor that only affects the Editor and won't affect game code at all or be visible in builds. So please don't take delays when resizing the Game View as an indication that the UI itself is delayed.

    If however you're seeing delays when not resizing the Game View, then that could potentially be a bug. However, make absolutely sure that calculations that convert from world space to screen space happen after the camera is updated with its new position, not before.
     
  10. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    Thanks runevision, I'll run some tests. It must be said I was testing on my laptop with both the scene view and the game view opened and sometimes I get lag from that. I'll validate that it's okay with a standalone build.