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
1
Question by heaversm · Jul 14, 2011 at 04:04 PM · coroutineloopinterval

Coroutine Loops

Coroutine Loops

Hi - I'm trying to understand coroutines (I come from Flash where you just run loops or use timers). I looked at this post:

http://answers.unity3d.com/questions/131858/instantiation-at-interval-times.html

but it doesn't seem to address looping. Nor does the documentation for unity - coroutines simply run and then finish. If I try to make a repeat call to the function from within the function, I get an error. Furthermore, if I move certain lines of my code out of update and into the coroutine function, I get errors. All I want to do is have a launcher which fires a projectile at a random interval every 5-10 seconds. This is my code, and I want to replace the dependency on the fire button with an automatic loop.

     function Update()
 {
     if( Input.GetButtonDown( "Fire1" ) ){
         var randomAngle:float = Random.Range(120, 180);
         var randomRot = Quaternion.Euler(0,Random.Range(-30, 30),-10);
         launcherRef.transform.rotation = initialRot * randomRot;
         
         var numRandom:float = (Random.value*objectSizeMax);
         //instantiate a projectile and throw it toward the cube (x axis)
         var instantiatedProjectile : Rigidbody = Instantiate( projectile, transform.position, transform.rotation ) ;
         instantiatedProjectile.transform.localScale = Vector3.one * (numRandom); //I add these two together here because I have no clue what I'm doing
         instantiatedProjectile.velocity = transform.TransformDirection( Vector3( speed, 0, 0) );
         for (var i=0; i<boundaryRef.length;i++){
             Physics.IgnoreCollision( instantiatedProjectile.collider, boundaryRef[i].collider); //make sure to ignore collision between launcher and missile
         }
     }
 };
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
3
Best Answer

Answer by Peter G · Jul 14, 2011 at 04:23 PM

If you have a constant interval, then I suggest using InvokeRepeating(), but for a variable time like this I would do something like this.

 function Start () {
      //Initialize your variables.
     ProjectileLaunch();
 }
 
 function ProjectileLaunch () :IEnumerator {
     while (true) {
          //Launch Projectile
     
          yield WaitForSeconds (Random.Range( minTime , maxTime );
     }
 }


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 heaversm · Jul 14, 2011 at 04:28 PM 0
Share

awesome thanks! I was trying to put the while statement within start.

avatar image Eric5h5 · Jul 14, 2011 at 05:59 PM 0
Share

@heaversm: putting the while statement in Start is fine.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Running multiple coroutines in loops with different time 2 Answers

Play Looped Sound? 4 Answers

How do I loop an animation with Unity 4.3? 5 Answers

Iteration of coroutines 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