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 BCouto · Oct 26, 2010 at 05:02 PM · javascripttimerdelay

Random Delay time

I'm trying to set up some falling objects in a 2d view, right now they fall and respawn fine, but I'm trying to get a few 'special' ones to fall after a random delay between 5-20 seconds each. How would I go about achieving this? I know how to use the Random.Range, but how would I apply this to a delay timer?

This is my delay code:

var objectSpeed: int;

var delay = .5; var timer = 0.0;

function Update () {

timer += Time.deltaTime;

if (timer >= delay){

amtToMove = objectSpeed * Time.deltaTime;

transform.Translate(Vector3.down * amtToMove); //timer = 0.0;

}

Any help would be appreciated.

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
0
Best Answer

Answer by Eric5h5 · Oct 26, 2010 at 07:53 PM

var objectSpeed: int; var minWait = 5.0; var maxWait = 20.0;

function Start () { yield WaitForSeconds(Random.Range(minWait, maxWait));

 while (true) {
     transform.Translate(-Vector3.up * objectSpeed * Time.deltaTime);
     yield;
 }

}

Note that Vector3.down is undocumented/deprecated; you should use -Vector3.up instead.

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 BCouto · Oct 26, 2010 at 09:12 PM 0
Share

Ok, that seems to work.. sort of. Inside of the while statement, I placed an if statement so that if the object reached a height, its position will reset to where it was before. The only problem now is that it ignores the delay after it drops one time. How can I make the delay work each time?

avatar image Eric5h5 · Oct 26, 2010 at 09:19 PM 0
Share

@$$anonymous$$: if I understood correctly, enclose all the code in the Start function in a "`while (true)`" loop, and with your if statement, use some condition other than "true" so that the inner while loop ends appropriately. (Or at least use break so that you break out of the inner loop when needed.)

avatar image BCouto · Oct 26, 2010 at 10:33 PM 0
Share

Actually, I just copied the yield line into the if statement, and it did the trick. Thanks for the help.

avatar image
0

Answer by AliAzin · Oct 26, 2010 at 05:08 PM

You can simply use yield structure before respawning :

// before respawning do this :
yield WaitForSeconds(Random.Range(5,20));
//respawning
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

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

No one has followed this question yet.

Related Questions

Need help with delaying a timer (javascript) 2 Answers

Stealth AI Script. If less than or equal to not working correctly. Any fixes ? 1 Answer

Setting Scroll View Width GUILayout 1 Answer

JavaScript Timer/Highscore Problem 1 Answer

Animation Delay Problem 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