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
2
Question by RyanVanVliet · Nov 18, 2012 at 05:00 PM · time

Working with probability over time

I am looking for some help with the math for firing an event at some point over a period of time.

Assume I want a light to change from green to red, at some point between 2 and 30 seconds. I need to make it much more likely to happen at 5 seconds than at 25 seconds, but also much more likely to happen at 5 seconds than at 2 seconds. Ideally the increases are logarithmic rather than linear. The chance of the light changing is 100%; when it happens is all that is being determined.

alt text

I will be doing this work with JavaScript.

I hope I worded this well, and thank you in advance for any insight.

graph.jpg (28.7 kB)
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

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by aldonaletto · Nov 18, 2012 at 10:00 PM

You can simulate the curve with a few line segments, using Lerp to interpolate the values in each segment. Let's suppose you want 3 segments, from 2 to 4, 4 to 6 and 6 to 30:

 function ChangeColorAfterDelay(){
   var x = Random.Range(0.0, 3.0); // get a float value between 0 and 3
   var t: float;
   if (x < 1.0){ // first segment
     t = Mathf.Lerp(2, 4, x);
   } else if (x < 2.0){ // second segment
     t = Mathf.Lerp(4, 6, x-1.0);
   } else { // third segment
     t = Mathf.Lerp(6, 30, x-2.0);
   }
   Invoke("ChangeColor", t); // change the color after the random delay t
 }
 
 function ChangeColor(){
   light.color = Color.red;
 }

This code gives equal chances to the 3 segments: 33% for [2-4[, 33% for [4-6[, 33% for [6-30]. The basic idea may be used for more than 3 segments, and each segment may be redefined to get different distributions.

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 RyanVanVliet · Nov 19, 2012 at 02:46 AM 0
Share

This looks like exactly what I was looking for. Thanks everyone for all the great answers, and for so many different ways to tackle it.

avatar image
2

Answer by whydoidoit · Nov 18, 2012 at 10:26 PM

So I'd say the best way would be to define a variable on your script like this:

   var curve : AnimationCurve;

Setup the curve like this perhaps, using the Inspector:

alt text

The find out the amount of time like this:

     var timeToChange = curve.Evaluate(Random.value * curve[curve.length-1].time);

The curve here hangs around 5 seconds for most of the range. You can make the curve as wide as you like (the random is multiplied out to take account of that).


screen shot 2012-11-18 at 22.22.46.png (46.7 kB)
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 Owen-Reynolds · Nov 18, 2012 at 09:33 PM

A specular lighting trick to get fall-off curves from 0-1 is `pow(random(0,1), P);`. Random 0-1 squared starts at 0, stays near the bottom, and curves up to 1. The larger the value of P, the more it "likes" to hang around at 0. But the range is always the full 0-1 (0.999 to the 7th is still pretty much 1.)

To turn that into a 5-30 range, `T = 5+25*pow(random.value, 4)`

To get the fall-off going back to 2, maybe `T=5-3*pow(random.value, 9);` and flip a coin for which one to use.

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

13 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

Related Questions

How to change my stop watch code into real time? 2 Answers

Pause game that not using deltatime for movment 1 Answer

What is the best way to assign a duration to particles? 1 Answer

How to impliment a 10 minute day clock, and tie it to a directional light rotation. 4 Answers

How to do seveal actions like scale on an object one after another? 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