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

Controlling Animations Pragmatically

Discussion in 'Animation' started by lordpidey, Apr 16, 2015.

  1. lordpidey

    lordpidey

    Joined:
    Apr 16, 2015
    Posts:
    3
    Hello, I am working on an entirely 2d, sprite based game, and I think I've coded myself into a hole, and I want to see what people think the best solution is. I am a fairly good programmer, but a novice at Unity.


    So, the player has a horrendously complex state-machine, which interacts with many other things besides the animator.

    As such, I have created a custom script to handle the state machine.
    The script works perfectly.

    However, I have no idea how is best to tie the state machine script to the actual animator.
    Complicating the issue, is that sometimes the state machine shifts into the same state, which should have the animation restart.
    It would also be helpful, but not absolutely necessary for the state machine to know when an animation finishes.

    Anyway, what is the best way for me to proceed?
     
  2. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    I do not understand what you are trying to achieve. What do you mean by tie the script to the animator? You can drag and drop the script on the game object that hold that animator and it should be 'tie' to the same gameobject.
     
  3. lordpidey

    lordpidey

    Joined:
    Apr 16, 2015
    Posts:
    3
    Perhaps it would be easiest if I wrote some code as an example.

    Code (CSharp):
    1. protected void enterIdleState()
    2. {
    3.    do_other_things();
    4.    GetComponent<Animator>().playAnimation("resourcedirectory/idle.anim");
    5. }
    6.  
    7.  
    8. protected void handleIdleState()
    9. {
    10.    if (GetComponent<Animator>().AnimationOver()){
    11.      do_something();
    12.      return;
    13.    }
    14.    if (someCondition){
    15.      enterIdleState();
    16.      // restart animation from frame zero, and re-perform state entrance logic if somecondition is met
    17.    }
    18.    do_other_things();
    19. }
    20.  

    I simply want some way, via scripting to tell the animator to immediately play a specific animation. It would also be nice (but not AS important) to have some way to tell when an animation has reached it's last frame.

    Ideally, I'd like to not have to mess with the state machine in the animator at all. It just seems too imprecise for what I want.
     
  4. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Why you found it to imprecise?

    In 5.0 we did add State machine beahviour which is a script component with some predefine callback like
    OnStateEnter
    OnStateUpdate
    OnStateExit
    http://docs.unity3d.com/ScriptReference/StateMachineBehaviour.html

    If you don't want to mess with a controller then you should use the legacy animation component
     
  5. lordpidey

    lordpidey

    Joined:
    Apr 16, 2015
    Posts:
    3
    I found it imprecise For several reasons.

    The first is how you set blending. by moving sliders with the mouse. I saw no way to numerically input this. I want something to start at zero seconds, not 0.0002 seconds, since that's as close as I could put my mouse to it.

    The second is that AFAICT there is no way for a script to IMMEDIATELY start an animation, the closest thing that can be done is to create a parameter, and modify that parameter which will cause the animation to start one frame later.
     
  6. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Which version are you using?
    In 5.0 you can enter any numeric value you want from the editor, also you always have the scripting API to build your controller. See attached picture

    Simply use Animator.Play to play any state from your controller.
    http://docs.unity3d.com/ScriptReference/Animator.Play.html


    transitionsetup.png
     
    theANMATOR2b and rakkarage like this.