Home > Blockchain >  (Unity) Progress bar appearing backwards
(Unity) Progress bar appearing backwards

Time:06-01

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 rather than going backwards.

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);

(The X pivot point is set to 0)

How can I make it go the correct direction?

CodePudding user response:

You can invert the value by subtracting it from 1.

var progress = 1 - (playerNEnd / startNEnd);

This works for values that are constrained between 0 and 1.

1 - 0.75 = 0.25
1 - 0.50 = 0.50
1 - 0.25 = 0.75

CodePudding user response:

You could try linear interpolation for better control over the values.

float progress = Mathf.Lerp(1f, 0f, playerNEnd / startNEnd);
progressBar.transform.localScale = new Vector3(progress, 1, 1);
  • Related