Hello, I am currently a beginner in Unity scripting. I am trying to make a simple minigame. So, my problem is about how to split progress bar into three checkpoint where each checkpoint need to be pressed in order to progress. I did follow youtube tutorial and managed to make the bar filled to the end.
Here is the code
public Slider ManaBar;
private float mana = 20f;
public float currentMana;
private float maxManaOne = 30f;
//private float maxManaTwo = 60f;
public float maxMana = 1f;
public bool Active = true;
// Update is called once per frame
void Update()
{
//if (Input.GetKeyDown(KeyCode.Space))
{
if(currentMana != maxManaOne)
{
currentMana += mana * Time.deltaTime;
ManaBar.value = currentMana / maxManaOne;
}
}
}
public Slider ManaBar;
public float ManaRegenerationRate = 1f; // 1 unit / second
public List manaSteps = new List(){ 10, 20, 30 }; // Change values in inspector, 30 being the max mana value here, make sure the values are sorted
public KeyCode NextStepKey = KeyCode.Space;
private float mana;
private float MaxMana => manaSteps[manaSteps.Count - 1];
void Update()
{
float targetMana = GetTargetMana();
mana = Mathf.MoveTowards(mana, targetMana, ManaRegenerationRate * Time.deltaTime);
ManaBar.value = mana / MaxMana;
}
private float GetTargetMana()
{
// Loop through all the *intermediate* steps
for (int i = 0 ; i < manaSteps.Count - 1 ; i++)
{
// If step is reached
// return next step if key is pressed, or step itself otherwise
if (Mathf.Approximately(mana, manaSteps*))*
return Input.GetKeyDown(NextStepKey) ? manaSteps[i + 1] : manaSteps*;*
// If next step detected, return it
if (mana < manaSteps*)*
return manaSteps*;*
}
// Fallback: return last step = max mana
return MaxMana;
}