1
\$\begingroup\$

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:

https://stackoverflow.com/questions/34960494/distance-from-start-point-to-end-point-in-percentage-in-update

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?

\$\endgroup\$
  • 1
    \$\begingroup\$ It sounds like your distance never reaches zero. Have you verified that your player is reaching the target position on every axis, or tried zeroing out the component of the separation that's parallel to the camera's view axis? \$\endgroup\$ – DMGregory Oct 13 at 18:23
  • \$\begingroup\$ @DMGregory it seems like it never reached the target because it was a game object. But when i added a cube as the target, the progress was correct. Do you know why? Why does an empty game object react differently to a cube? \$\endgroup\$ – busuu Oct 14 at 19:24
  • \$\begingroup\$ There is no fundamental difference between an empty game object and a cube — they both have a transform, and that's all you're using here. So the change from an empty object to a cube was not itself the fix. But you did something different when you placed your cube (maybe placed it on the same plane as the player character, rather than separated from it on the z axis?) which fixed the issue as a side effect. \$\endgroup\$ – DMGregory Oct 14 at 19:28
0
\$\begingroup\$

My target element was an empty game object that had children of cubes where the progress went to around 75% and stopped. Then I created a cube that had children of cubes and the progress worked fine. I don't know why is this the case but seems like an empty game object and a cube react differently.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.