Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Some problems in mecanim animation ?

Discussion in 'Editor & General Support' started by KKS21199, Mar 24, 2013.

  1. KKS21199

    KKS21199

    Joined:
    Nov 22, 2012
    Posts:
    139
    :confused: Hello i saw the mecanim animation tutorial by unity and get to know abt it. So i made my own scene and made a controller with a new player using the same script from unity. I used new animation files from the 150 animation which unity gave for free.

    Here is the script

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. // Require these components when using this script
    5. [RequireComponent(typeof (Animator))]
    6. [RequireComponent(typeof (CapsuleCollider))]
    7. [RequireComponent(typeof (Rigidbody))]
    8. public class BotControlScript : MonoBehaviour
    9. {
    10.     [System.NonSerialized]                 
    11.     public float lookWeight;                    // the amount to transition when using head look
    12.    
    13.     [System.NonSerialized]
    14.     public Transform enemy;                     // a transform to Lerp the camera to during head look
    15.    
    16.     public float animSpeed = 1.5f;              // a public setting for overall animator animation speed
    17.     public float lookSmoother = 3f;             // a smoothing setting for camera motion
    18.     public bool useCurves;                      // a setting for teaching purposes to show use of curves
    19.  
    20.    
    21.     private Animator anim;                          // a reference to the animator on the character
    22.     private AnimatorStateInfo currentBaseState;         // a reference to the current state of the animator, used for base layer
    23.     private AnimatorStateInfo layer2CurrentState;   // a reference to the current state of the animator, used for layer 2
    24.     private CapsuleCollider col;                    // a reference to the capsule collider of the character
    25.    
    26.  
    27.     static int idleState = Animator.StringToHash("Base Layer.Idle");   
    28.     static int locoState = Animator.StringToHash("Base Layer.Locomotion");          // these integers are references to our animator's states
    29.     static int jumpState = Animator.StringToHash("Base Layer.Jump");                // and are used to check state for various actions to occur
    30.     static int jumpDownState = Animator.StringToHash("Base Layer.JumpDown");        // within our FixedUpdate() function below
    31.     static int fallState = Animator.StringToHash("Base Layer.Fall");
    32.     static int rollState = Animator.StringToHash("Base Layer.Roll");
    33.     static int waveState = Animator.StringToHash("Layer2.Wave");
    34.    
    35.  
    36.     void Start ()
    37.     {
    38.         // initialising reference variables
    39.         anim = GetComponent<Animator>();                     
    40.         col = GetComponent<CapsuleCollider>();             
    41.         enemy = GameObject.Find("Enemy").transform;
    42.         if(anim.layerCount ==2)
    43.             anim.SetLayerWeight(1, 1);
    44.     }
    45.    
    46.    
    47.     void FixedUpdate ()
    48.     {
    49.         float h = Input.GetAxis("Horizontal");              // setup h variable as our horizontal input axis
    50.         float v = Input.GetAxis("Vertical");                // setup v variables as our vertical input axis
    51.         anim.SetFloat("Speed", v);                          // set our animator's float parameter 'Speed' equal to the vertical input axis             
    52.         anim.SetFloat("Direction", h);                      // set our animator's float parameter 'Direction' equal to the horizontal input axis       
    53.         anim.speed = animSpeed;                             // set the speed of our animator to the public variable 'animSpeed'
    54.         anim.SetLookAtWeight(lookWeight);                   // set the Look At Weight - amount to use look at IK vs using the head's animation
    55.         currentBaseState = anim.GetCurrentAnimatorStateInfo(0); // set our currentState variable to the current state of the Base Layer (0) of animation
    56.        
    57.         if(anim.layerCount ==2)    
    58.             layer2CurrentState = anim.GetCurrentAnimatorStateInfo(1);   // set our layer2CurrentState variable to the current state of the second Layer (1) of animation
    59.        
    60.        
    61.         // LOOK AT ENEMY
    62.        
    63.         // if we hold Alt..
    64.         if(Input.GetButton("Fire2"))
    65.         {
    66.             // ...set a position to look at with the head, and use Lerp to smooth the look weight from animation to IK (see line 54)
    67.             anim.SetLookAtPosition(enemy.position);
    68.             lookWeight = Mathf.Lerp(lookWeight,1f,Time.deltaTime*lookSmoother);
    69.         }
    70.         // else, return to using animation for the head by lerping back to 0 for look at weight
    71.         else
    72.         {
    73.             lookWeight = Mathf.Lerp(lookWeight,0f,Time.deltaTime*lookSmoother);
    74.         }
    75.        
    76.         // STANDARD JUMPING
    77.        
    78.         // if we are currently in a state called Locomotion (see line 25), then allow Jump input (Space) to set the Jump bool parameter in the Animator to true
    79.         if (currentBaseState.nameHash == locoState)
    80.         {
    81.             if(Input.GetButtonDown("Jump"))
    82.             {
    83.                 anim.SetBool("Jump", true);
    84.             }
    85.         }
    86.        
    87.         // if we are in the jumping state...
    88.         else if(currentBaseState.nameHash == jumpState)
    89.         {
    90.             //  ..and not still in transition..
    91.             if(!anim.IsInTransition(0))
    92.             {
    93.                 if(useCurves)
    94.                     // ..set the collider height to a float curve in the clip called ColliderHeight
    95.                     col.height = anim.GetFloat("ColliderHeight");
    96.                
    97.                 // reset the Jump bool so we can jump again, and so that the state does not loop
    98.                 anim.SetBool("Jump", false);
    99.             }
    100.            
    101.             // Raycast down from the center of the character..
    102.             Ray ray = new Ray(transform.position + Vector3.up, -Vector3.up);
    103.             RaycastHit hitInfo = new RaycastHit();
    104.            
    105.             if (Physics.Raycast(ray, out hitInfo))
    106.             {
    107.                 // ..if distance to the ground is more than 1.75, use Match Target
    108.                 if (hitInfo.distance > 1.75f)
    109.                 {
    110.                    
    111.                     // MatchTarget allows us to take over animation and smoothly transition our character towards a location - the hit point from the ray.
    112.                     // Here we're telling the Root of the character to only be influenced on the Y axis (MatchTargetWeightMask) and only occur between 0.35 and 0.5
    113.                     // of the timeline of our animation clip
    114.                     anim.MatchTarget(hitInfo.point, Quaternion.identity, AvatarTarget.Root, new MatchTargetWeightMask(new Vector3(0, 1, 0), 0), 0.35f, 0.5f);
    115.                 }
    116.             }
    117.         }
    118.        
    119.        
    120.         // JUMP DOWN AND ROLL
    121.        
    122.         // if we are jumping down, set our Collider's Y position to the float curve from the animation clip -
    123.         // this is a slight lowering so that the collider hits the floor as the character extends his legs
    124.         else if (currentBaseState.nameHash == jumpDownState)
    125.         {
    126.             col.center = new Vector3(0, anim.GetFloat("ColliderY"), 0);
    127.         }
    128.        
    129.         // if we are falling, set our Grounded boolean to true when our character's root
    130.         // position is less that 0.6, this allows us to transition from fall into roll and run
    131.         // we then set the Collider's Height equal to the float curve from the animation clip
    132.         else if (currentBaseState.nameHash == fallState)
    133.         {
    134.             col.height = anim.GetFloat("ColliderHeight");
    135.         }
    136.        
    137.         // if we are in the roll state and not in transition, set Collider Height to the float curve from the animation clip
    138.         // this ensures we are in a short spherical capsule height during the roll, so we can smash through the lower
    139.         // boxes, and then extends the collider as we come out of the roll
    140.         // we also moderate the Y position of the collider using another of these curves on line 128
    141.         else if (currentBaseState.nameHash == rollState)
    142.         {
    143.             if(!anim.IsInTransition(0))
    144.             {
    145.                 if(useCurves)
    146.                     col.height = anim.GetFloat("ColliderHeight");
    147.                
    148.                 col.center = new Vector3(0, anim.GetFloat("ColliderY"), 0);
    149.                
    150.             }
    151.         }
    152.         // IDLE
    153.        
    154.         // check if we are at idle, if so, let us Wave!
    155.         else if (currentBaseState.nameHash == idleState)
    156.         {
    157.             if(Input.GetButtonUp("Jump"))
    158.             {
    159.                 anim.SetBool("Wave", true);
    160.             }
    161.         }
    162.         // if we enter the waving state, reset the bool to let us wave again in future
    163.         if(layer2CurrentState.nameHash == waveState)
    164.         {
    165.             anim.SetBool("Wave", false);
    166.         }
    167.     }
    168. }
    169.  
    And all of them worked correctly with moving forward back. I arranged them made it settings and everything. But when i do for jump, it jumps correctly and continue to jump when i press up button. But when i replace it with my old jump file using the sme pace and position it jumps correctly. I know that i missed one setting between that two files. Below in the attachment i have added those two fbx files, please anyone see it and say what I did wrong, if you want more files including the controllers


    View attachment $Anim.rar
     
  2. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I'm at GDC this week. Let me know if you're still having an issue, and I will try to get to this next week.
     
  3. KKS21199

    KKS21199

    Joined:
    Nov 22, 2012
    Posts:
    139
    still in the same situtation..when you see it and got an answer also PM me :D
     
  4. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I don't seem to understand what the problem is. It sounds like you don't have a problem when I read the note you wrote. It sounds like everything is working correctly. This probably is not true.

    Can you please try to describe in detail what exactly is the problem? If you need to, can you make example web-players as well, and host them on drop box, so we can see what the issue is?
     
  5. KKS21199

    KKS21199

    Joined:
    Nov 22, 2012
    Posts:
    139
    The problem i am having is, when i make a bot controller of my own using the 150 animation files given by unity, i am having problem,

    For example, The jump animation, when i press spacebar it jumps then again it jumps and continue to jump whenever i press the vertical up key, the jump animation was connected to the locomotion so i can't run once i pressed the up arrow.

    I have searched google, and disabled the Loop Pose animation.

    First try the files I attached in this forum, i will now create a example game.
     
  6. KKS21199

    KKS21199

    Joined:
    Nov 22, 2012
    Posts:
    139
    r u there
     
  7. KKS21199

    KKS21199

    Joined:
    Nov 22, 2012
    Posts:
    139
  8. juliobds

    juliobds

    Joined:
    Dec 27, 2011
    Posts:
    25
    Check the condition that gets out of the jump animation in the animator, the condition is probably never true.
     
  9. KKS21199

    KKS21199

    Joined:
    Nov 22, 2012
    Posts:
    139
    no, it's truee.
     
  10. juliobds

    juliobds

    Joined:
    Dec 27, 2011
    Posts:
    25
    Then if you see it get out of the animation it should go to another one, either an idle animation or whatever you have set it up to be. Make sure the condition in that animation isn't true making it go back to the jump animation.
     
  11. KKS21199

    KKS21199

    Joined:
    Nov 22, 2012
    Posts:
    139
    did u play the demo game link i gave, the two levels in the game use the same script and same animation except for the jump, one is working, the jump animation from unity mecanim tutorial and other the 150 anims downloaded from assets store is looping, settings are same, download the file : View attachment $Anim.rar

    And there will be the two .fbx files, i can upload project files if needed
     
  12. juliobds

    juliobds

    Joined:
    Dec 27, 2011
    Posts:
    25
    I tried to play it, but as soon as I pressed the forward key to move the character the game crashed. In both scenes.
     
  13. KKS21199

    KKS21199

    Joined:
    Nov 22, 2012
    Posts:
    139
  14. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I'm not sure what you've done or how you've done it... but the webplayer has a hard crash when you run it and the app that you've set to download has a hard crash that it going to force me to restart my machine...

    For you specific issue, it sounds like your blend tree in mechanim is incorrect, and you are not properly handling the end of the jump.

    Jumps are handled in the mechanim tutorial:
    http://forum.unity3d.com/threads/159894-Mecanim-Unity-4-0-starter-tutorial

    Can you give me any more information about how you are building these applications/webplayers? There is something fatally wrong in the way you are making them.

    What version of Unity are you running? If it's the free version, did you get it from our website? Can you delete/uninstall Unity and reinstall it? I have never seen this behaviour on a player before.

    Can YOU run the application or webplayer?