Search Unity

controlling speed and moving health bar smoothly

Discussion in 'Scripting' started by sharpg, Apr 1, 2015.

  1. sharpg

    sharpg

    Joined:
    Mar 10, 2015
    Posts:
    150
    hello guys,
    i am using following script to corntrol the health of my object.i am cleaning wound of my object with cotton.once i touch my wound , health bar increases from 0 to .10% but with jerk.i wan to control the speed of progression of my health bar so it move smoothly not with jerk.
    Please help.
    Code (csharp):
    1.  
    2. usingUnityEngine;
    3. usingSystem.Collections;
    4. usingUnityEngine.UI;
    5. publicclassCleanWound : MonoBehaviour {
    6.  
    7. public Image hBar;
    8.  
    9. voidStart()
    10. {
    11. hBar.fillAmount =0;
    12. }
    13. voidOnTriggerEnter2D (Collider2Dother){
    14.  
    15.  
    16.  
    17. if(other.tag =="Cotton"){
    18.  
    19. renderer.material.color = Color.Lerp(renderer.material.color,Color.clear,Time.deltaTime*11f);
    20.  
    21. hBar.fillAmount = hBar.fillAmount + .10f;
    22.  
    23. if(hBar.fillAmount==0.3f || hBar.fillAmount==0.6f || hBar.fillAmount==1f )
    24. {
    25.  
    26. Destroy(gameObject);
    27.  
    28. }
    29.  
    30. }
    31. }
    32. }
    33.  
     
  2. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    729
    Well you're just using + which will make it jump. To do this properly you can do something like this.

    Code (csharp):
    1.  
    2. public float curHp;
    3. public float tempHp;
    4.  
    5. void Update () {
    6.  
    7.     if(tempHp < curHp) {
    8.  
    9.         tempHp += 0.1;
    10.  
    11.     }
    12.  
    13. }
    14.  
    15. void OnTriggerEnter2D (Collider2Dother){
    16.  
    17.  
    18.     if(other.tag =="Cotton"){
    19.      
    20.         curHp += 1;
    21.  
    22.     }
    23. }
    24.  
    25.  
    In this case you want the tempHp to be the value that's displayed.
     
  3. sharpg

    sharpg

    Joined:
    Mar 10, 2015
    Posts:
    150
    No sir Problem not solved.helth bar is still jumping not moving smoothly.please help.
     
  4. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    729
    It should be working just fine. Are you using a temp variable in the bar display? Also you could change 0.1 to something lower like 0.001 instead.
     
  5. sharpg

    sharpg

    Joined:
    Mar 10, 2015
    Posts:
    150
    Thank you very much sir I do following and problem solved.
    Code (csharp):
    1.  
    2.  
    3. boolhDown=false;
    4.  
    5. publicGameObjectpart;
    6. publicImagehbFront;
    7. publicfloatcurHp;
    8. publicfloattempHp;
    9.  
    10.  
    11.  
    12. voidStart()
    13.  {
    14. hbFront.fillAmount=0;
    15.  }
    16. voidUpdate()
    17.  {
    18.  
    19. if (tempHp<curHp && hDown==true)
    20.  {
    21. tempHp += 0.008f;
    22. hbFront.fillAmount=tempHp;
    23.  }
    24.  
    25.  
    26.  }
    27.  
    28.  
    29. void OnTriggerExit2D()
    30.  {
    31. hDown=false;
    32.  }
    33. void OnTriggerEnter2D(Collider2Dother)
    34.  {
    35. hDown=true;
    36.  
    37. if(other.collider2D.tag=="Towel" && other.collider2D.tag !="ThornUI")
    38.  {
    39. renderer.material.color =Color.Lerp (renderer.material.color, Color.clear,Time.deltaTime*11f);
    40.  
    41. curHp=1f;
    42.  
    43. Update();
    44.  
    45.  
    46.  
    47. if (hbFront.fillAmount==1)
    48.  {
    49. part.SetActive(true);
    50.  }
    51.  
    52.  
    53.  }
    54.  }
    55.  
    56.  
    57.  
    58. }
    59.  
     
  6. Gamba

    Gamba

    Joined:
    Feb 8, 2015
    Posts:
    29
    Why don't you just lerp like you did for the material color?
     
  7. sharpg

    sharpg

    Joined:
    Mar 10, 2015
    Posts:
    150
     
  8. Gamba

    Gamba

    Joined:
    Feb 8, 2015
    Posts:
    29
    I've never actually used Color.Lerp(), but try it without the damping.

    Code (csharp):
    1. private Color startColor = renderer.material.color;
    2.  
    3. private void Update()
    4. {
    5.     float normalizedTime = currentTime / totalTime;
    6.     renderer.material.color = Color.Lerp( startColor, Color.clear, normalizedTime );
    7. }
    Just remember that this creates an instance of the material.
     
    sharpg likes this.
  9. sharpg

    sharpg

    Joined:
    Mar 10, 2015
    Posts:
    150
    Thanks Sir,
    Can u please tell me how can i paint or draw a line with mouse or something like give nail polish effect to nail objects?
     
  10. Gamba

    Gamba

    Joined:
    Feb 8, 2015
    Posts:
    29
    Check out LineRenderer. You give it a start and end point (or multiple points to create segments), and a width. It's always billboarded though.

    If you're just doing 2D, you could try using two Image components, one as a parent object with a mask component, and the other as a child.

    I've never had to solve that problem in Unity so I'm sure there could be other ways as well.
     
  11. sharpg

    sharpg

    Joined:
    Mar 10, 2015
    Posts:
    150
    Thanks Sir, I am using only 2d.