Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed Oct 01, 2020 at 10:23 PM by twotostudio for the following reason:

Other

avatar image
0
Question by twotostudio · Oct 01, 2020 at 03:35 PM · time.deltatimestopcancel

How do I keep an increment process continuous without the update function?

  void gravincrease()
     {
         Physics.gravity = grav;
         grav.y -= 0.05f * Time.deltaTime;
     }

I don't want to write this code in update. Because whenever I want, I want to using CancelInvoke. How can I make such a function permanent? or how can I stop these codes for a while when I start a coroutine?

Comment
Add comment · Show 3
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 twotostudio · Oct 01, 2020 at 03:45 PM 0
Share
      IEnumerator downgrav()
         {
             Vector3 initialGravity = Physics.gravity;
             Physics.gravity = initialGravity * 0.5f;
             stopgrav = true;
             yield return new WaitForSeconds(10f);
             Physics.gravity = initialGravity;
             stopgrav = false;
         }
 {
 
  if (stopgrav==true)
         {
             CancelInvoke("gravincrease");
         }
 
    }
avatar image twotostudio twotostudio · Oct 01, 2020 at 03:46 PM 0
Share

Actually what I want to do is to make this part work.

avatar image rh_galaxy twotostudio · Oct 01, 2020 at 09:11 PM 0
Share

It is important to show in what function your code is. And also the code that calls your functions. Who runs gravincrease()? Is it in an InvokeRepeating() call? If you call CancelInvoke() it will stop all Invokes and you have to start them again when you want that. Personally I want to write all my code in Update, and never use Coroutines or Invokes.

2 Replies

  • Sort: 
avatar image
1

Answer by UnityToMakeMoney · Oct 01, 2020 at 10:19 PM

I would just make a WaitForSeconds coroutine with no delay and stop the coroutine through a boolean flag.

 IEnumerator gravincrease(float delay=0f){//default argument - does not need to be filled
          yield return new WaitForSeconds(delay);//wait for delay
          Physics.gravity = grav;
          grav.y -= 0.05f;
          if(!boolFlag)//if your flag is false
                   StartCoroutine(gravincrease(delay));//call this function again
 }


Hoped this helped :). Also as a side note, it is recommended to comment your code and camelcase (or some other method) to name functions and variables.

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 twotostudio · Oct 01, 2020 at 10:26 PM 0
Share
  bool stopcor=false;
 
  public void gravdown()
     {
         StartCoroutine(downgrav());
     }
     IEnumerator downgrav()
     {
         stopcor = true;
         Vector3 initialGravity = Physics.gravity;
         Physics.gravity = initialGravity * 0.5f;      
         yield return new WaitForSeconds(10f);
         Physics.gravity = initialGravity;
         stopcor = false;
     }
 
  void Update()
     {
         if (stopcor == false)
         {
             if(grav.y>-11)
             grav.y -= 0.05f * Time.deltaTime;
             Physics.gravity = grav;
         }
 }


Thanks a lot for the help. I solved the problem by trying. I also used bool but in a different way.

avatar image
0

Answer by Zaeran · Oct 01, 2020 at 05:10 PM

You can assign a coroutine to a variable like so:

 Coroutine downGravCoroutine = StartCoroutine(downgrav());

You can then stop it later using:

 StopCoroutine(downGravCoroutine());

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 twotostudio · Oct 01, 2020 at 05:47 PM 0
Share

I guess these codes are so that I can stop the "downgrav" part. The process I want to pause for a while is an increase in gravity over time.

Follow this Question

Answers Answers and Comments

138 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

Related Questions

Any way to stop a build? 3 Answers

Stop instance 1 Answer

Cancel WWW post operation 1 Answer

Track time between 1 Answer

Audio won´t stop 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