Search Unity

is it possible to make health bar gradually decrease without using the Update method?

Discussion in 'Scripting' started by Pixitales, Jul 28, 2019.

  1. Pixitales

    Pixitales

    Joined:
    Oct 24, 2018
    Posts:
    227
    Whenever I takedamage, I call game event listener. CharacterStats is a scriptable object.

    Here is my simple health bar script:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class PlayerHealth : MonoBehaviour
    6. {
    7.     private Image content;
    8.  
    9.     [SerializeField]
    10.     private CharacterStats playerHealth;
    11.  
    12.     void Start()
    13.     {
    14.         content = GetComponent<Image>();
    15.  
    16.         content.fillAmount = playerHealth.CurrentHealth / playerHealth.maxHealth;
    17.     }
    18.  
    19.     public void UpdateHealth()
    20.     {
    21.         content.fillAmount = Mathf.Clamp01(playerHealth.CurrentHealth / playerHealth.maxHealth);
    22.     }
    23. }
    24.  
    Is there a way to make health bar gradually decrease without using Update method? I am trying to improve performance of my game because it kinda lags.
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    I would use the profiler to weed out where performance problems are. Updating a health bar should not be a problem. Also premature optimization is not advisable, you will waste more time looking for issues that take away from building the actual game.
     
    Joe-Censored likes this.
  3. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,913
    Try putting UI elements that move or are updated onto their own Canvas. Floating health numbers will cause a redraw of the entire UI for example.
     
    Vryken and Ryiah like this.
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Well, you should only change the fill amount when the value actually changes (i.e. save the last value, and compare it with current). Setting a canvas element's value always Forces a complete redraw.

    Note, however, that before you start optimizing, first run the profiler to make sure that you are tackling the real performance killers.
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Removing the Update method will not improve performance, that's not why the game lags.

    Use the profiler.
     
    Vryken likes this.
  6. GeorgeCH

    GeorgeCH

    Joined:
    Oct 5, 2016
    Posts:
    222
    Basically, you can minimize the performance impact of your Update() operation by making sure that it only ever does what it should do under certain conditions, and does nothing otherwise.

    Here is a simple UI script that checks with a PlayerController class (or wherever you're getting the progress bar value from) for its current value every frame but only ever updates the progress bar if the value has changed from the previous frame:

    Code (CSharp):
    1. using UnityEngine.EventSystems;
    2. using UnityEngine.UI;
    3.  
    4. public class ProgressBar : Monobehavior
    5. {
    6.  
    7.     private PlayerController playerController;
    8.  
    9.     private Image bar;
    10.  
    11.     private void Awake()
    12.     {
    13.         bar = GetComponent<Image>();
    14.         playerController = FindObjectOfType<PlayerController>();
    15.     }
    16.  
    17.     private void Update()
    18.     {
    19.         if (playerController.playerHealthPercentage == bar.fillAmount)
    20.         {
    21.             return;
    22.         }
    23.    
    24.         bar.fillAmount = playerController.playerHealthPercentage;
    25.     }
    26. }
    That said, I second all the other posters: updating a progress bar should not have any noticeable impact on your performance. If this is what tips the game over from choppy to smooth FPS, then you've got much bigger problems to worry about.
     
  7. Pixitales

    Pixitales

    Joined:
    Oct 24, 2018
    Posts:
    227
    There are over 300 gameobjects checking for collisions so thats why it lags
     
  8. Pixitales

    Pixitales

    Joined:
    Oct 24, 2018
    Posts:
    227
    Oh ok and i already used profiler before.
     
  9. Pixitales

    Pixitales

    Joined:
    Oct 24, 2018
    Posts:
    227
    It doesnt lag in the beginning until more and more gameobjects spawn and checking collision.
     
  10. Pixitales

    Pixitales

    Joined:
    Oct 24, 2018
    Posts:
    227
    Thanks everyone for ur answer. I will try to use the update method.
     
  11. GeorgeCH

    GeorgeCH

    Joined:
    Oct 5, 2016
    Posts:
    222
    With so many objects, try using events instead.
     
  12. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    That can be problematic. I've done several things in my game with high physics CPU usage to get it under control.

    1) Make sure all colliders are primitives, not mesh colliders. Use so called "compound colliders" of multiple primitives to approximate the object's shape if a single primitive collider won't do.

    2) If the objects are on somewhat of a flat plane yet spread over a wide area, switch to "Multibox Pruning Broadphase" which chops up your game world into smaller chunks and checks for collisions within each chunk separately instead of comparing every collider to every collider.
    https://docs.unity3d.com/Manual/class-PhysicsManager.html

    3) If the GameObjects aren't moving particularly fast, or the physics simulation doesn't need to be very exact, consider changing the Fixed Timestep so physics updates occur less frequently. By default physics is updated 50 times per second, which could be overkill for your game.
    https://docs.unity3d.com/Manual/class-TimeManager.html
     
  13. Pixitales

    Pixitales

    Joined:
    Oct 24, 2018
    Posts:
    227
    Ooh i havent tried that. Its a 2d top down zombie game. More zombies the better. They need to check for wall collision and turn in the opposite direction. They all have kinematic rigid bodies and box colliders. Some zombies are fast moving.
     
  14. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Just do profiling, make a change, profile again, and see if things improve or not. Things like #2 and #3 above are just simple settings changes you can undo at any time, so pretty easy to tweak and look for improvement. My game is a lot different than yours, but I found 25 physics updates a second to be the sweet spot for me, which cut physics CPU usage by nearly half on its own (Fixed Timestep changed to 0.04). YMMV