Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Unity 5 - Playing animation from AnimationState forwards and backwards has no effect

Discussion in 'Animation' started by Spankenstein, Apr 26, 2015.

  1. Spankenstein

    Spankenstein

    Joined:
    Jan 9, 2014
    Posts:
    25
    I have a single default state in my animation controller named SpikeBrightFloor.



    This animation state has two frames which I wish to either play forwards and stop or play backwards and stop depending on a flag called isDeadly.

    This is what I am currently doing:

    Code (csharp):
    1.  
    2.         var animator = GetComponent<Animator>();
    3.         var info = animator.GetCurrentAnimatorStateInfo(0); // Only one state
    4.  
    5.         isDeadly = !isDeadly;
    6.  
    7.         if (isDeadly)
    8.         {
    9.             // Play forwards
    10.             animator.playbackTime = 0f;             // Beginning of state
    11.             animator.speed = 0.1f;
    12.             animator.Play("SpikedBrightFloor");
    13.         }
    14.         else
    15.         {
    16.             // Play backwards
    17.             animator.playbackTime = info.length;    // End of state
    18.             animator.speed = -0.1f;
    19.             animator.Play("SpikedBrightFloor");
    20.         }
    I can get the state and I can get the length of the state and I can set the animator parameters but the animation never seems to play. What is going wrong?
     
  2. Spankenstein

    Spankenstein

    Joined:
    Jan 9, 2014
    Posts:
    25
    Solved:

    Set animator speed to 0f when initialising the script.

    Code (csharp):
    1.  
    2.         var animator = GetComponent<Animator>();
    3.         animator.speed = 0f;
     
  3. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    you don't need to set playbackTime, this is only needed when you do record and playback your recorded animation.

    animator.Play is enough to play "SpikedBrightFloor" from the beginning.