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. Dismiss Notice

[Solved] Looping on an animation

Discussion in 'Animation' started by Boogafreak, Jan 9, 2019.

  1. Boogafreak

    Boogafreak

    Joined:
    Oct 2, 2017
    Posts:
    42
    Hi,

    Please help if you can :
    I have a task and there seems to be several ways to go at it, but I'm getting confused on how to do it --
    I need to move game objects 90*X pixels, where X is an integer between 1 and 9 (this depends on board situation). The animation graph is linear (constant speed).

    I can think of :
    1. Making 9 animations, one for each movement, but that seems bad practice..
    2. Loop X times on same animation - Can't find a good explanation on how to do this without waiting for Idle mode each time, which may break animation fluidity..
    3. Place this X integer in an animation parameter and use it somehow (how??)

    Any Advice would help.
    Booga.
     
  2. Boogafreak

    Boogafreak

    Joined:
    Oct 2, 2017
    Posts:
    42
    Well, for all it may concern, I combined #2 and #3 into a working scheme :
    1. Place an integer parameter ("loop_times")to the animation.
    2. When you want the animation to start looping, set it to loop times in code (using animator.SetInteger())
    3. Place a condition of (loop times > 0) to get out of idle to the animation.
    4. On the animation state, implement OnStateEnter() and reduce the number of loops on "loop_times" parameter :
    animator.SetInteger("fall_loops", animator.GetInteger("fall_loops") - 1);

    Hope this helps anyone else :)
    Booga.
     
  3. lxmllr

    lxmllr

    Joined:
    Mar 27, 2018
    Posts:
    6
    Mildly related: Can you explain how to get OnStateEnter() to fire on each loop? It's only firing on the first pass through the state for me and I can't for the life of me figure out why