I usually use this method to animate a game object from one point to another, may be useful to you.
public void MoveGO (GameObject TempGO, Vector3 StartPos, Vector3 EndPos) { float clipLength = 1f; AnimationCurve curve1 = null, curve2 = null, curve3 = null; AnimationClip clip = null; curve1 = AnimationCurve.Linear (0, StartPos.x, clipLength, EndPos.x); curve2 = AnimationCurve.Linear (0, StartPos.y, clipLength, EndPos.y); curve3 = AnimationCurve.Linear (0, StartPos.z, clipLength, EndPos.z); clip = new AnimationClip (); clip.SetCurve ("", typeof(Transform), "localPosition.x", curve1); clip.SetCurve ("", typeof(Transform), "localPosition.y", curve2); clip.SetCurve ("", typeof(Transform), "localPosition.z", curve3); if (TempGO.GetComponent ("Animation") == null) { TempGO.AddComponent ("Animation"); } if (TempGO.animation.IsPlaying ("AnimationDemo")) { //TempGO.animation["AnimationDemo"].time = 0.5f ; TempGO.animation.Sample (); TempGO.animation.RemoveClip ("AnimationDemo"); } TempGO.animation.AddClip (clip, "AnimationDemo"); TempGO.animation ["AnimationDemo"].speed = 1f; TempGO.animation.Play ("AnimationDemo"); //TempGO.animation.wrapMode=WrapMode.PingPong; }
source share