Search Unity

Trigger animation from code / Disable state blending

Discussion in '2D' started by honigkuchen, Nov 25, 2013.

  1. honigkuchen

    honigkuchen

    Joined:
    Nov 24, 2013
    Posts:
    7
    Hey Ho!

    Is there any chance to trigger an animation from code?
    The new Animator is driving me crazy...
    I create a state machine myself, if i need one. Am i really forced to use the Animator (States, Transitions and stuff) to trigger animations?
    And maybe any idea how to disable state blending in the transition inspector? Maybe i don't get the advantages of this whole new system, but tk2d still seems to be the right choice for a 2D sprite game :/

    Cheers!
     
  2. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    You don't need to use the animator, just add the animation component to your gameobject and then you can play animations from it in code.
     
  3. honigkuchen

    honigkuchen

    Joined:
    Nov 24, 2013
    Posts:
    7
    When i drag the Animation onto the GameObject, it automagically adds an Animator and creates a new Controller associated with the Animation :/
    All i want to do is call something like
    Code (csharp):
    1. gameObj.Animator.Play("Idle")
    So easy... and yet so impossible? ^^

    Cheers!
     
  4. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Your skipping a step. Unity is assuming you want to use the Animator Component if you start by dragging the Animation onto the gameobject. Start by Selecting your gameobject and from the top select Components>Miscellaneous>Animation. This will add an Animation "Component" to the object.
    see: http://docs.unity3d.com/Documentation/ScriptReference/Animation.html

    The Animation Component has a list of Animations that you can put all of your animations in and then reference from a script.

    There is a distinction between an Animation Asset which stores the animation information and an Animation Component which manages an object's animations at runtime.
     
    Last edited: Nov 26, 2013