I'm trying to subtract health in a turn based battle system that I've been working on recently and whenever I would try and subtract player health in a specific enum/state in state machine (housed within update) only once but every time I would run the code the variable would subtract multiple times, but my function for subtracting Enemy Health would subtract once (as it should) since it was housed within a Task On Click function. Is there any way I can subtract Player Health within my state machine without having it repeat like this? I'm still fairly new to coding in Unity so any help I can get would be very appreciated. Thank you!
Code for reference:
public void TaskOnClick()
{
if (currentState == BattleStates.PLAYERCHOICE)
{
currentState = BattleStates.PLAYERATTACK;
StartCoroutine(PlayerAttack());
StartCoroutine(SwitchStates1());;
}
}
public IEnumerator SwitchStates1()
{
yield return new WaitForSeconds(2);
Player1AttackingOver = true;
yield return new WaitForSeconds(0.5f);
currentState = BattleStates.ENEMYCHOICE;
}
public IEnumerator EnemyAttack()
{
yield return new WaitForSeconds(0.5f);
enemy1.transform.position = EnemyAttackPosition;
spriteChange.playerHP -= 25;
yield return new WaitForSeconds(1f);
enemy1.transform.position = EnemyPosition;
yield return new WaitForSeconds(1f);
}
public enum BattleStates
{
BATTLESTART,
PLAYERCHOICE,
PLAYERATTACK,
ENEMYCHOICE,
ENEMYATTACK,
LOSE,
WIN
}
void Update()
{
M_ATTACK.onClick.AddListener(TaskOnClick);
Debug.Log(currentState);
Debug.Log(Values.EnemyHealth);
Debug.Log(spriteChange.playerHP);
switch (currentState)
{
case (BattleStates.BATTLESTART):
StartCoroutine(BattleWaitStart());
break;
case (BattleStates.PLAYERCHOICE):
break;
case (BattleStates.PLAYERATTACK):
UI.GetComponent().enabled = true;
break;
case (BattleStates.ENEMYCHOICE):
Player1AttackingOver = false;
StartCoroutine(EnemyAttack());
break;
case (BattleStates.ENEMYATTACK):
break;
case (BattleStates.LOSE):
break;
case (BattleStates.WIN):
break;
}
}
↧