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 hosap4ik · Nov 24, 2019 at 11:05 AM · startboolinvokerepeating

how to run two different function every x seconds based on bool value?

I know I can use invokeRepeating to run function every x seconds. And invokeRepeating should not be in Update or FixedUpdate. It should be in Start or Awake. But in my code it doesn't work. Please look at my code.

     public Rigidbody rb;
     public float movingSpeed = 200;
     public int maxSpeed = 3000;
     public bool forwardBackMoving, sideMoving;
     void Start()
     {
         rb = GetComponent<Rigidbody>();
         forwardBackMoving = true;
         sideMoving = true;
         if ((forwardBackMoving == true || sideMoving == true)&&movingSpeed<maxSpeed)
         {
             InvokeRepeating("increaseSpeed", 5.0f, 5.0f);
         }
         else if(forwardBackMoving==false&&sideMoving==false)
         {
             CancelInvoke();
             InvokeRepeating("decreaseSpeed", 1.0f, 5.0f);
         }
     }

This is my code. When one of two bools are set to true, the first one triggers. The second one should be triggered when both booleans are false.

  public void keyUpEvent()
         {
             if (Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.S))
             {
                 forwardBackMoving = false;
             }else if (Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.D))
             {
                 sideMoving = false;
             }
         }
 

I have this code which changes boolean values. This function is called in FixedUpdate method. Changing bool works without any problem, but the second invokeRepeating never never never wants to trigger even both bools are changed to false. Please can anybody help me?

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

Answer by Blindleistung · Nov 24, 2019 at 11:44 AM

You are starting one of the two repeating things in start. The condition is only checked once and then this one function, "increase speed" or "decrease speed", are done over and over.

So you need to call one function, like adjustSpeed , and inside this function check the condition.

Comment
Add comment · Show 3 · 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 hosap4ik · Nov 24, 2019 at 12:02 PM 0
Share

But I can't make them just as one function because those two functions should have different second parameter. As you see, increaseSpeed has 5.0f as second parameter but decreaseSpeed has 1.0f

avatar image hosap4ik · Nov 24, 2019 at 12:29 PM 0
Share

Or do you mean call adjustSpeed in Start() and declare bools and do invokeRepeats in adjustSpeed?

avatar image hosap4ik · Nov 24, 2019 at 12:37 PM 0
Share

I tried adjustSpeed and moved if statements to adjustSpeed and called adjustSpeed inside start()

     void Start()
     {
         rb = GetComponent<Rigidbody>();
         forwardBack$$anonymous$$oving = true;
         side$$anonymous$$oving = true;
         adjustSpeed();
     }
     void adjustSpeed()
     {
         if ((forwardBack$$anonymous$$oving == true || side$$anonymous$$oving == true) && movingSpeed < maxSpeed)
         {
             InvokeRepeating("increaseSpeed", 5.0f, 5.0f);
         }
         else if (forwardBack$$anonymous$$oving == false && side$$anonymous$$oving == false)
         {
             CancelInvoke();
             InvokeRepeating("decreaseSpeed", 1.0f, 5.0f);
         }
     }

same result.

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

116 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

Related Questions

Issues with InvokeRepeating in Start/Update functions 1 Answer

Initialising List array for use in a custom Editor 1 Answer

if and invoke repeating in start... 1 Answer

How do I replace Invoke Repeating to allow a repeat rate change 1 Answer

Android application start before splash 0 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