• 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 melvincostner · Jun 12, 2021 at 11:56 AM · buttonrigidbody2dfunctiontimerboolean

determine the minimum time for the player to hold the button

the idea is that when the player holds the heal button, the character stays still and concentrating the healing (as in the hollow knight), and spends part of the collected souls, i have been trying something like this.

     public void SoulsControl()
     {
         if (souls >= maxSouls)
         {
             souls = maxSouls;
         }
 
         if (playerInputs.healing && souls > healCust)
         {
             canMove = true;
             rb.velocity = Vector2.zero;
             souls -= healCust;
             isHealing = true;
 
             if (isHealing)
             {
                 StartCoroutine(GainHealth());
             }
             else
             {
                 StopCoroutine(GainHealth());
                 canMove = false;
                 isHealing = false;
             }
 
         }
     }
     IEnumerator GainHealth()
     {
         yield return new WaitForSeconds(timeBtwHeal);
         Damageable damageable = GetComponent<Damageable>();
         damageable.SetHealing(1);
         isHealing = false;
         canMove = false;
 
     }

Comment
Add comment · Show 1
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 unity_HTr7mOeTIRzvVA · Jun 12, 2021 at 02:55 PM 0
Share

I don't know if this is what you mean. I thinking that you want to record how long did the player held the button.

 IEnumerator GainHealth()
      {
               float holdDuration = 0;
               
 while(holdDuration <= timeBtwHeal && isButtonHeld){ // "isButtonHeld" is what ever input youre using here, just change it.
 
 holdDuration += Time.deltaTime;
 yield return null;
 }
 
 Debug.Log("the player held the button for " + holdDuration +" seconds");
 
                        Damageable damageable = GetComponent<Damageable>();
                                 damageable.SetHealing(1);
                                          isHealing = false;
                                                   canMove = false;
      }

1 Reply

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

Answer by PhyrePhrost · Jun 12, 2021 at 05:29 PM

You can use a function that set a boolean value to true... And a timer into the update function, when the boolean value is true the timer start... when passing certain seconds you will call the method

 public bool startTimer = false;
 float timer = 0;
 public float timeToReach = 0.3f;
 
 void Update()
 {
    if(startTimer)
    {
       if(timer<timeToReach)
          timer+=Time.deltaTime;
       else
          SoulsControl();
    }
 }
 
 public void OnButtonPressed()
 {
    startTimer=true;
 }
 
 public void OnButtonReleased()
 {
    startTimer=false;
    timer = 0;
 }
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 melvincostner · Jun 15, 2021 at 02:39 PM 1
Share

thank you very much it worked for me!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

152 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

Related Questions

How to time when a button is pressed 1 Answer

Boolean function doesn't work anymore 2 Answers

Button not executing function. C# 3 Answers

Missing function in button on click 1 Answer

How to stop the countdown timer when I have collected all the coins? 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