::Hi Vĩ,
You need to add an additional parameter, like so:
IEnumerator CastCoroutine()
{
anim.SetBool("Casting", true);
yield return new WaitForSeconds(0.15f);
//side cast
if (yAxis == 0 || (yAxis < 0 && Grounded()))
{
GameObject _fireBall = Instantiate(sideSpellFireball, SideAttackTransform.position, Quaternion.identity);
//flip fireball
if(pState.lookingRight)
{
_fireBall.transform.eulerAngles = Vector3.zero; // if facing right, fireball continues as per normal
}
else
{
_fireBall.transform.eulerAngles = new Vector2(_fireBall.transform.eulerAngles.x, 180);
//if not facing right, rotate the fireball 180 deg
}
pState.recoilingX = true;
anim.SetInteger("Direction", 1);
}
//up cast
else if( yAxis > 0)
{
anim.SetInteger("Direction", 0);
Instantiate(upSpellExplosion, transform);
rb.velocity = Vector2.zero;
}
//down cast
else if(yAxis < 0 && !Grounded())
{
downSpellFireball.SetActive(true);
}
Mana -= manaSpellCost;
yield return new WaitForSeconds(0.35f);
anim.SetBool("Casting", false);
pState.casting = false;
}
Then, add an extra spell animation state on the Animator, and set your transitions to both spell states as such:
- One of them will trigger when Casting is true and Direction equals 0
- The other will trigger when Casting is true and Direction equals 1
I make Direction an integer instead of a boolean so that if you want to add more animation variants in future. You can do it.
Hope this is clear!