Search Unity

Animation Blending problems

Discussion in 'Scripting' started by Johker, Mar 29, 2011.

  1. Johker

    Johker

    Joined:
    Jun 24, 2010
    Posts:
    24
    I am playing around with animation blending and have an issue where my "throw" animation only gets played once, on the first button press, and never again after that. The "idle" and "walk" animations works perfectly. I have a feeling Im missing something really simple but cant seem to find it. Any help would be greatly appreciated.

    Here is my script :

    Code (csharp):
    1.  
    2. public class CharacterAnimations : MonoBehaviour
    3. {
    4.     private Vector3 velocity = Vector3.zero;
    5.     private CharacterController controller;
    6.    
    7.     private AnimationState idleAnimation;
    8.     private AnimationState walkAnimation;
    9.     private AnimationState throwAnimation;
    10.    
    11.     private void Start()
    12.     {
    13.         controller = GetComponent(typeof(CharacterController)) as CharacterController;     
    14.        
    15.         // Idle Animation
    16.         idleAnimation = animation["idle"];
    17.         idleAnimation.layer = -2;
    18.         idleAnimation.blendMode = AnimationBlendMode.Blend;
    19.         idleAnimation.wrapMode = WrapMode.PingPong;
    20.         idleAnimation.enabled = false;
    21.         idleAnimation.weight = 1.0f;
    22.        
    23.         // Walk Animation
    24.         walkAnimation = animation["walk"];
    25.         walkAnimation.layer = -1;
    26.         walkAnimation.blendMode = AnimationBlendMode.Blend;
    27.         walkAnimation.wrapMode = WrapMode.Loop;
    28.         walkAnimation.enabled = false;
    29.         walkAnimation.weight = 1.0f;
    30.        
    31.         // Throw Animation
    32.         throwAnimation = animation["throw"];
    33.         throwAnimation.layer = 1;
    34.         throwAnimation.blendMode = AnimationBlendMode.Blend;
    35.         throwAnimation.wrapMode = WrapMode.Once;
    36.         throwAnimation.enabled = false;
    37.         throwAnimation.weight = 1.0f;
    38.         throwAnimation.AddMixingTransform(transform.Find("Character_Root/Character_Spine"));
    39.    
    40.         animation.Stop();
    41.         animation.Play("idle");
    42.     }
    43.    
    44.     private void Update()
    45.     {
    46.         Vector3 velocity = controller.velocity;
    47.        
    48.         if (velocity != Vector3.zero)
    49.         {
    50.             animation.CrossFade("walk");
    51.         }
    52.         else
    53.         {      
    54.             animation.Blend("walk", 0.0f, 0.3f);
    55.         }
    56.        
    57.         if ((Input.GetButtonDown("Attack"))  (!animation.IsPlaying("throw")))
    58.         {
    59.             animation.CrossFade("throw");
    60.         }
    61.     }
    62. }
    63.  
    64.  
     
  2. Johker

    Johker

    Joined:
    Jun 24, 2010
    Posts:
    24
    It would appear that I dont fully understand how animation weights and blending works. When I set the weight to 1.0f just before I call animation.CrossFade("throw") it works perfectly.

    Code (csharp):
    1.  
    2. private void Update()
    3.     {
    4.  
    5.         if ((Input.GetButtonDown("Attack"))  (!animation.IsPlaying("throw")))
    6.         {
    7.                         throwAnimation.weight = 1.0f;
    8.             animation.CrossFade("throw");
    9.         }
    10.     }
    11.  
    So an animaition looses it's weight after it has CrossFaded, even if it is set to WrapMode.Once? Anyone knows how it works?
     
  3. amir_ro

    amir_ro

    Joined:
    Sep 8, 2010
    Posts:
    5
    You may verify the states of your animations using the debug mode in the inspector. You can also check AnimationsWatcher - a plug-in in the asset store that I've written to provide a more user friendly status of these animations