Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 KyleF1997 · Aug 08, 2017 at 03:58 AM · animationinputfunctioncallvoid

How to prevent a function from being called more than once

I have a combat system that I'm working on which involves players using quick and heavy attacks. I've encountered an issue wherein the attack function can be called twice if the player rapidly clicks the mouse, and is always called twice if the player holds the mouse button for a heavy attack. Here's the relevant code snippets:

 void Update()
 {
     if(anim.GetCurrentAnimatorStateInfo(0).IsName("idle"))
     {
         isAttacking = false;
     }
     else
     {
         isAttacking = true;
     }

     if (!isAttacking)
     {
         // Right Attacks

         if ((Input.GetButton("Fire1")) && (Input.GetAxis("Horizontal") > 0))
         {
             holdTime++;
             if (holdTime >= 10)
             {
                 StartCoroutine(rightHook());
                 holdTime = 0;
             }
         }

         if ((Input.GetButtonUp("Fire1")) && (Input.GetAxis("Horizontal") > 0) && holdTime < 10)
         {
             StartCoroutine(rightJab());
             holdTime = 0;
         }

 // Player Combat Functions

 IEnumerator rightHook()
 {
     anim.SetTrigger("rightHook");
     applyDamage(heavyDamage);
     debugCall++;
     Debug.Log("Function called: " + debugCall + " times.");
     yield break;
 }

With the debug log you can see above, it counts twice. This is an issue, because it means damage is applied twice, and the animation is also triggered twice which looks bad. I thought an easy, clean way to determine whether or not the player was attacking was to check if they were in their idle animation (I'd have to change it later, but at the moment there's no walking animations or anything else outside of the combat system). I'm guessing I'll have to figure out a more intuitive way to return the isAttacking condition, but I'm at a loss. Any help would be greatly appreciated!

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 game4444 · Aug 08, 2017 at 04:53 AM

Your code looks fine. It would be running when again Holdtime become 10. I tried code same like you. Working perfectly fine. but its calling again and again when timer value equals to 10.

 float timer = 0;
 void Update()
 {
     if ((Input.GetButton("Fire1")) && (Input.GetAxis("Horizontal") > 0))
     {
         timer += 1; if (timer >= 10)
         {
             StartCoroutine(print());
             timer = 0;
         }
     }
 }

 IEnumerator print()
 {
     print("print times ");
     yield break;
 }
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 ShadyProductions · Aug 08, 2017 at 10:35 AM 0
Share

Please format code correctly next time, I did it for you.

avatar image KyleF1997 · Aug 08, 2017 at 11:54 AM 0
Share

I managed to work out a temporary solution, I've raised the float value for a heavy attack to 20. The issue seemed to be that the float value was filling up twice before the "isAttacking" bool could be switched on.

Still a bandaid solution but it'll do for a prototype. Giving you the best answer because you gave me the idea to mess around with the float value.

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

174 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 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 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 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 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 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 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 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 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 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 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

Call a function at a certain frame in animation without Animation Events? 1 Answer

GetButton Problem 1 Answer

Check if a function is no longer being called? 3 Answers

How to get different Animations when different button combos are pressed 2 Answers

How to reference another script and call a function in C# ? 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