How can fix attack input so that it only works after animation is over

Hello i just need help with this particular script. Ive been trying to figure it out even with animation events but i still cant figure it out. Heres the starting code

I know its needs more but im told there are multiple ways to fix this. i just need helping seeing with guidance the easiest way to follow.

void Update()
    {
        if (Input.GetButtonDown("Attack"))
        {
          Attack();
        }
        void Attack()
        {
            anim.SetTrigger("attack");//
        }

Currently im trying to figure out do i need to add a True false bool setting for this like i did for my crouch to work

video example here. Im mashing the button on purpose to show what i dont want. Basically i need guidance on how to stop it from allowing me to do that. I only want the input to be allowed until the animation is entirely over and also i dont want him able to whip while moving. I originally followed a Brackey’s tutorial. It only applies to the type of attack he was going for unlike mines. it was only one video for it

First, take your attack function out of your update function. It won’t work trying to do it that way. Keep the input in update though.

Second, you can use animation events to set a bool to check for animation end, or you can grab the animation clip length and set a timer to reset your attack bool that way as well.

Too be a little more in depth, yes add a bool called isAttacking to your script. Then use that with animation events to check if you can attack again or not.

Add the bool above in the script looking like this?

private bool isAttacking = false;

or this you mean?

public void SetBool(string name, bool value);

Both, the second one but a little different.

Make isAttacking public, make an if statement in your attack function using it, and use a secondary script on your animator referencing the attack script and make a public void SetAttackTrue() and public void SetAttackFalse() functions. Now use animation events to control your isAttacking bool.

ok so i did this.

public void SetAttackTrue()
{
isAttacking = true;
}
public void SetAttackFalse()
{
isAttacking = false;
}

is that wrong?^^

Nothing happened when i tried to apply the events to certain frames for it to fix the issue. I tried to do the IF statement but i was getting compile errors. Im using a SetTrigger and i was getting confused because it was starting too look like ill have to change it to a SetBool instead to the IF statement like my how my crouch function looks

i suck at my level of Unity knowledge right now (which ill get better at eventually) and what i keep hearing is theres multiple ways to fix this issue. I just need to know whats the most simple to follow and learn from. This is a brick wall situation for me, im being honest. Literally a blank after that at attempt. looked at the docs too to see if theres any cheat sheet examples of similar situations and i saw nothing^^

I don’t know why an if statement gave you errors but it should have looked like this

First script

public bool isAttacking = false;

void Update()
    {
        if (Input.GetButtonDown("Attack"))
        {
          Attack();
        }
}

void Attack()
   {
      If(!isAttacking){
        anim.SetTrigger("attack");
     }
   }

Script that goes on the gameobject with the animator

public void SetAttackTrue()
{
isAttacking = true;
}
public void SetAttackFalse()
{
isAttacking = false;
}

i got an error because i tried to do it this way below which obviously is wrong

if (Input.GetButtonDown("Attack"))
            {
                anim.SetBool("attack", true);
                isAttacking = true;
           
            }
            else if (Input.GetButtonUp("Attack"))
            {
                anim.SetBool("attack", false);
                isAttacking = false;

            }

i changed my IsAttacking SetTrigger to a Bool (because i was a lil confused) to see what happens that way and then i got the compile errors.

But anyway ill try again this evening what you showed me there to see what happens. I just have to grasp how to Build certain codes the right way. Like i said im seeing various ways to do the same thing so im trying to catch on to the easiest method for me to see or follow that i can use. What works best is what ill stick with. Thanks again for the replies

Ok Thanks a lot it worked. after i set up the script as you showed me i added a false attack event to the end of the animation (last frame) and one thats true at the start. Fixed the issue. Ill save this to my notes to look back on now to remember

Cheers

1 Like

Thank god there are people who actually understand and guide you when you have a problem!

oh for sure. Just have to be patient and keep busy tinkering while waiting on the help response. but yeah it is refreshing when its really help and not riddles to a noob that really hasnt grasped it yet. But at the same time you gotta show you are making some sort of effort too whether its a horrible attempt or not shown. Its a dice Roll at any forum for help. Some times you will get help and some times you wont.

anyway for those just starting if you had the same issue the code and method direction is there to possibly help your current situation

Peace