- Home /
Progress bar for player progress is backwards
I'm trying to create a progress bar that shows how close the player is to the finish in a 3D area, but I can't figure out how to make it go in the correct direction.
startNEnd = Vector3.Distance(start.transform.position, GameObject.Find("GoalPrefab(Clone)").transform.position);
playerNEnd = Vector3.Distance(player.transform.position, GameObject.Find("GoalPrefab(Clone)").transform.position);
progressBar.transform.localScale = new Vector3(playerNEnd/startNEnd, 1, 1);
Answer by TheTrueDuck · Jun 03 at 12:02 PM
You are checking the distance of the player to the goal, but the Vector3.Distance() value will get smaller when your player advances closer to the goal, which is why it appears inverted. Try something along the lines of this:
float startingDistanceFromGoal = Vector3.Distance(start.transform.position, GameObject.Find("GoalPrefab(Clone)").transform.position);
float playerDistanceFromGoal = Vector3.Distance(player.transform.position, GameObject.Find("GoalPrefab(Clone)").transform.position);
float percentageComplete = 1 - (playerDistanceFromGoal / startingDistanceFromGoal);
progressBar.transform.localScale = new Vector3(percentageComplete, 1, 1);
You will probably also want to clamp this value between zero and one with Mathf.Clamp01() to avoid the number going negative.
Depending on how big your progress bar is, you may also want to multiply this percentage before setting it to the transform.
You probably also don't need to check what the distance from the starting point to the goal is every frame, as I expect this will stay constant while playing, so consider just calculating it once at the start, and then using that value from then on. Hope this helps! :)