Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Script to start animation at a random frame

Discussion in 'Scripting' started by DaveyJJ, Jul 2, 2007.

  1. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    I have an animation ("sway1") that has 30 frames in it then loops. I'd like to start the animation at a random one of the 30 frames and also some random time after the scene starts ... so can someone help me round this out please? I'll attach it to a prefab that is then placed in the scene.

    Code (csharp):
    1.  
    2.  
    3. var startAnimation = Random.Range(1, 10);
    4. var startFrame = Random.Range(1, 30);
    5.  
    6. function Start ()
    7. {
    8.   // animate me after startAnimation seconds at startFrame of sway1
    9. }
    10.  
    Thanks!

    PS. Happy Canada Day ... fireworks with the kids in an hour :D
     
  2. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Maybe something like this:


    Code (csharp):
    1.  
    2.  
    3. function Start ()
    4. {
    5.     yield WaitForSeconds (Random.Range (0.0, 10.0));
    6.     animation ["sway1"].time = Random.Range (0, animation ["sway1"].length);
    7.     animation.Play ("sway1");
    8. }
    9.