Search Unity

Bar starting at 0 and going to 100.

Discussion in 'Getting Started' started by MCamac55, Jan 21, 2015.

  1. MCamac55

    MCamac55

    Joined:
    Jan 8, 2015
    Posts:
    22
    How would I create a hunger bar that would go from 0-100 in a matter of like 1 minute?
     
  2. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,628
    you need a start time, and the current time minus the start time to see how long has passed, then scale that number down to fit within like a range of 0..1 or 0..pixel width or something... then use that to scale your `bar` object.
     
  3. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,048
    I have maked bar with UI Button (Image). Then delete button script. Image typ set to filled (make Metod, Origin as you need). Now you should only change Fill Amount for working bar. Make c# script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.EventSystems;
    4. using UnityEngine.UI;
    5.  
    6. public class Hunger : MonoBehaviour {
    7.     float HungerCurrent =1f;
    8.     float HungerMax =60f;
    9.     public float HungerChange;
    10.     Image image;
    11.  
    12. void Start () {
    13.         image = GetComponent <Image> ();
    14. }
    15.     void Update () {
    16. if (HungerCurrent <60) {
    17.        HungerCurrent += Time.deltaTime;
    18.         HungerChange = HungerCurrent / HungerMax;
    19.         image.fillAmount = HungerChange ;
    20.     }
    21.     }
    22. }
    23.  
    i didnot tested skript, may be somewhere is one bug or two :)