AnimationCurve doesn't work with rotations

I am working with creating animating objects in Unity through the use of AnimationClip and SetCurve. Due to the nature of the program I must use this method. When I use this method on transformations it works perfectly. When I attempt to rotate the object, I get nothing. No movement whatsoever and the Animation Editor shows the keys are there but are completely flat. I have been researching this on the forums, but come up with nothing that works. Here is the code I have been using. Any help is appreciated.

function Start () {
    // Create the curve
    var curve = new AnimationCurve(Keyframe(0, 0.0f), Keyframe(1, 280.0f));
    // Create the clip with the curve
    var clip : AnimationClip = new AnimationClip();
    clip.SetCurve("", Transform, "localRotation.x", curve);
    clip.SetCurve("", Transform, "localRotation.y", curve);
    clip.SetCurve("", Transform, "localRotation.z", curve);
    clip.SetCurve("", Transform, "localRotation.w", curve);
    // Add and play the clip
    animation.AddClip(clip, "test");
    animation.Play("test");
}
@script RequireComponent(Animation)

No ideas, anyone?

I can’t say why this causes the curve to be flat, but you do seem to have a wrong idea about the meaning of the x,y,z and w values. Rotations are stored as quaternions whereas the number 280 indicates to me that you expect Euler angles. Try making a quaternion for your desired angle and then create four separate curves for the x,y,z and w values based on that quaternion.

same here after try set one curve for xyz and w
another idea please

Now is 2017 so… maybe this will help someone else:

Animation anim = GetComponent<Animation>();
AnimationClip rotateClip = new AnimationClip();
rotateClip.legacy = true;
rotateClip.wrapMode = WrapMode.Loop;

Keyframe[] key;
key = new Keyframe[2];
key[0] = new Keyframe(0.0f, 0.0f);
key[1] = new Keyframe(10.0f, 360.0f);
AnimationCurve animCurve = new AnimationCurve(key);
rotateClip.SetCurve("", typeof(Transform), "localEulerAngles.y", animCurve);

anim.AddClip(rotateClip, "rotate");
anim["rotate"].layer = 1;
anim.Play("rotate");
1 Like