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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Laggy health bar

Discussion in 'Scripting' started by kostic017, Aug 19, 2022.

  1. kostic017

    kostic017

    Joined:
    May 25, 2019
    Posts:
    9
    Health bars are supposed to be positioned above the characters' heads. Here's the script that I'm using:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class HealthBar : MonoBehaviour
    5. {
    6.     [SerializeField]
    7.     private Slider slider;
    8.  
    9.     [SerializeField]
    10.     private Color low;
    11.  
    12.     [SerializeField]
    13.     private Color high;
    14.  
    15.     [SerializeField]
    16.     private Vector3 offset;
    17.  
    18.     private void Update()
    19.     {
    20.         slider.transform.position = Camera.main.WorldToScreenPoint(transform.parent.position + offset);
    21.     }
    22.  
    23.     public void SetHealth(float health)
    24.     {
    25.         slider.gameObject.SetActive(health < 100f);
    26.         slider.maxValue = 100f;
    27.         slider.value = health;
    28.         slider.fillRect.GetComponentInChildren<Image>().color = Color.Lerp(low, high, slider.normalizedValue);
    29.     }
    30. }
    31.  
    The hierarchy:


    The script is attached to the HealthBar objects.

    If a character stands still, a health bar is displayed perfectly fine above his head. But if the character is moving or rotating, the health bar starts misbehaving.



    I have no idea why it's not standing still above his head when simply rotating. Maybe it has something to do with the fact that the health bar is parented under the object that's rotating?

    Full code if needed: https://github.com/kostic017/ZombieShooter
     
  2. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    Exactly what you hypothesise, your body is rotating equally as fast as your update is running, I expect that to cause some problems. As such, just separate the two and you should be all good.
     
  3. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Unparent the health bar or use the LateUpdate function.
    Heres a short description of the use of LateUpdate and it seems perfect for your case.
     
  4. kostic017

    kostic017

    Joined:
    May 25, 2019
    Posts:
    9
    Unfortunately, I had to do both for the smoothest experience.
     
    Magiichan likes this.
  5. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    If this annoys you you could still keep the game objects organized!
    I'd do this by creating an empty gameobject named "Healthbars".
    When creating a health bar use Healthbars as parent.

    It's up to you how you wanna delete the health bar when the user is destroyed.
    I'd suggest listening to MonoBehaviour.OnDestroy on the player & subsequently destroy the health bar using a reference to the gameObject.