Reputation: 79
I have 4 "mana balls". They are 4 individual game objects. After player casted a spell, one magic ball is spent and so on until he has spent all 4 magic balls. I want to develop a recharging system. When player casted a spell and first mana ball was spent, it needs to recharge again after 10 sec. If he casted again, second mana ball was spent and first cancels its recharging, and recharging of second mana ball began. After 10 seconds, second mana ball has recharged, and then begins next 10 seconds of first mana ball recharging.
I have code for spending mana balls:
private void ManaBall()
{
if(finishMagic == true && manaball4 == false && manaball3 == false)
{
manaBall4.SetActive(false); // Mana ball sprite is deactivated
manaball4 = true; // Boolean to confirm this mana ball was deactivated (for Ifs)
manaBallEmpty4.SetActive(true); // Empty space sprite instead of mana ball is activated
}
else if (finishMagic == true && manaball4 == true && manaball3 == false)
{
manaBall3.SetActive(false);
manaball3 = true;
manaBallEmpty3.SetActive(true);
}
}
I have reversed code for recharging mana balls:
private void ManaReload()
{
if (manaball4 == true && manaball3 == false)
{
manaBallEmpty4.SetActive(false);
manaball4 = false;
manaBall4.SetActive(true);
}
else if (manaball4 == true && manaball3 == true)
{
manaBallEmpty3.SetActive(false);
manaball3 = false;
manaBall3.SetActive(true);
}
I've tried creating coroutine which checks if any mana balls were spent after player casted a spell, and if it was, it started recharging it. But everytime I casted a spell, that function would start, giving shorter intervals than 10 seconds in between.
I need some advice where or how to put recharging function in code.
Coroutine I've tried:
IEnumerator FinishSpellAttack()
{
finishMagic = true;
ManaBall();
yield return new WaitForSeconds(1f);
finishMagic = false;
yield return new WaitForSeconds(10f);
ManaReload();
}
Upvotes: 1
Views: 236
Reputation: 998
Perhaps you should have an array of the mana ball bools. If you use one, set the last true element to false. Then have one method that counts up and sets the first false element back to true.
I might do something like this :
bool[] manaBalls = { true, true, true, true };
bool usingCoroutine = false;
void UseMana() {
for(int i = manaBalls.Length - 1; i >= 0; i--) {
if(manaBalls[i]) {
manaBalls[i] = false;
if(!usingCoroutine) {
StartCoroutine("Timer")
}
break;
}
}
}
void ReplenishMana() {
for(int i = 0; i < manaBalls.Length; i++) {
if(!manaBalls[i]) {
manaBalls[i] = true;
if(i == manaBalls.Length - 1) {
StopCoroutine("Timer");
}
return;
}
}
}
You'll want to spice this up and add the timer in appropriately. This is psuedu ish code.
UseMana would go from the end of the array to the front. The first unused one it finds gets used. ReplenishMana does the oppisite. It goes from front to back finding the first false element and sets it back to true. Then it checks if that's the end of the array, if it is, stop the coroutine.
Upvotes: 1
Reputation: 23
sometimes try Invoke() method for creating delays.
https://docs.unity3d.com/ScriptReference/MonoBehaviour.Invoke.html
Upvotes: 1