Search Unity

Start animation at random frame

Discussion in 'Scripting' started by khanstruct, May 29, 2015.

  1. khanstruct

    khanstruct

    Joined:
    Feb 11, 2011
    Posts:
    2,869
    This is for a simple sliding animation for my UI. I just want one of the images to begin its animation in the middle of its loop.

    In the old script, it would be something like animation["state"].time = whatever.

    But this no longer works :(

    I've looked, but can't seem to find how to do this with Unity5. Can someone help?

    Thanks!
     
  2. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
  3. khanstruct

    khanstruct

    Joined:
    Feb 11, 2011
    Posts:
    2,869
  4. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Could you paste the error? :)
     
  5. khanstruct

    khanstruct

    Joined:
    Feb 11, 2011
    Posts:
    2,869
    No. I am poor. I have no internet at home, so I have to pose my questions while I'm at work, then go home to test the answers... then wait until the next day to say it didn't work. :(

    But in that particular example, it's not a runtime error. AnimationState doesn't have a "time". It also says that AnimationState is depreciated, and that I should use something like GetAnimatorContentInfo (or something like that). I've tried a thousand different things with that and all it ever has is one item in an array called ContentInfo... or AnimatorInfo... which I can't seem to do anything with (it too does not have "time").
     
  6. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Oh wait, do you use the latest version of unity!? I hate the almost exact problem with animationstate, and it got solved by just downloading the latest Unity3D 5.0.2f or something. Just go on the website and download the latest download assistant :)
     
  7. khanstruct

    khanstruct

    Joined:
    Feb 11, 2011
    Posts:
    2,869
    :(
     
  8. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you can pass the normalized time (i.e. 0 to 1) you want the animation to play from as the optional third parameter of Animator.Play(...)
     
    paupratscifp, Aseemy and rakkarage like this.
  9. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    @OP: if you're using Unity 4, don't use the documentation at http://docs.unity3d.com/ScriptReference/, as that's for Unity 5. Instead, open Unity and select Help -> Scripting reference. That'll give you the same docs, but for your version of Unity.
     
  10. khanstruct

    khanstruct

    Joined:
    Feb 11, 2011
    Posts:
    2,869
    I'm using Unity 5, just not sure its the most recent version of 5.
     
  11. khanstruct

    khanstruct

    Joined:
    Feb 11, 2011
    Posts:
    2,869
    Ah! It worked!
    For those who may need it:
    Code (CSharp):
    1. thisAnim.Play("Smoke", -1, Random.Range(0.0f, 1.0f));
    (thisAnim is my animator, and Smoke is the name of the animation state).

    Thanks!
     
    Dibloo, neginfinity, ElnuDev and 3 others like this.
  12. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Thanks for posting the solution! :)
     
    ElnuDev and khanstruct like this.
  13. ElnuDev

    ElnuDev

    Joined:
    Sep 24, 2017
    Posts:
    298
    Thanks so much! This still works in 2018.2.2f1!
     
    khanstruct likes this.
  14. HeyBishop

    HeyBishop

    Joined:
    Jun 22, 2017
    Posts:
    238
    I'm using 2018.3.0f2 and it does not seem to be working. :(
     
  15. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    You can now use something like:

    Code (CSharp):
    1. panelFullUI[animationName].time = time;
    2.             panelFullUI.clip = panelFullUI[animationName].clip;
    3.             panelFullUI.Sample();
    4.             panelFullUI.Play();
     
  16. HeyBishop

    HeyBishop

    Joined:
    Jun 22, 2017
    Posts:
    238
    I just figured out what was happening - I was instantiating an object and thisAnim.Play("Smoke", -1, Random.Range(0.0f, 1.0f)); was in the Start() function. I switched it to OnEnbable() and got the results I was looking for.