Search Unity

Progress bar Help

Discussion in 'Scripting' started by Fekzh21, Mar 21, 2019.

  1. Fekzh21

    Fekzh21

    Joined:
    Dec 29, 2018
    Posts:
    18
    progress bar

    How could you create a progress bar that takes the distance between one object and another

    that when the player is approaching a certain object, the bar is filled
     
  2. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    782
    I guess the first step is to figure out the distance between the player and the object.
    This may do:
    Code (CSharp):
    1. float distance = Vector3.Distance(player.transform.position, object.transform.position);
    If it's in the Update () method it will be updated on each frame.
     
  3. jasrei

    jasrei

    Joined:
    Apr 19, 2018
    Posts:
    15
    You could determine at what distance the bar should be full, use Vector3.Distance to find the distance. If the distance is less than the "full" distance, divide the distance by the "full" distance to get a number between 0 and 1. Set your progress bar min to 0, max to 1 and value to the number you get.

    Not at a computer to fully test this out, but that's the general idea.