Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Obtaining all AnimatorStateInfo from Mecanim

Discussion in 'Animation' started by RamzaB, Jan 20, 2015.

  1. RamzaB

    RamzaB

    Joined:
    Nov 4, 2012
    Posts:
    42
    Last edited: Jan 22, 2015
  2. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Right, AnimatorStateInfo is a struct filled by the animator while in play mode. It does contain all the information of the current state played by the animator or the next one to be play while in transition.

    What would you like to change by script? And would you like to change those values at runtime or in the editor?
     
  3. RamzaB

    RamzaB

    Joined:
    Nov 4, 2012
    Posts:
    42
    I was thinking to modify the speed of the AnimationStateInfo by script. For example, I have two characters which have a running animation, and they share the same Animator prefab, but
    - Character A runs faster, so the speed for the running animation should be set to 2
    - Character B runs normal, so its running animation speed is 1
    The speed data itself is stored on a another component, (PlayerComponent for example).
    Is it possible to do this ? If possible, I don't want to change the speed of the Animator itself though.

    Another thing that my colleague is currently thinking is for debugging purposes. He would like to have a combobox displayed during runtime, which consists of all the AnimationStateInfo that a selected character has.
    If the value of the combobox is changed, then the selected character should perform the selected animation.
    Any ideas how to do this ?
     
  4. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    You cannot change the speed of an animation at runtime in 5.0, we are currently working on this feature for 5.1

    There is no way to query all the state from controller at runtime, you would need to run a script in the editor that would store all the state name or hash id from the controller, and then at runtime use this information to show your combo box and call Animator.Play() to trigger the animation.

    http://docs.unity3d.com/ScriptReference/Animator.Play.html
     
  5. RamzaB

    RamzaB

    Joined:
    Nov 4, 2012
    Posts:
    42
    I see. Could you please let me know how to query the available states using a script in the editor then ?
     
  6. vanmani

    vanmani

    Joined:
    Jul 11, 2012
    Posts:
    6
    It's a nightmare. And you'd be solving a problem that shouldn't exist in a counterintuitive hacky way. Resurrecting this thread a bit because I recently wrote up my feelings about the lack of runtime access to the state machine in the current version of Mecanim, with the somewhat antagonistic title: Mecanim is broken and needs to be fixed properly. Posting this here hoping that Mecanim.Dev will see it and weigh in on it ;)

    At the end of the day it's a design problem. The state machine itself is not cloned at runtime, so making changes to it at runtime would be problematic, thus they're not exposed to us. This is contrary to most Unity designs, where the object is instanced at runtime, and changes made to an object at runtime have no impact upon the original source object.

    As Mecanim.Dev said, there's a solution to this scheduled for 5.1 which allows you to drive the animation speed using an animation parameter. This doesn't really go far enough in my view - it solves this particular problem, and will satisfy me in the short term, but I'd rather we could actually access the guts of the mecanim state machine at runtime and do what we will. Professional game developers don't like having their hands held and being told "no, that's not allowed.", particularly when it seems obvious that it should be.

    No disrespect intended though: I love Unity and everything it represents and enables. This one issue with Mecanim does bug me though.
     
  7. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    Hey @vanmani can you give a couple examples beyond the OP how having access to the state machine would be used in a practical manner?
    Or possibly give a couple examples how this restricts the use of mecanim in the current build?

    Thanks -
    Please use small words - I'm an animator not a programmer. :)
     
  8. AlleywayNick

    AlleywayNick

    Joined:
    Jan 26, 2014
    Posts:
    26
    Vanmani, you are 100% correct. I hope Unity fixes this asap!

    Is there a timeframe on the release of 5.1?
     
    Last edited: Apr 20, 2015
  9. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    vanmani - I reread your post and now understand the issue you have pointed out, and agree.