Create animation by scripting

Hi all!

I saw this post :
"

  • using UnityEngine;
  • using UnityEditor;
  • using System.Collections;
  • public class TestAnim : Editor
  • {
  • [UnityEditor.MenuItem(“TestAnim/Create Test”)]
  • public static void CreateAnim ()
  • {
  • AnimationClip clip = new AnimationClip();
  • clip.SetCurve(“”, typeof(Transform), “position.x”, AnimationCurve.EaseInOut(0, 0, 2, 10));
  • clip.SetCurve(“”, typeof(Transform), “position.y”, AnimationCurve.EaseInOut(0, 10, 2, 0));
  • clip.SetCurve(“”, typeof(Transform), “position.z”, AnimationCurve.EaseInOut(0, 5, 2, 2));
  • AssetDatabase.CreateAsset(clip, “Assets/Test.anim”);
  • }
  • }

"
and I was wondering, how to use this? How to create an element and add this script to the element so The animation start. Is it possible to add a delay before launching the element? And is it possible to get the basic transformation of the GameObject this script is attached?

What I want to achieve is to create severals objects on a scene, and make them all have the same animation but with 1 second between each.
(For example, all object start in the sky, and they fall down 1 by 1 in front of the astonished eyes of the camera).

I am quite new with Unity, and i’m probably not searching at the good places, but apart this post, I only found posts about how just start animation that are already created …

Sorry if this post already exists,
If someone can help me, I would be really happy!

Thanks all, and have a nice evening!

Well,
after more and more researches I found out that
using UnityEngine;
using System.Collections;
public class Animation1 : MonoBehaviour {
public float time;
AnimationClip anim;
// Use this for initialization
void Start () {
StartCoroutine(DelayedAnimation());
anim = animation.clip;
anim.ClearCurves ();
Debug.Log (transform.position.x);
Debug.Log (transform.position.x + 1.0);
AnimationCurve curve = AnimationCurve.Linear(0.0F, transform.position.x, 2.0F, transform.position.x+1);
anim.SetCurve(“”, typeof(Transform), “localPosition.x”, curve);
}
IEnumerator DelayedAnimation(){
yield return new WaitForSeconds(time);
animation.Play (anim.name);
}
}
does the trick if you put an animation on your object.