Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Lerp animation on health bar

Discussion in 'Scripting' started by Tiny-Tree, Aug 20, 2013.

  1. Tiny-Tree

    Tiny-Tree

    Joined:
    Dec 26, 2012
    Posts:
    1,226
    Hello, im stuck since this morning(in china) im trying to make my Health and shield bar Sliders move when there is damages. but i want to make the slider value moving smoothly with a lerp, but i cant find the proper way to do this, i feel im very close to, could someone check my script please?

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HealthBar : MonoBehaviour {
    5.  
    6.     public int CurHealth;
    7.     public int Maxhealth;
    8.     public int CurShield;
    9.     public int Maxshield;
    10.     GameObject CombatPanel;
    11.    
    12.     public float test;
    13.     int OldCurHealth;
    14.     int OldCurShield;
    15.    
    16.     //Bar Floats
    17.     public float HealthFloat;
    18.     public float ShieldFloat;
    19.     public float EaseTime;
    20.    
    21.     public bool EaseBool=false;
    22.    
    23.    
    24.     public void GetStats()
    25.     {
    26.     Maxhealth = CombatPanel.GetComponent<CombatManager>().MaxHealth;
    27.     Maxshield = CombatPanel.GetComponent<CombatManager>().ShieldMax;
    28.     CurHealth = CombatPanel.GetComponent<CombatManager>().Health;
    29.     CurShield = CombatPanel.GetComponent<CombatManager>().ShieldValue;
    30.  
    31.    
    32.     }
    33.     public void GetOldStats()
    34.     {
    35.         OldCurHealth = CombatPanel.GetComponent<CombatManager>().Health;
    36.         OldCurShield = CombatPanel.GetComponent<CombatManager>().ShieldValue;
    37.     }
    38.  
    39.     void Start()
    40.     {
    41.     CombatPanel =GameObject.FindGameObjectWithTag("CombatPanel");
    42.     GetStats ();
    43.         GetOldStats();
    44.     }
    45.    
    46.    
    47.     public void HealthChange()
    48.     {
    49.        
    50.         GetStats ();
    51.    
    52.        
    53.         //set Sliders (animate)
    54.         StartCoroutine("EaseBAR");
    55.        
    56.         GameObject.Find ("SliderSB").GetComponent<UISlider>().sliderValue = ShieldFloat;
    57.         GameObject.Find ("SliderHB").GetComponent<UISlider>().sliderValue = HealthFloat;
    58.         //get old values at the end to compare
    59.         GetOldStats ();
    60.     }
    61.    
    62.     // Update is called once per frame
    63.     void Update ()
    64.     {      
    65.         HealthChange ();
    66.        
    67.         if(EaseBool)
    68.         {
    69.             EaseTime=Time.deltaTime;
    70.             if(EaseTime>1)
    71.             {
    72.                 //stopeasing
    73.             }
    74.         }
    75.        
    76.     }
    77.    
    78.     void Curveanim()
    79.     {
    80.         //shield
    81.         ShieldFloat = ((float)CurShield)/((float)Maxshield);
    82.        
    83.         //health
    84.         HealthFloat = ((float)CurHealth)/((float)Maxhealth);
    85.        
    86.         //ease
    87.         test = Mathf.Lerp(1.0f,3.0f, 6.0f);// bug here: test stay to 0
    88.         GameObject.Find ("SliderSB").GetComponent<UISlider>().sliderValue = Mathf.Lerp(((float)OldCurShield),ShieldFloat, EaseTime);
    89.         GameObject.Find ("SliderHB").GetComponent<UISlider>().sliderValue = Mathf.Lerp(((float)OldCurHealth),HealthFloat, EaseTime);
    90.     }
    91.    
    92.    
    93.     IEnumerator EaseBAR()
    94.     {
    95.         EaseTime = 0;
    96.         //shield
    97.         ShieldFloat = ((float)CurShield)/((float)Maxshield);
    98.        
    99.         //health
    100.         HealthFloat = ((float)CurHealth)/((float)Maxhealth);
    101.        
    102.         //ease
    103.         test = Mathf.Lerp(1.0f,3.0f, Mathf.SmoothStep (0.0f,1.0f,EaseTime));// bug here: test stay to 0
    104.         GameObject.Find ("SliderSB").GetComponent<UISlider>().sliderValue = Mathf.Lerp(((float)OldCurShield),ShieldFloat, EaseTime);
    105.         GameObject.Find ("SliderHB").GetComponent<UISlider>().sliderValue = Mathf.Lerp(((float)OldCurHealth),HealthFloat, EaseTime);
    106.    
    107.         yield return new WaitForSeconds(EaseTime);
    108.     }
    109. }
    110.  
    im not doing code since longtime so im pretty bad at this, but i dont like to stuck on my problems:-?
     
  2. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    It looks like in the code bellow EaseTime is always 0. Try to change it to something more, for example 0.1f

    Code (csharp):
    1.  
    2.     IEnumerator EaseBAR()
    3.     {
    4.         EaseTime = 0;
    5.  
    6.         //shield
    7.         ShieldFloat = ((float)CurShield)/((float)Maxshield);
    8.  
    9.         //health
    10.         HealthFloat = ((float)CurHealth)/((float)Maxhealth);
    11.  
    12.         //ease
    13.         test = Mathf.Lerp(1.0f,3.0f, Mathf.SmoothStep (0.0f,1.0f,EaseTime));// bug here: test stay to 0
    14.         GameObject.Find ("SliderSB").GetComponent<UISlider>().sliderValue = Mathf.Lerp(((float)OldCurShield),ShieldFloat, EaseTime);
    15.         GameObject.Find ("SliderHB").GetComponent<UISlider>().sliderValue = Mathf.Lerp(((float)OldCurHealth),HealthFloat, EaseTime);
    16.  
    17.         yield return new WaitForSeconds(EaseTime);
    18.     }
    19.  
     
    Last edited: Aug 20, 2013
  3. Tiny-Tree

    Tiny-Tree

    Joined:
    Dec 26, 2012
    Posts:
    1,226
    i changed the value but it doesnt work too, there is something that dont ease my float, i cant find where
     
  4. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    Ok, the simpliest test. It should works well:
    Code (csharp):
    1.  
    2.         IEnumerator EaseBAR()
    3.         {
    4.             EaseTime = 0.1F;
    5.      
    6.             //ease
    7.             test = Mathf.Lerp([B]test[/B], 3.0f, EaseTime);// bug here: test stay to 0
    8.  
    9.             yield return new WaitForSeconds(EaseTime);
    10.         }
    11.  
    Mathf.Lerp have to recieve 3 variables: initialValue, targetValue and currentPosition (progress).
    You can use them in 2 ways:
    1)
    test = Mathf.Lerp(test, 3.0f, EaseTime);
    in this case EaseTime can be unchangable variable, because initialValue always increase (it is "test" variable)

    2)
    test = Mathf.Lerp(0.0f, 3.0f, EaseTime);
    in this case initialValue and targetValue is constant, and EaseTime must increase from 0.0f to 1.0f (EaseTime can not be unchangable).
     
  5. Tiny-Tree

    Tiny-Tree

    Joined:
    Dec 26, 2012
    Posts:
    1,226
    thank you for the answer, the first case worked but i cant use it on my case because:

    my function work like this each time receive damages, I move my float from OldCurHealth to HealthFloat during Easetime seconds

    maybe its an other function ? i tried lerp and step but cant move it the way i want

    for exemple something like
    Code (csharp):
    1. myfloat = Easefunction(0,1,5 )//this will move my float from 0 to 1 in 5 seconds

    Code (csharp):
    1.     IEnumerator EaseBAR()
    2.     {
    3.             //health
    4.             HealthFloat = ((float)CurHealth)/((float)Maxhealth);
    5.        
    6.             //ease
    7.             GameObject.Find ("SliderHB").GetComponent<UISlider>().sliderValue = Mathf.Lerp(((float)OldCurHealth),HealthFloat, EaseTime);
    8. //need to move the float "SliderValue" from "OCurHealth" to "HealthFloat" in xxseconds(Easetime)
    9.             yield return new WaitForSeconds(EaseTime);
    10.     }
     
  6. colourcrisis

    colourcrisis

    Joined:
    Apr 12, 2018
    Posts:
    1
    I know it's old, but I'm just adding this here for everyone's reference. The couroutine is written wrong.

    It should be StartCoroutine(EaseBAR()); not StartCoroutine("EaseBAR");
     
  7. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    870
    In respect of starting the Coroutine both are identical, the only difference is in the available method you'd use to stop them.

    This seems to have been a bit of a pointless necro.
     
    DoublePixelStudio likes this.