How I can set so this button will play a random animation attached to my model?
I have 20 animation on the 3d model but I want to play one of the 20 animation randomly each time i press the button.
var hand : GameObject;
function OnGUI(){
if (GUI.Button (Rect (105, 457, 100, 22), "Quiz")) {
hand.animation.Play("J");
}
i don’t understand the addclip. I imported my .fbx files and did the split of the animation on the object. I see the different animation clips on my object.
i created first array and manual dropped my animation in their.
var allmotions : AnimationClip[ ]; → so that doesn’t work than?
I meant something like this (again, quick and untested) – I don’t know why you didn’t add the clips to the animation directly though and call them there, but this uses the allmotions array that you had (I guess you want it that way for some other reason?)
var allmotions : AnimationClip[];
function Start () {
for (var i = 0; i < allmotions.Length; i++)
animation.AddClip(allmotions[i], "random"+i);
}
function OnGUI(){
if (GUI.Button (Rect (105, 457, 100, 22), "Quiz")) {
var random = Random.Range(0, allmotions.Length);
animation.Play("random"+random);
}
}
The Animation.Play function only recognizes strings (not AnimationClips for some reason).
The only way I’ve found that works is to change this:
var allmotions : AnimationClip[];
Into this:
var allmotions : String[];
And then type the names of the animations into the array.
I’m sure there’s a way to convert a value into a String within the script, but I have yet to find the function. Someone what to enlighten us?
Here’s the rest of the code:
var allmotions : String[];
var hand : GameObject;
function OnGUI()
{
var randomAnim = allmotions[Random.Range (0, whatever)];
if (GUI.Button (Rect (105, 457, 100, 22), "Quiz"))
{
hand.animation.Play (randomAnim);
}
}
Edit: After reading Charles post, I remembered THIS little gem (I changed the hand.animation.Play Line):
var allmotions : AnimationClip[];
var hand : GameObject;
function OnGUI()
{
var randomAnim = allmotions[Random.Range (0, whatever)];
if (GUI.Button (Rect (105, 457, 100, 22), "Quiz"))
{
hand.animation.Play ("" + randomAnim);
}
}
“” + Value means that the variable will convert it’s value to a string. I can’t believe I forgot about it!
I have a need to play random animations also. I tried each code sample in this post without success. If I hard code the animation name in the animation.Play call that animation will play. I cannot get the random substitution working.
What is going wrong? Does the same animation play each time, do they play in non-random order…? Maybe you can post something you’ve tried and explain what goes wrong with it.
If I hard code the name of the animation in the animation.Play call then that specific animation plays when the user touches the screen.
example:
animation.Play(“idle_breath”); plays a specific animation of the puppet breathing
but:
The following displays the breathing puppet but nothing happens when I touch the screen. If I hardcode any of the animations into the animation.Play call in the Update function then that specific animation will play when I touch the screen.
The call to the Random function in the above code snippet is not commented out in my running example. It was commented out in this specific version of the script because I was testing.
Try using “iPhoneInput.touchCount > 0” as the condition. Also, the second parameter to Random.Range should be the number of items in the array (ie, one greater than the highest index).
Thanks for the tips. The entire script is included below. I still get the same behavior. I’ve tried each of the calls to Random with the same result. If I hard code an animation name in the “touch if” block then that animation will play. Any other thoughts you might have after seeing this script are really appreciated.