Animation Script

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

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

The sync did the job! thank you. Good job guys.

htwarrior

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 ?

using UnityEngine;
using System.Collections;

public class EagleControl : MonoBehaviour
{
    public float speedGlide = 1f;
    public float speedNormal = 2f;
    public float speedStrong = 4f;
    public float bankSpeed = 90f;
    public float bankSmooth = .5f;

    public Transform eagleTransform;
    void Start()
    {
        rigidbody.freezeRotation = true;

        eagleTransform.animation.AddClip(eagleTransform.animation["Head Right"].clip, "HeadRight");
        eagleTransform.animation["HeadRight"].AddMixingTransform(deepFind(eagleTransform,"neck"),true);
        eagleTransform.animation.AddClip(eagleTransform.animation["Head Left"].clip, "HeadLeft");
        eagleTransform.animation["HeadLeft"].AddMixingTransform(deepFind(eagleTransform,"neck"), true);
        eagleTransform.animation.AddClip(eagleTransform.animation["Head Down"].clip, "HeadDown");
        eagleTransform.animation["HeadDown"].AddMixingTransform(deepFind(eagleTransform,"neck"), true);
        eagleTransform.animation.AddClip(eagleTransform.animation["Head Up"].clip, "HeadUp");
        eagleTransform.animation["HeadUp"].AddMixingTransform(deepFind(eagleTransform,"neck"), true);

        eagleTransform.animation["Gliding"].layer = eagleTransform.animation["Flight Strong"].layer = eagleTransform.animation["Flight Regular"].layer = 1;
        eagleTransform.animation.SyncLayer(1); // brovides nice smooth blending between the various flight powers

        // Set all eagleTransform.animations to loop
        eagleTransform.animation.wrapMode = WrapMode.Loop;

        // Except our action eagleTransform.animations, Dont loop those
        eagleTransform.animation["Bank Left"].wrapMode = WrapMode.Clamp;
        eagleTransform.animation["Bank Right"].wrapMode = WrapMode.Clamp;
        eagleTransform.animation["HeadLeft"].wrapMode = WrapMode.Clamp;
        eagleTransform.animation["HeadRight"].wrapMode = WrapMode.Clamp;
        eagleTransform.animation["HeadUp"].wrapMode = WrapMode.Clamp;
        eagleTransform.animation["HeadDown"].wrapMode = WrapMode.Clamp;
        eagleTransform.animation["Diving"].wrapMode = WrapMode.Clamp;
        eagleTransform.animation["Grab"].wrapMode = WrapMode.Clamp;

        // this doesn't work
 //       eagleTransform.animation["Flight Regular"].layer = -1;

//        eagleTransform.animation.Stop();
    }

    static Transform deepFind (Transform t, string s)
    {
	    Transform dt = t.Find (s);
	    if (dt != null)	
		    return dt;
	    else
	    {
		    foreach (Transform child in t) 
            {
			    dt = deepFind (child, s);
			    if (dt != null)	return dt;
		    }
	    }	
	    return null;
    }

    IEnumerator SetRotationPhysics(bool b)
    {
        yield return new WaitForSeconds(1f);
        rigidbody.freezeRotation = b;
    }

    void OnCollision()
    {
        StartCoroutine(SetRotationPhysics(false));
        StartCoroutine(SetRotationPhysics(true));
    }

    private float velocity;
 
    void FixedUpdate()
    {
        Vector3 rotation = transform.localEulerAngles;
        rotation.z = Mathf.SmoothDampAngle(rotation.z,-Input.GetAxis("Horizontal") * bankSpeed, ref velocity, bankSmooth );
        transform.localEulerAngles = rotation;

        rigidbody.AddRelativeForce(0, 10, speedNormal, ForceMode.Acceleration); // adding lift force to counter gravity and forward force to counter drag

        if (Input.GetAxis("Vertical") > 0.1f)
        {
            rigidbody.AddRelativeForce(0, 5f, speedStrong - speedNormal, ForceMode.Acceleration);
            eagleTransform.animation.CrossFade("Flight Strong");
            Debug.Log("Strong");
        }
        else
        {
            if (Input.GetAxis("Vertical") < -0.1f)
            {
                rigidbody.AddRelativeForce(0, -3f, speedGlide - speedNormal, ForceMode.Acceleration);
                eagleTransform.animation.CrossFade("Gliding");
                Debug.Log("Gliding");
            }
            else
            {
                eagleTransform.animation.CrossFade("Flight Regular");
                Debug.Log("Regular");
            }
        }

        if (Input.GetButtonDown("Jump"))
        {
            eagleTransform.animation.CrossFade("Diving", 0.3f);
        }

        if (Input.GetButtonDown("Fire1"))
        {
            // We are running so play it only on the upper body
            if (eagleTransform.animation["Flight Regular"].weight > 0.5f)
                eagleTransform.animation.CrossFadeQueued("Grab", 0.3f, QueueMode.PlayNow);
            // We are in idle so play it on the fully body
            else
                eagleTransform.animation.CrossFadeQueued("Grab", 0.3f, QueueMode.PlayNow);
        }
    }
}