Alternative to the If/else block in OnAttack

I just wanted to share a different means of setting the IsAttacking property. The InputAction.CallbackContext struct contains the method ReadValueAsButton() which returns true if the button is pressed and false if not. Therefore you could do the following:

public void OnAttack(InputAction.CallbackContext context)
{
    IsAttacking = context.ReadValueAsButton();

    // Code from the lecture:
    // if (context.performed)
    // {
    //  IsAttacking = true;
    // }
    // else if (context.canceled)
    // {
    //  IsAttacking = false;
    // }
}

Whether you choose to use the approach used in the lecture or the code I shared above, It doesn’t really matter, they do the same thing. I just wanted to share in case there are people who are interested.

Cheers

2 Likes

You are a life saver! I couldn’t for the life of me get my code to work how the course showed. But this alternate way was able to allow me to continue.

Thanks tons!!!

Privacy & Terms