- Home /
Random actions time based
Hi, i have this script that plays random animations on my character, works well the problem is that is frame based. The question is how would you make it so it would be time based? Also do i need to move the var random : int = Random.Range(1,5) to a start func.? It keeps giving me the error that needs to be moved to a start or awake.
var count : float = 0; var switchSpeed: float = 1000; var rangeDice: float = 5; var random : int = Random.Range(1,5);
function Update() { count++; if(count >= switchSpeed) { count = 0; CreatePlane(); random = Random.Range(1,rangeDice); } }
function CreatePlane() { switch(random) { case 4: // Plays animation 4 break; case 3: // Plays animation 3 break; case 2: // Plays animation 2 break; case 1: // Plays animation 1 break; } }
What do you mean "time-based"? You want the actions to be performed at certain times, you want there to be a certain amount of time between random actions, etc.?
Yes. Because when testing the game on different machines the actions are performed much faster or much slower dpending on the framerate. Another example testing the game on the Editor the actions are performed much slower that on a build.
Answer by Statement · Jan 09, 2011 at 02:29 PM
// Changed switchSpeed to 1, meaning one 1 second. var switchSpeed : float = 1; var rangeDice : float = 5;
// When the next animation should play. private var nextTime : float = -1;
function Update() { if(Time.time >= nextTime) { nextTime = Time.time + switchSpeed; PlayAnimation(Random.Range(1, rangeDice)); } }
// Changed name to PlayAnimation to better reflect its purpose. function PlayAnimation(animationId : int) { switch (animationId) { case 4: // Plays animation 4 break; case 3: // Plays animation 3 break; case 2: // Plays animation 2 break; case 1: // Plays animation 1 break; } }
Note that you can probably condense this code into
var switchSpeed : float = 1; var rangeDice : float = 5;
InvokeRepeating("PlayRandomAnimation", float.Epsilon, switchSpeed);
function PlayRandomAnimation() { PlayAnimation(Random.Range(1, rangeDice)); }
function PlayAnimation(animationId : int) { switch (animationId) { // ... omitted redundant code } }
and I guess you could simply use an array with legal clips to further simplify this to
var switchSpeed : float = 1; var animations : String[];
InvokeRepeating("PlayRandomAnimation", float.Epsilon, switchSpeed);
function PlayRandomAnimation() { var index = Random.Range(0, animations.Length);
animation.Play(animations[index]); }
Answer by Peter G · Jan 09, 2011 at 02:30 PM
Use a coroutine. Its better for performance than calling Update every frame.
var spawnTime : float; var rangeDice : int = 5;
function Start () { while(true) { CreatePlane(Random.Range(1, rangeDice)); yield WaitForSeconds(spawnTime); //Wait x seconds. } }
CreatePlane (value : int) { //Do your stuff here; }
or even easier, just use InvokeRepeating()
var spawnTime : float; var rangeDice : int = 5;
function Start () { InvokeRepeating("CreatePlane", spawnTime, spawnTime); //InvokeRepeating("Method", delayTime, repeatTime);
}
CreatePlane () { var randomValue : int = Random.Range(1, rangeDice); //Do your stuff here; }
I am just a bit curious about how a coroutine has better performance over Update?
It is my understanding that Update is called via Reflection internally and that isn't real great for performance. So using coroutines basically just saves method calls which saves the method call overhead + reflection just like InvokeRepeating() is more efficient than Update as well.
If you want to see this, create a custom Update system have an event that fires once per frame using a coroutine i.e. void Start() { while(true) { eventFire(); yield return null; } } then ins$$anonymous$$d of Update, attach delegates to your event for each "tick" i.e. void Start () { someClass.eventFire += someDelegate(someUpdateFunction); } If you do some tests, you should notice that a custom tick manager is more efficient than calling Update().
Your answer
Follow this Question
Related Questions
Timed event Question 2 Answers
Random AudioSource and Animation? 0 Answers
How can I read time from clip that is PlayQueued? 1 Answer
Randomized weather, little stuck 1 Answer