Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Animation Script

Discussion in 'Scripting' started by htwarrior, Sep 27, 2006.

  1. htwarrior

    htwarrior

    Joined:
    Feb 25, 2006
    Posts:
    40
    Hello, I am having some problems animating a model using animation.CrossFade between two animations. I hope somebody have a better idea .My code is :

    var animationRoot : Animation; // animationRoot from the inspector

    function Start () {

    animationRoot.wrapMode = WrapMode.Loop;

    }


    function Update(){

    if(HorseSpeed > 0.0 HorseSpeed <= 50.0 ) {
    animationRoot.CrossFade("run", 0.1); // I have tried 0.3

    }

    if(HorseSpeed > 50.0 HorseSpeed < 70.0) {
    animationRoot.CrossFade("whip", 0.1); // I have tried 0.3

    }

    }


    But I still have jumps betweeen animations, I don't undertand well the concept of blending in Unity. Is it blending the answer?

    hTwarrior
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    The first thing is to make sure the animations loop correctly without blending.

    Just set the run as the default animation clip and see if it plays/loops right. Then set the whip clip to be the default clip and see if it loops right.

    If that works your script seems to correct. Btw. the longer the cross fade the smoother the blend will be of course.

    The last thing to consider is of course, that you might need to synchronize the animation cycles, if they run at different frequencies:
    http://unity3d.com/Documentation/ScriptReference/Animation.SyncLayer.html
     
  3. htwarrior

    htwarrior

    Joined:
    Feb 25, 2006
    Posts:
    40
    The sync did the job! thank you. Good job guys.

    htwarrior
     
  4. llavigne

    llavigne

    Joined:
    Dec 27, 2007
    Posts:
    977
    on the synch doc, you have to put all animation to the same layer, it seems that if i then set one default animation to layer -1 this doesn't become the default and later in the script, when i do CrossFade ("default anim") this doesn't work - until i remove the animation["default"].layer = -1.
    How do I solve this more elegantly ?

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class EagleControl : MonoBehaviour
    6. {
    7.     public float speedGlide = 1f;
    8.     public float speedNormal = 2f;
    9.     public float speedStrong = 4f;
    10.     public float bankSpeed = 90f;
    11.     public float bankSmooth = .5f;
    12.  
    13.     public Transform eagleTransform;
    14.     void Start()
    15.     {
    16.         rigidbody.freezeRotation = true;
    17.  
    18.         eagleTransform.animation.AddClip(eagleTransform.animation["Head Right"].clip, "HeadRight");
    19.         eagleTransform.animation["HeadRight"].AddMixingTransform(deepFind(eagleTransform,"neck"),true);
    20.         eagleTransform.animation.AddClip(eagleTransform.animation["Head Left"].clip, "HeadLeft");
    21.         eagleTransform.animation["HeadLeft"].AddMixingTransform(deepFind(eagleTransform,"neck"), true);
    22.         eagleTransform.animation.AddClip(eagleTransform.animation["Head Down"].clip, "HeadDown");
    23.         eagleTransform.animation["HeadDown"].AddMixingTransform(deepFind(eagleTransform,"neck"), true);
    24.         eagleTransform.animation.AddClip(eagleTransform.animation["Head Up"].clip, "HeadUp");
    25.         eagleTransform.animation["HeadUp"].AddMixingTransform(deepFind(eagleTransform,"neck"), true);
    26.  
    27.         eagleTransform.animation["Gliding"].layer = eagleTransform.animation["Flight Strong"].layer = eagleTransform.animation["Flight Regular"].layer = 1;
    28.         eagleTransform.animation.SyncLayer(1); // brovides nice smooth blending between the various flight powers
    29.  
    30.         // Set all eagleTransform.animations to loop
    31.         eagleTransform.animation.wrapMode = WrapMode.Loop;
    32.  
    33.         // Except our action eagleTransform.animations, Dont loop those
    34.         eagleTransform.animation["Bank Left"].wrapMode = WrapMode.Clamp;
    35.         eagleTransform.animation["Bank Right"].wrapMode = WrapMode.Clamp;
    36.         eagleTransform.animation["HeadLeft"].wrapMode = WrapMode.Clamp;
    37.         eagleTransform.animation["HeadRight"].wrapMode = WrapMode.Clamp;
    38.         eagleTransform.animation["HeadUp"].wrapMode = WrapMode.Clamp;
    39.         eagleTransform.animation["HeadDown"].wrapMode = WrapMode.Clamp;
    40.         eagleTransform.animation["Diving"].wrapMode = WrapMode.Clamp;
    41.         eagleTransform.animation["Grab"].wrapMode = WrapMode.Clamp;
    42.  
    43.         // this doesn't work
    44.  //       eagleTransform.animation["Flight Regular"].layer = -1;
    45.  
    46. //        eagleTransform.animation.Stop();
    47.     }
    48.  
    49.     static Transform deepFind (Transform t, string s)
    50.     {
    51.         Transform dt = t.Find (s);
    52.         if (dt != null)
    53.             return dt;
    54.         else
    55.         {
    56.             foreach (Transform child in t)
    57.             {
    58.                 dt = deepFind (child, s);
    59.                 if (dt != null) return dt;
    60.             }
    61.         }  
    62.         return null;
    63.     }
    64.  
    65.     IEnumerator SetRotationPhysics(bool b)
    66.     {
    67.         yield return new WaitForSeconds(1f);
    68.         rigidbody.freezeRotation = b;
    69.     }
    70.  
    71.     void OnCollision()
    72.     {
    73.         StartCoroutine(SetRotationPhysics(false));
    74.         StartCoroutine(SetRotationPhysics(true));
    75.     }
    76.  
    77.     private float velocity;
    78.  
    79.     void FixedUpdate()
    80.     {
    81.         Vector3 rotation = transform.localEulerAngles;
    82.         rotation.z = Mathf.SmoothDampAngle(rotation.z,-Input.GetAxis("Horizontal") * bankSpeed, ref velocity, bankSmooth );
    83.         transform.localEulerAngles = rotation;
    84.  
    85.         rigidbody.AddRelativeForce(0, 10, speedNormal, ForceMode.Acceleration); // adding lift force to counter gravity and forward force to counter drag
    86.  
    87.         if (Input.GetAxis("Vertical") > 0.1f)
    88.         {
    89.             rigidbody.AddRelativeForce(0, 5f, speedStrong - speedNormal, ForceMode.Acceleration);
    90.             eagleTransform.animation.CrossFade("Flight Strong");
    91.             Debug.Log("Strong");
    92.         }
    93.         else
    94.         {
    95.             if (Input.GetAxis("Vertical") < -0.1f)
    96.             {
    97.                 rigidbody.AddRelativeForce(0, -3f, speedGlide - speedNormal, ForceMode.Acceleration);
    98.                 eagleTransform.animation.CrossFade("Gliding");
    99.                 Debug.Log("Gliding");
    100.             }
    101.             else
    102.             {
    103.                 eagleTransform.animation.CrossFade("Flight Regular");
    104.                 Debug.Log("Regular");
    105.             }
    106.         }
    107.  
    108.         if (Input.GetButtonDown("Jump"))
    109.         {
    110.             eagleTransform.animation.CrossFade("Diving", 0.3f);
    111.         }
    112.  
    113.         if (Input.GetButtonDown("Fire1"))
    114.         {
    115.             // We are running so play it only on the upper body
    116.             if (eagleTransform.animation["Flight Regular"].weight > 0.5f)
    117.                 eagleTransform.animation.CrossFadeQueued("Grab", 0.3f, QueueMode.PlayNow);
    118.             // We are in idle so play it on the fully body
    119.             else
    120.                 eagleTransform.animation.CrossFadeQueued("Grab", 0.3f, QueueMode.PlayNow);
    121.         }
    122.     }
    123. }
    124.