Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in '2D' started by DTheChemist, Aug 12, 2021.

  1. DTheChemist

    DTheChemist

    Joined:
    Jan 18, 2021
    Posts:
    115
    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.

    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (Input.GetButtonDown("Attack"))
    4.         {
    5.           Attack();
    6.         }
    7.         void Attack()
    8.         {
    9.             anim.SetTrigger("attack");//
    10.         }
    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
     
  2. Rin-Dev

    Rin-Dev

    Joined:
    Jun 24, 2014
    Posts:
    557
    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.
     
  3. Rin-Dev

    Rin-Dev

    Joined:
    Jun 24, 2014
    Posts:
    557
    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.
     
  4. DTheChemist

    DTheChemist

    Joined:
    Jan 18, 2021
    Posts:
    115
    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);
     
    Last edited: Aug 12, 2021
  5. Rin-Dev

    Rin-Dev

    Joined:
    Jun 24, 2014
    Posts:
    557
    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.
     
  6. DTheChemist

    DTheChemist

    Joined:
    Jan 18, 2021
    Posts:
    115
    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^^
     
    Last edited: Aug 12, 2021
  7. Rin-Dev

    Rin-Dev

    Joined:
    Jun 24, 2014
    Posts:
    557
    I don't know why an if statement gave you errors but it should have looked like this

    First script
    Code (CSharp):
    1. public bool isAttacking = false;
    2.  
    3. void Update()
    4.     {
    5.         if (Input.GetButtonDown("Attack"))
    6.         {
    7.           Attack();
    8.         }
    9. }
    10.  
    11. void Attack()
    12.    {
    13.       If(!isAttacking){
    14.         anim.SetTrigger("attack");
    15.      }
    16.    }
    Script that goes on the gameobject with the animator
    Code (CSharp):
    1. public void SetAttackTrue()
    2. {
    3. isAttacking = true;
    4. }
    5. public void SetAttackFalse()
    6. {
    7. isAttacking = false;
    8. }
     
  8. DTheChemist

    DTheChemist

    Joined:
    Jan 18, 2021
    Posts:
    115
    i got an error because i tried to do it this way below which obviously is wrong


    Code (CSharp):
    1. if (Input.GetButtonDown("Attack"))
    2.             {
    3.                 anim.SetBool("attack", true);
    4.                 isAttacking = true;
    5.            
    6.             }
    7.             else if (Input.GetButtonUp("Attack"))
    8.             {
    9.                 anim.SetBool("attack", false);
    10.                 isAttacking = false;
    11.  
    12.             }
    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
     
    Last edited: Aug 13, 2021
  9. DTheChemist

    DTheChemist

    Joined:
    Jan 18, 2021
    Posts:
    115
    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
     
    Rin-Dev likes this.
  10. Oliver0769

    Oliver0769

    Joined:
    Apr 28, 2021
    Posts:
    11
    Thank god there are people who actually understand and guide you when you have a problem!
     
  11. DTheChemist

    DTheChemist

    Joined:
    Jan 18, 2021
    Posts:
    115
    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