Search Unity

Grounded animation!

Discussion in 'Scripting' started by Sam Shiels, Oct 26, 2007.

  1. Sam Shiels

    Sam Shiels

    Joined:
    Feb 25, 2007
    Posts:
    160
    Hi, I got a character to play an Idle animation when it's grounded. The only problem is that when I move it the Idle animation wont stop. If I take away the Idle animation the air animation still plays even when I'm grounded!

    Here's the script:

    Code (csharp):
    1.  
    2. var animationRoot : Animation;
    3.  
    4. function Update ()
    5. {
    6.     var controller : CharacterController = GetComponent(CharacterController);
    7.     if (controller.isGrounded)
    8.     {
    9.     print("Grounded");
    10.     animationRoot.Play("Idle");
    11.     }
    12.         else {
    13.     animationRoot.Play("Jump");
    14.     }
    15. }
    16.  
    Any help will be great!

    Thanks.
     
  2. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    It looks like a bit of the old if else treatment might be needed. This works but havent tried it with animation yet. Let us know eh?
    Code (csharp):
    1.  
    2. function Update ()
    3. {
    4.     var controller : CharacterController = GetComponent(CharacterController);
    5.     if (controller.isGrounded)
    6.     {
    7.     print("Grounded");
    8.  
    9.     }
    10.        else if (!controller.isGrounded) {
    11.    
    12.         print(" NotGrounded");
    13.     }
    14. }
    HTH
    AC
     
  3. Sam Shiels

    Sam Shiels

    Joined:
    Feb 25, 2007
    Posts:
    160
    Thanks. I'll try it tomorrow.
     
  4. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    Code (csharp):
    1.  
    2. function Update ()
    3. {
    4.     var controller : CharacterController = GetComponent(CharacterController);
    5.  
    6.     if (controller.isGrounded)
    7.     {
    8.         print("Grounded");
    9.     }
    10.     else if (!controller.isGrounded)
    11.     {
    12.         print(" NotGrounded");
    13.     }
    14. }
    15.  
    Is exactly the same as:

    Code (csharp):
    1.  
    2. function Update ()
    3. {
    4.     var controller : CharacterController = GetComponent(CharacterController);
    5.  
    6.     if (controller.isGrounded)
    7.     {
    8.         print("Grounded");
    9.     }
    10.     else
    11.     {
    12.         print(" NotGrounded");
    13.     }
    14. }
    15.  
    Printing not grounded is a good idea though. Another thing is it might be better to do it on an event basis, so that the animation is changed once when something happens, for example when the player lands instead of once per frame.
     
  5. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Thanks Yoggy, I had no idea.

    This script works, but if you follow Yoggys lead, you can change it how u like:
    Code (csharp):
    1. function Update ()
    2. {
    3.     var controller : CharacterController = GetComponent(CharacterController);
    4.     if (controller.isGrounded)
    5.     {
    6.     print("Grounded");
    7.     animation.Play("idle");
    8.  
    9.     }
    10.        else if (!controller.isGrounded) {
    11.    
    12.         print(" NotGrounded");
    13.         animation.Play("jump");
    14.     }
    15. }
    16.  
    AC