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 djpitris · Aug 11, 2021 at 09:41 AM · variableloopwait

While something is reoccuring keep value the same

Hello,

Been playing in Unity for about a week now. So far I was able to find all the answers all around the internet, but for this problem I'm clueless (mainly, because I don't know how to properly formulate the question...).

In this game if you click the character fox it sets a parameter to certain value, then it should wait for 2 seconds and revert the parameter.

 void FixedUpdate(){
 ....
             if (hit.transform.name == "fox")
              AudioRecorder.noiseMin = 20;
             else
              StartCoroutine(ReturnNoiseMin());
             
                 IEnumerator ReturnNoiseMin()
                 {
                     yield return new WaitForSeconds(2f);
                     AudioRecorder.noiseMin = 0.5f;
                 }
 ...
 }
 

Works fine when you click the character only 1. However when you keep clicking, the first 2 seconds pass and for a few frames it sets the value back. Which is kind of logical and expected, but I can't figure out how to "prolong" this 2 seconds with each click.

I tried using Time.time + 2 seconds, but its the same thing just written differently...

I believe the answer is simple and stupid, but I just can't figure it out and this is my second game with the same issue. In the first it was on collision => slow down object => after 0.5 seconds return speed, but if you kept colliding every 0.5 seconds you got the speed back for few frames...

Thank you very much for any hint. Best Regards Petr

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

2 Replies

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

Answer by berkun5 · Aug 11, 2021 at 10:13 AM

  void FixedUpdate(){
  ....
              if (hit.transform.name == "fox")
               AudioRecorder.noiseMin = 20;
              else
               StartCoroutine(ReturnNoiseMin());
              
                  IEnumerator ReturnNoiseMin()
                  {
                      yield return new WaitForSeconds(2f);
                      AudioRecorder.noiseMin = 0.5f;
                  }
  ...
  }



Hi @djpitris , In this script, you are settings the AudioRecorder.noiseMin to 20 if the name is Fox. So if the name is actually Fox it never runs the else part and never starts the coroutine.

If i understood correct, you want to set the AudioRecorder.noiseMin = 20, wait for 2 seconds, set the AudioRecorder.noiseMin = 0.5f.

You can go for something like this:

 bool isActive;
 void FixedUpdate()
 {
     if (hit.transform.name == "fox" && !isActive)
     {
         AudioRecorder.noiseMin = 20;
         StartCoroutine(ReturnNoiseMin());
     }
     else
     {
         return;
     }
 }
 
 IEnumerator ReturnNoiseMin()
 {
     isActive = true;
     yield return new WaitForSeconds(2f);
     AudioRecorder.noiseMin = 0.5f;
     isActive = false;
 }
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 djpitris · Aug 11, 2021 at 10:35 AM

Hi @berkun5 ,

Thank you very much for your response and hint.

After I wrote it I realized the mistake and it seems I also found a different solution?

I set slappedTime to the time the touch happened and outside the hit detection I check if the time is lower than current time - 2 seconds, then run the ReturnNoiseMin.

Time to fix my previous game too :-)

     void FixedUpdate()
     {
         if (slappedTime <= Time.time - 2f && AudioRecorder.noiseMin != 0.5f)
         {
             ReturnNoiseMin();
         }
 
     if (hit.transform.name == "fox")
     {
         AudioRecorder.noiseMin = 20;
         slappedTime = Time.time;
     }
 
     void ReturnNoiseMin()
     {
         AudioRecorder.noiseMin = 0.5f;
     }

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 djpitris · Aug 11, 2021 at 12:02 PM 0
Share

Works on my other game too, so I hope its not too wrong, I will keep it here for others.

In the other game I check if I'm colliding with certain thing (I leaved out the unrelated IFs...)

     void FixedUpdate()
     {
 ...
         if (digTime <= Time.time - 0.25f && speed != 8f)
         {
 
             SetBackSpeed();
         }
 ...
 }
     private void OnCollisionStay2D(Collision2D other)
     {
 ...
             digTime = Time.time;
             speed = 4f;
 ...
 }
 
     private void SetBackSpeed()
     {
         speed = 8f;
     }

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

128 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

Related Questions

Sequentially wait for multiple coroutines to finish 0 Answers

Have a function to Wait until true? 4 Answers

Help With "For Loop" Not Working? 2 Answers

Find Position in Array or List? 2 Answers

Can't pass a variable as method parameter? 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