Shooting Animation OnFire

void OnFire(InputValue value)
    {
        if (!isAlive) { return; }

        Instantiate(bullet, gun.position, transform.rotation);
        myAnimator.SetBool("isShooting", true);  
}

How can i get my Shooting Animation to go back to Idle animation when i release fire button using this new input system ?

1 Like

This is a little bit tricky to pull off and hard to understand for beginners since the only way to achieve this is by using something called Lambda expressions.

Once you set up all your inputs you have to go to the Input Actions object in the folder you saved it. Select it, click the Generate Class checkbox and then click on Apply

That will create a new script called the same as your Input Actions object, in my case it is named PlayerControls, but if you followed Rick, yours will be called Tilevania.

In your PlayerMovement script, you’ll need to cache and create a new Tilevania class. To do that, create a new variable of type Tilevania and in the Awake method set the variable to a new Tilevania class.

Tilevania controls;

private void Awake()
{
    controls = new Tilevania();
}

When your player is enabled, you have to also enable the Action Map, when your player disables you need to disable the Action Map too or it will cause a lot of bugs. Doing that is fairly simple. The Action Map is this part of the Input Actions game object.

private void OnEnable() { controls.Player.Enable(); }
private void OnDisable() { controls.Player.Disable(); }

Your action map might be named differently, keep that in mind when writing your code. Below is an example of where to change the name of your action map.

private void OnEnable() { controls.MyActionMap.Enable(); }

You are almost ready to go. For the input to react to a button release, you have to use something called Lambda expressions, which I won’t go into detail about here since the post is already long enough.

In the Awake method, assign the method you want your character to execute when releasing a button. In your case, if you followed Rick the code will look something like this:

private void Awake()
{
    controls = new Tilevania();

    controls.Player.Fire.canceled += ctx => MyAction();
}

Let me talk about that line of code for a bit:

  • controls is the class we created.
  • Player is the input map we want to access.
  • Fire is the action.

You can access any action you want by following that sequence.

Here's another example! - Click to Read

    void Awake()
    {
        controls = new PlayerControls();
        controls.AnotherMap.DanceWaterDance.canceled += ctx => Debug.Log("Water is dancing!");
    }

  • canceled means when the button stops being pressed.
  • ctx means context, which I won’t go into a lot of detail but it basically means nothing in this case, you could name it whatever you want, but the word ‘context’ is commonly used for this type of scenario.
  • => that is called Lambda expression.
  • MyAction is the method you want to execute when the button is released.

There are many ways to set that up, but the most efficient one, since you are using Lambda expressions would be the following.

private void Awake()
{
    controls = new Tilevania();

    controls.Player.Fire.canceled += ctx => myAnimator("isShooting", false);
}

You could add whatever method you want, for instance.

private void Awake()
{
    controls = new Tilevania();

    controls.Player.Fire.canceled += ctx => StopFiring();
}

private void StopFiring()
{
    myAnimator("isShooting", false);
    //Do more actions in here!
}

Or even skip that whole new method thing entirely and create the method right there.

private void Awake()
{
    controls = new Tilevania();

    controls.Player.Fire.canceled += ctx => {
        myAnimator("isShooting", false);
        //Do more actions in here!
    };
}

That’s the power of Lambda, well, I did kinda dig a lot into that, but whatever.

I know is convoluted but that’s the only way to access that method, at least to my knowledge.

This is how the entire setup looks like - Click to Read
Tilevania controls;

private void Awake()
{
    controls = new Tilevania();

    controls.Player.Fire.canceled += ctx => myAnimator("isShooting", false);
}

private void OnEnable() { controls.Player.Enable(); }
private void OnDisable() { controls.Player.Disable(); }

Hope this helps!

3 Likes

thankyou so much for this. You are an absolute star ! this will help loads.

Iv’e changed Tilevania to Cybergirl by the way :stuck_out_tongue:

1 Like

yeh i got it working ! :smiley:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms