Search Unity

Mecanim adding animation clips from scripts

Discussion in 'Scripting' started by Oxnard, Nov 22, 2012.

  1. Oxnard

    Oxnard

    Joined:
    Nov 22, 2012
    Posts:
    2
    Hey guys,
    Is it possible to add new animation clips to AnimatorController from scripts? And how can I change existing clips and manage blend trees from scripts. Tell me please is it possible?
     
  2. Ben-Massey

    Ben-Massey

    Joined:
    Jun 13, 2011
    Posts:
    581
  3. Oxnard

    Oxnard

    Joined:
    Nov 22, 2012
    Posts:
    2
    Yep. I have a character with Animator component, then I created AnimatorController and add there my animation clips. My question is how can I change for example idle clip to idle1 clip in my AnimatorController from scripts at runtime.
    Sorry for my poor English.
     

    Attached Files:

  4. huang

    huang

    Joined:
    Aug 16, 2012
    Posts:
    34
    i want to know that too
     
  5. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    Blend trees, and nested blend trees. You can find a vid for this on Unity Yoo tube.
     
  6. huang

    huang

    Joined:
    Aug 16, 2012
    Posts:
    34
    thanks fore your reply.

    different weapon have different change weapon animation. i must check the weapon type, then play the associate animation of that.


    reload ak47 and reload m4 must be have different animation .i can't use the script to play a custom animation clip.
     
  7. dextradei

    dextradei

    Joined:
    Nov 2, 2012
    Posts:
    1
    One thing you can do is use an integer parameter "gunType" in the AnimatorController which tells the state machine which gun is equipped. (0=ak, 1=m4, etc)
    now add all your reload clips for each weapon type to your reloading layer in the AnimationController
    for each clip, add an empty clip and make a transition between the empty clip and the reloading clip and vice versa (use your reload AnimationController parameter for these transitions, maybe a boolean called "reload" going into the reload animation, and "exit time" 0.8 or something to get back to the empty)
    Then, make a "ring" of transitions between the empty states where each transition has the condition "gunType" "not equal" "n" where n is the number for the weapon type. so akEmpty->m4Empty when "gunType" "not equal" "0", m4Empty->mp5Empty when "gunType" "not equal" "1", mp5Empty->awpEmpty when "gunType" "not equal" "2", etc until you get to the last reload animation, make it transition back to akEmpty (gunType 0, the first one)
    Then, in your script, when the player changes weapon, you call animator.SetInteger("gunType", n), and the animation state machine will change states around the empty ring until it stops on the empty state for the new weapon, and when the reload parameter is set (animator.SetBool("reload", true) or something), the correct reload animation will be played.
    Make sure the ring transitions are instant by dragging one of the 2 sides of the crossfade area onto the other side in the inspector when then transition is selected.