Skip to main content

You could make use of Combining Enumerator values with Bit-wise OperationsCombining Enumerator values with Bit-wise Operations, that way you can define a set of actions, and combine them on the same Enum value, and then check if a certain flag was activated. I use this for Animator controller operations, since some states may depend on multiple states to be on.

You could make use of Combining Enumerator values with Bit-wise Operations, that way you can define a set of actions, and combine them on the same Enum value, and then check if a certain flag was activated. I use this for Animator controller operations, since some states may depend on multiple states to be on.

You could make use of Combining Enumerator values with Bit-wise Operations, that way you can define a set of actions, and combine them on the same Enum value, and then check if a certain flag was activated. I use this for Animator controller operations, since some states may depend on multiple states to be on.

Source Link
LifGwaethrakindo
  • 1.4k
  • 2
  • 15
  • 27

You could make use of Combining Enumerator values with Bit-wise Operations, that way you can define a set of actions, and combine them on the same Enum value, and then check if a certain flag was activated. I use this for Animator controller operations, since some states may depend on multiple states to be on.

As for the combo checking, you could make use of co-routines to have a timing for combo checking, as already suggested. Example (I'll assume you've read the page I attached):

public enum PlayerActions
{
    Idle = 0,
    Attack = 1,
    Jump = 2,
    Defend = 4,
    SpecialAttack = 8,
    Die = 16
}

public class Player : MonoBehaviour
{
    public Coroutine comboCheck;
    public float comboWait;
    public KeyCode attackKey; 
    public KeyCode jumpKey; 
    private PlayerActions actions;
    private Animator animator;

    void Awake()
    {
        animator = GetComponent<Animator>();
    }

    void Update()
    {
        TrackActions();
        UpdateAnimator();
    }

    void UpdateAnimator()
    {
        animator.SetBool("Attack", (actions & PlayerActions.Attack) == PlayerActions.Attack);
        animator.SetBool("Jump", (actions & PlayerActions.Jump) == PlayerActions.Jump);
        animator.SetBool("Defend", (actions & PlayerActions.Defend) == PlayerActions.Defend);
        animator.SetBool("SpecialAttack", (actions & PlayerActions.SpecialAttack) == PlayerActions.SpecialAttack);
        animator.SetBool("Die", (actions & PlayerActions.Die) == PlayerActions.Die);
    }

    void TrackActions()
    {
        if(Input.GetKey(attackKey)) actions |= PlayerActions.Attack;
        else if((actions & PlayerActions.Attack) == PlayerActions.Attack && comboCheck == null) actions ^= PlayerActions.Attack;

        if(Input.GetKey(jumpKey)) actions |= PlayerActions.Jump;
        else if((actions & PlayerActions.Jump) == PlayerActions.Jump && comboCheck == null) actions ^= PlayerActions.Jump;

        if(comboCheck == null) comboCheck = StartCoroutine(ComboCheck());
    }

    IEnumerator ComboCheck()
    {
        WaitForSeconds wait = new WaitForSeconds(comboWait);
        yield return wait;

        // Your combo criteria:
        if((actions & PlayerActions.Jump) == PlayerActions.Jump && (actions & PlayerActions.Attack) == PlayerActions.Attack)
        {
            // Do some cool Jump + Attack wombo combo...
        }
    }
}

NOTE: The bit-wise operation enums are a suggestion to keep track of multiple states under just one variable, it is cheaper in memory, and easier to maintain than having n quantity of bools for n quantity of states. So, bools would also make the trick.

Hope it helps.