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
0
Question by MountDoomTeam · Oct 15, 2012 at 01:51 PM · updatedisplaywhileshowrefresh

make Update display/animate iterations of While(i<20)loop?

it's a digger function that digs tunnels in space when i press z button.

its in a while loop so if i want to dig 20 spaces, i do the while loop 20 times. this freezes unity for a moment, and then shows all 20 spaces have been dug.

I would like to see the tunnel advancing as fast as the processor can dig, i dont mind scrapping the while loop for something else, or to include a command "RefreshDisplay" in the while loop, something like that? thanks for being so amazingly helpful guys i am learning so much ;)

 function Update()
 {
   if ( Input.GetKeyDown("z") )
   {
      d=0;
      while (d < digSize)
   {

   DigRandomTunnel();

    d=d+1; }
 }
 }
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

Answer by UGTools · Oct 15, 2012 at 03:09 PM

You can try something like this:

     function Update()
     {
        if(Input.GetKeyDown("z"))
        {
          digsRemaining += digsPerPressedZ;
     
          if(digsRemaining > digsMax)
          {
            digsRemaining = digsMax;
          }
        }
     
        if(digsRemaining > 0)
        {
           DigRandomTunnel();
           digsRemaining = digsRemaining - 1;
        }
     }

This way each time the key is pressed you only increase the counter (with an optional limit). Each frame DigRandomTunnel() will be called only once so that the screen is updated as quickly as possible and the process doesn't get stalled. This will happen until there is nothing left to dig (digsRemaining will be 0).

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 ByteSheep · Oct 15, 2012 at 02:44 PM

It would be helpful to know what you are doing in the DigRandomTunnel() function, but I think that a while loop is not what you would need in this situation. The while loop will try to execute the 20 spaces in one single frame..
How are you specifying digSize's value though? Does the user input the value? Or would you like the digSize to vary depending on the duration the user presses the z key?
If you could give us the code in your DigRandomTunnel() function it would be easier to how exactly you're making the blocks disappear in the game.. This code is assuming you are using an animation and that you would like the amount of spaces dug to depend upon the duration of the keypress:

function Update() { if ( Input.GetKeyDown("z") ) { DigRandomTunnel(); //call DigRandomTunnel() once every frame { }

function DigRandomTunnel() { digSize++; //increase digSize each time we remove a block yield WaitForSeconds(0.2); //wait for 0.2 sec before next block is removed

animation.Play(); //play animation or whatever it is you are doing here }

This is just a guess at what the code might look like, however to be able to actually give a proper answer we would have to see some more of your code :)

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 Kryptos · Oct 15, 2012 at 02:59 PM 0
Share

Calling a new coroutine each frame is not a good idea. New coroutines will be stacked upon each other.

Using coroutine is a good idea (the best in my opinion), but you need to "protect" the call, for example by using a boolean:

 var digging = false;
 function Update()
 {
     if (!digging && Input.Get$$anonymous$$eyDown("z"))
     {
         DigRandomTunnel();
     }
 }

 function DigRandomTunnel()
 {
     digging = true;
     for(var i : int = 0; i < 20; i++)
     {
         // code: "digging"
         yield; // wait until next frame
     }
     digging = false;
 }
avatar image MountDoomTeam · Oct 15, 2012 at 06:20 PM 0
Share

thanks alot!!! i will attempt to figure out a solution from these options. here is a demo of the digging code. my first 3d program :) http://www.youtube.com/watch?v=$$anonymous$$HpIb6iDv$$anonymous$$Q&feature=youtu.be

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

12 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

Related Questions

How to refresh my webplayer at runtime 3 Answers

I am Making a driving simulation.i want ti display main camera rendered in to another exe of unity3d its possible?pl z give reply any one. 2 Answers

Most efficient way of pausing a function 0 Answers

Update function - Auto update 1 Answer

Is it possible to make a camera update only once every X seconds ? 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