Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by KetihG28 · Nov 04, 2014 at 06:55 AM · animatorcombo

Attack combo

I have been researching a lot on attack animations and scripting combos. I have used the following code that I've found online but I had a few questions about it, if anyone has to time to clarify.

Code Sample:

 if (Input.GetButtonDown("Punch") && isGrabbing == false)
 {
      LastPress = Time.timeSinceLevelLoad;
 
 if(tap > 2)
 {
        tap = 1;
 }
 
      else
      {
         tap++;
      }
 
      Combo2();
 
   }
 
    if(timer - LastPress > 1)
    {
     tap = 0;
     if(timer - LastPress < Atime)
         {
         attack = false;
     }
 
         else
         {
            attack = true;
     }
 }

I do not fully understand how to control the key presses. When I press my Punch button, the animation is constantly interrupted because there is not delay between key press.

The following code is a switch statement that I'm using with Unity's Animator Control because I'm using Unity2D, which does not allow "legacy animations"

Switch Statement:

 void Combo () 
 {
     //WaitForSeconds(2);
     if(attack)
         {
         switch (tap)
         {
                     
         case 0:
         //Reset to Idle
         anim.Play("FightStance");
         break;
                     
         case 1:
         //Combo Start
         anim.Play("punch1 1");
         break;
                     
             case 2:
         //Combo Prolonge
         anim.Play("punch2");
         break;
                     
         case 3:
         //Combo Finished
         anim.Play("kick1");
         break;
         }
     }
 }

Sorry for all the detail but my main question is: How can I allow the animator to play my full attack animation before I press the Punch button to perform the next animation. Also, is it possible to force my pressing of the Punch button be inactive until the animator finish playing my animation state.

Thanks in advance to anyone who can help me with this Unity2D puzzle.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by yashpal · Nov 04, 2014 at 08:27 AM

Hello @KetihG28, There are many ways you can perform this task.

1)Playing two separate animations one after another upon completion

2) You can make Animation Events in your animation. Put Animation event at the end Of clip. Next, In your script you make one bool which indicate animation is playing or not(I call it IsComboAnimationPlaying). And IsComboAnimationPlaying = true when you play your animation. and make one function which is set false IsComboAnimationPlaying. like

 void AnimationIsOver()
 {
    IsComboAnimationPlaying = false;
 }

And Give function named "AnimationIsOver" in your your Animation Events. And Don't call next animation while IsComboAnimationPlaying == true So Basically Animation event call AnimationIsOver when animation is reach to point where you put animation event in animation clip.

3) You can same way disable Punch Button by

 PunchButtonGameObject.SetActive (false);
 PunchButtonGameObject.SetActive (true);


And Many more ways. Hope This will help you.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by KetihG28 · Nov 06, 2014 at 04:59 AM

I have resolved the issue! Thankx yashpal! I'm not a beginner with Unity but I'm not a pro either. I didn't know anything about Animation events but I knew that would be the ticket to solving my issue. Here's a snippet of my code:(1st Code Snippet) void Combo () { if(attack) { switch (tap) {

 case 0:
 //Reset to Idle
 anim.Play("FightStance");
 break;
                 
 case 1:
 //Combo Start
 anim.Play("punch1 1");
 IsComboAnimationPlaying = true;
 break;
                 
 case 2:
 //Combo Prolonge
 anim.Play("punch2");
 IsComboAnimationPlaying = true;
     break;
                 
 case 3:
 //Combo Finished
 anim.Play("kick1");
 IsComboAnimationPlaying = true;
 break;
 }
  }

}

You suggested IsComboAnimationPlaying to be my condition to control what happens before and after an animation.

I also input this snippet:(2nd Code Snippet)

void AnimationIsOver() { IsComboAnimationPlaying = false;

 Debug.Log ("Animation has finished");  <--- Not required just checking to see if the function is being acknowledged

}

So in order for IsComboAnimationPlaying to be most effective, I added an Animation event at the end of each of my attacks.

Resolution in order: 1)bool IsComboAnimationPlaying set to false

2)Have the 2nd snippet of code that I've shown, be the off switch for IsComboAnimationPlaying.

3) I set IsComboAnimationPlaying = true when I performed each animation (On switch) (1st code snippet)

4) After each animation has played I set IsComboAnimationPlaying = false (Off switch)

The major thing that controlled this so well was my "tap" commands that work through the animations. I set the condition to that, so that I couldn't constantly pressed my Punch button and increment the tap value.

Code Snippet:

timer = Time.timeSinceLevelLoad;

//if(Input.GetKeyDown(KeyCode.F))

if (IsComboAnimationPlaying == false) <------ Very important to control tap value { Debug.Log("Attack is allowed");

     if (Input.GetButtonDown ("Punch")) 
     {
     LastPress = Time.timeSinceLevelLoad;
 if (tap > 2) 
     {
     tap = 1;
 }
     else 
     {
     tap++;
 }
     Combo ();

}

     if (timer - LastPress > 1) 
     {
     tap = 0;
    if (timer - LastPress < Atime)
        {
     attack = false;
    }
        else
        {
      attack = true;
    }
     }

}

Sorry for the long explanation, I'm obviously new to forums but I just wanted to give a shot out to yashpal and also spread the wealth of knowledge I've gain. Thanks again.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dr_GeoFry · Nov 02, 2018 at 08:29 PM 0
Share

@ $$anonymous$$etihG28

what did your completed script look like, and did it completely achieve what you set out to when you asked this question?

avatar image
0

Answer by Dr_GeoFry · Nov 02, 2018 at 08:28 PM

@ KetihG28

what did your completed script look like, and did it completely achieve what you set out to when you asked this question?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Wait for animation to finish? Animator.SetBool... 1 Answer

Mecanim Combo Tree returning to Idle State 1 Answer

2D Animation does not start 1 Answer

Combo Attack/Animator issue 0 Answers

Switching between scripts within the inspector using code 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges