Search Unity

Play animation with scripting and Mecanim.

Discussion in 'Animation' started by rjdfhorn2006, Jan 8, 2014.

  1. rjdfhorn2006

    rjdfhorn2006

    Joined:
    Jun 14, 2010
    Posts:
    141
    Is it possible to play an animation with only code using the Mecanim system? I want to avoid setting up speed variables and inputs shown in the Mecanim tutorial video - I simply want to animate a model using the animation files provided by the Mecanim system and other animated files. I was hoping you could do this through simple scripting (similar to how the Legacy animation system works, e.g. Animation.Play("Run"). I've set up a character avatar/controller with Mecanim - i've placed several animation states into the Animator window - is it possible to use only scripting to play each one of these animations separately? For example, I would want to use something like:

    Code (csharp):
    1.  
    2. if(Input.GetKeyDown("j"))
    3. {
    4.           Animator.Play("Run");
    5. }
    6.  
    This doesn't work obviously, and nothing else i've tried works - is it even possible to do something like this using Mecanim?
     
  2. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    945
    What features do you need from Mecanim, can't you use the legacy system then?
     
  3. jRocket

    jRocket

    Joined:
    Jul 12, 2012
    Posts:
    700
    You'll still need to set up your states, but could you simply have each state correspond to an animation and play the state with Animator.Play()?