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
0
Question by EaglePuncher · Jul 15, 2013 at 10:20 AM · mecanimupdatecombochainpunch

Resetting a combo chain

I'm trying to use mecanim to chain combo animations together. My trouble is the counter that I assign in anim will always be 0, because Update() runs much faster than you can click the mouse to increase the counter. Is there a simple way to execute this every fifth update or so, or delay it so the counter can increase properly?

 void Update(){

 if(Input.GetButtonDown("Fire1")){
    
 if (Mathf.Abs(Time.time - nextPunch) < 2){
 
         switch(punch){
  
             case 0:
                 //This is the first swing in the chain
         punch = punch + 1;
                 nextPunch = Time.time + punchRate;
         anim.SetFloat("Punch", punch);
                 break;
 
             case 1:
                 //This is the second swing in the chain
                 
         punch = punch + 1;
                 nextPunch = Time.time + punchRate;
         anim.SetFloat("Punch", punch);
                 break;
 
             case 2:
                 //This is the last swing in the chain
         punch = punch + 1;
                 nextPunch = Time.time + punchRate + punchDelay;
         anim.SetFloat("Punch", punch);
                 break;
          }
       }
     } 
     else {
          punch = 0;
      nextPunch = 0.0f;

     }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by amphoterik · Jul 15, 2013 at 11:55 AM

A counter system is pretty easy. Just create a counter, threshold, and test:

 int counter = 0;
 int threshold = 5;
 
 void Update()
 {
     if(++counter <= threshold) return;
     counter = 0;
     //rest of your code
 
 }

This will only run the update every 5 times.

Now, while that answers your question, it is probably not great to delay the update method. You might be doing other important things in there. Instead, you would want to delay only the resetting of the combo. You would also want to do it based on time and not a set number of updates. For example, if you wanted to reset the counter every half second:

 float counter = 0;
 float threshold = .5f;
 
 void Update()
 {
     counter += Time.deltaTime;
     if(counter <= threshold) return;
     counter -= threshold;

     //rest of your code
 
 }
Comment
Add comment · Show 2 · 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 EaglePuncher · Jul 15, 2013 at 08:00 PM 0
Share

Thanks so much, the second one worked perfectly! It should be noted for future searchers that my update loop is now structured as such:

     void Update()
 
 {
     counter += Time.deltaTime;
     if(counter <= threshold)  //notice no semicolon
     
         //combo code
     
         return;
         counter -= threshold;
         //continue updating
 
     }

This allows my update to run while there isn't a combo going on. The original code as posted will cut update short every frame, which didn't work for my purpose.

avatar image amphoterik · Jul 16, 2013 at 11:29 AM 0
Share

Glad it worked. Please select my answer as the answer to this question. It helps future readers and gives you karma as well.

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

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to update 3D character 1 Answer

Does anybody have best practices on creating an action-adventurer character (Mecanim)? 0 Answers

Mecanim Trigger Stays too long true 1 Answer

mecanim blend tree attack combo 2 Answers

Mecanim 1 frame delay [solved] 2 Answers


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