Question by
MastersOfUs · Oct 04, 2017 at 01:51 AM ·
randommathlogic
Randomness based on time passed
How would I make an expression that returns true an average X times per minute based on time passe (time.deltaTime)
For example:
float timesPerMin = 10;
void Update() {
if(/* Some code*/) {
// This code is called 10 times per minute on average, based on Time.deltaTime
}
}
Comment
Answer by Cuttlas-U · Oct 04, 2017 at 04:27 AM
hi;
i created a TimePassedTemp that is a float; this value will be 1 after 1 sec; so all u have to do is check if it is equel to timesPerMin ;
private float TimePassedTemp;
void Update() {
TimePassedTemp += Time.deltaTime;
if ( TimePassedTemp >= timePerMin * 60 ) // each min is 60 sec
{
// do your staff
}
}
Answer by elenzil · Oct 04, 2017 at 09:49 PM
public class RandPerMinute : MonoBehaviour {
float timespanDuration = 60f;
float timesPerTimespan = 10f;
// auditing
int currentTimespan = -1;
int count = 0;
void Update() {
if (timespan != currentTimespan) {
if (currentTimespan >= 0) {
Debug.Log("count for timespan " + currentTimespan + " = " + count);
}
currentTimespan = timespan;
count = 0;
}
float timesPerSecond = timesPerTimespan / timespanDuration;
if (Random.Range(0f, 1f) < timesPerSecond * Time.deltaTime) {
count += 1;
// do your stuff here.
}
}
int timespan {
get {
return (int)(Time.time / timespanDuration);
}
}
}
Your answer
Follow this Question
Related Questions
How to stop getting duplicate numbers while using random.range 1 Answer
Scaling a Line 0 Answers
Multinomial Distribution in Unity? 2 Answers
How to swap any two gameobjects in a field area? 0 Answers
Select random number in series 1 Answer