#Unity's Input System and detecting when the button is no longer pressed.

1 messages · Page 1 of 1 (latest)

charred fjord
#

I am setting up an NES-style brawler. I have a boolean for my animation called "attacking" as I want this to function as though a turbo button is enabled. If the button is held down, just keep attacking.

I am at a loss as to how to recognize when the player stops pressing the attack button.

I am still learning the input system, and I don't truly understand it. Reading the attack button when depressed is working. Bimmy does start punching. He also starts and stops moving with the proper animation. However, I cannot seem to stop the attack loop.

public class Bimmy : BoxCharacter
{
    ...
    public InputActionReference attack;
    ...
    void OnEnable()
    {
        attack.action.started += Attack;
        ...
    }
    void OnDisable()
    {
        attack.action.started -= Attack;
        // Animator().SetBool(Anim.attacking, false); // I tried this, but it didn't work. I thought this is where the input is no longer depressed, read diabled. This clearly is not the case.
        ...
    }
    void Attack(InputAction.CallbackContext obj)
    {
        Animator().SetBool(Anim.attacking, true);
    }
}
steep pollen
#

you've only subscribed to the started event. you probably want to subscribe to the canceled event as well

charred fjord
#

ah, I see. On the attack.action.canceled ?

#

I will look into it, thank you.

charred fjord
#

Okay, now I am coming across something interesting. There is an ~3 second delay. I must have done something wrong. I went through and added an else if to my update loop.

private void Update()
    {
        _moveDirection = move.action.ReadValue<Vector2>();
        if(
            (MoveVector().x != 0 || MoveVector().y != 0) && 
            !Animator().GetBool(Anim.attacking)
        )
        {
            SpriteRenderer().flipX = MoveVector().x < 0;
            Animator().SetBool(Anim.walking, true);
            LinearVelocity(new Vector2
            (
                MoveVector().x * data.movementVelocity.x,
                MoveVector().y * data.movementVelocity.y
            ));
        }
        else if(attack.action.WasCompletedThisFrame() && combo == 0)
        {
            Animator().SetBool(Anim.attacking, false);
        }
        else
        {
            Animator().SetBool(Anim.walking, false);
            LinearVelocity(new Vector2(0.0f, 0.0f));
        }
    }

So then I thought, if I am looking for this eventuality in the loop, it is probably taking a few moments to catch up. I should set the action as performed as soon as possible.

    void Attack(InputAction.CallbackContext obj)
    {
        if(obj.performed)
        {
            Animator().SetBool(Anim.attacking, false);
        }
        else
        {
            Animator().SetBool(Anim.attacking, true);
        }
    }

This didn't work either. Now I cannot go into the attacking == true state. I know the Action.performed is a boolean inside a struct, and I admit I have not had to use structs before, but it seems like this check might be necessary to work.

Is there something I am missing about structs or is there another concept I need to understand?

#

As I understand them, structs are a little like objects, except they do not hold methods. They are effectively a collection of variables.

steep pollen
#

structs are a little like objects, except they do not hold methods
that is not entirely correct. you can have methods on structs. they are just value types.
have you checked your transition settings to ensure you don't have any exit time to transition out of the animation

charred fjord
#

to exit out of the attack animation? I would think I want that exit time and to keep the player still until the attack animation completes.

#

ah, this might be the issue. I have these transition events I was using to make my animation graph cleaner. I bet that is a janky work around that is causing the delay.

#

I figured that since they have no motions to show, the operation would be skipped.

#

yeah, that looks like it was the cause.

#

So instead of that path, I added another idle animation node. Do you think that is the right course of action?