Progress Bar and lerps
Hey My game has a Progress Bar and its fill rate depends on LERPS. Now the player has 5 choices to choose his pace of progress. I want everyone to get from point A to point B at the same time but their progress allowance will depend on the function Attaching script Thank you !!
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class LerpsScript: MonoBehaviour {
public Button Button;
public Image progressBar;
public Text txtAmount;
public float minimum = 0.0F;
public float maximum = 1.0F;
public int switchFlag;
public float workTime = 2.0f;
bool doProgress = false;
public float elapsedTime = 0f;
public void OnClickButton()
{
elapsedTime = 0;//when the progress bar is 0%
progressBar.fillAmount = 0;
doProgress = true;// start 2 run
StartCoroutine(IEnum());
}
IEnumerator IEnum()
{
while (doProgress)
{
if (progressBar.fillAmount == 1.0f) // finish the progress bar 100 %
{
doProgress = false; //stop 2 run
progressBar.fillAmount = 0; // update
}
else
{
if (switchFlag == 0)
{
elapsedTime += Time.deltaTime/ workTime;
float curr = Mathf.Sin(elapsedTime * Mathf.PI * 0.5f);
progressBar.fillAmount += Mathf.Lerp(minimum, maximum, curr);
}
if (switchFlag == 1)
{
elapsedTime += Time.deltaTime / workTime;
float curr = Mathf.Cos(elapsedTime * Mathf.PI * 0.5f); // curr=current
progressBar.fillAmount += Mathf.Lerp(minimum, maximum, curr);
}
if (switchFlag == 2)
{
elapsedTime += Time.deltaTime / workTime;
float curr= elapsedTime * elapsedTime;
progressBar.fillAmount += Mathf.Lerp(minimum, maximum, curr);
}
if (switchFlag == 3)
{
elapsedTime += Time.deltaTime/ workTime;
float curr = elapsedTime * elapsedTime * (3f - 2f * elapsedTime);
progressBar.fillAmount += Mathf.Lerp(minimum, maximum, curr);
}
if(switchFlag==4)
{
//elapsedTime += Time.deltaTime / workTime;
//float curr = elapsedTime * elapsedTime * elapsedTime * (elapsedTime * (6f * elapsedTime - 15f) + 10f);
//progressBar.fillAmount += Mathf.Lerp(progressBar.fillAmount, maximum, curr);
elapsedTime += Time.deltaTime/workTime;
float curr = elapsedTime;
progressBar.fillAmount = Mathf.Lerp(minimum, maximum, curr);
}
}
yield return new WaitForSeconds(0.1f);
}
}
public void OnValueChanged(Dropdown dropDown)
{
doProgress = false;
progressBar.fillAmount = 0;
StopCoroutine(IEnum());
switch (dropDown.value)
{
case 0:
Debug.Log("Yoho!");
switchFlag = 0;
break;
case 1:
Debug.Log("Wow!");
switchFlag = 1;
break;
case 2:
Debug.Log("Bla!");
switchFlag = 2;
break;
case 3:
Debug.Log("Whoo!");
switchFlag = 3;
break;
case 4:
Debug.Log("Good!");
switchFlag = 4;
break;
default:
Debug.Log("WTF!");
break;
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612161301im_/https://answers.unity.com/themes/thub/images/avi.jpg)