I am trying to build a progress bar that indicates the completion of the current level from 0 to 100% (or from 0 to 1 in probability). I found this example:
It works, but my progress bar stops at about 75% when i reach the target position and then it drops down. This is my code:
public GameObject player;
public GameObject target;
public Text scoreText;
private float startDistance;
private float currentDistance;
private void Start()
{
this.startDistance = Vector3.Distance(player.transform.position, target.transform.position);
}
private void Update()
{
this.currentDistance = Vector3.Distance(player.transform.position, target.transform.position);
float percentage = 1 - (currentDistance / startDistance);
transform.localScale = new Vector3(percentage, 1f, 1f);
this.scoreText.text = string.Format("Score: {0}%", Mathf.Round(percentage * 100f));
Debug.Log("Percentage Completed: " + (percentage));
}
The player moves by himself towards the target position at constant speed without acceleration. The player can move its character left or right in order to avoid obstacles. This doesn't change the player's speed, it just maneuvers the direction. Maneuvering, I realized the progress sometimes goes beyond 75% and sometimes below. Why is this happening? How to make the progress simply reach 100% when I reach the target position?