- Home /
Setting a javascript to function at random frames.
Hi, I'm trying to set up a character. It's default animation is the main "Wait". I'm trying to set it up so at a random moment or time, it will play some other animations that are meant for waiting. Like at sometime, the character turns around, or something like that. I set the parameters to when the parameter is set to 0, it plays default. When it is 1, the character will turn and look around. When it's 2, he looks back, When it is 3, he leans over. I'd like to set a javascript that will set the parameter to change from 0 from randomly 1, 2, and 3. And a frame later the parameter is immediately set back to 0. This function occurs at a random time, but not any less than every 4 seconds. Is this possible?
Thank you.
Yes it is possible,
first define the random range you want it to occur, you said 4 seconds the $$anonymous$$imum , i put 15 seconds as the maximum
var $$anonymous$$ywait : float = Random.Range(4.0, 15.0);
then set up a coroutine,
yield WaitForSeconds($$anonymous$$ywait);
then assign you "parameter" state randomly, because Random.Range will never return its max value we use 3.5 ins$$anonymous$$d of 3 then cast the return as an int
$$anonymous$$yCharacter.state = (int)Random.Range(0, 3.5);
from here you apply the character setting from these states.
Thank you, but can you please type a full example javascript? I'm having trouble.
what part are you having trouble? the co routine? http://docs.unity3d.com/Documentation/ScriptReference/Coroutine.html are you unsure when to call the co routine? well you you want it to run when you're character's input is nothing. make a boolean value so you can stop update from recalling your coutine over an over some pseudo code as a guide
var CouroutineRunning : Boolean = false;
var index : int = 0;
function Update() {
//check if the character inst receiving input and if the co routine is already running.
if(Input.Get$$anonymous$$eyDown($$anonymous$$eycode.UP)) == false && !CouroutineRunning ) {
$$anonymous$$onoBehaviour.StartCoroutine("RandomizeAnimIndex");
}
}
function RandomizeAnimIndex() {
CouroutineRunning = true;
var $$anonymous$$ywait : float = Random.Range(4.0, 15.0);
yield WaitForSeconds (5);
index = (int)Random.Range(0, 4);
CouroutineRunning = false;
}
also where i said 3.5 should be 4 so that they all have an equal chance(near enough) of occurring. if this is confusing you read this:
http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html
it states that Random.Range will never return the max value unless max = $$anonymous$$, which i not the case.
gathering from what you said, you want to randomize phases of Idle? I would just make 1 longish animation clip. then set the playtime position.
the animation code can vary depending on your game structure.
are you using mechiam or Legacy?
Your answer
Follow this Question
Related Questions
Can I make animations snap to a frame? 1 Answer
I'm wanting to play 2 or 3 randomized animation clips 1 Answer
Select frame for Animation 1 Answer
how to make an animation that can take value from script ? 0 Answers
random animations 2 Answers