Rotation by AnimationCurve script?

Hello. I need create animation curve with rotation by script.
What key i need add for correctly rotation?

clip.SetCurve(go.name, typeof (Transform), “localRotation.z”,
new AnimationCurve(new Keyframe(0, 1), new Keyframe(20, 180)));

i’ll try setCurve for all 4 coor. I try use Quanternion, but nothing work. May be i do somthing wrong… can some one help me?

i need 360 and more angle

You probably want to be animating the transform’s eulerAngles, not modifying the xyzw of the rotation property itself (transform.rotation is a Quaternion and modifying their properties by hand is where madness lies)

i can change euler angle by setCurve, but i can’t do angle more 360… =(

Unfortunately, you can’t easily drive Quaternions by curves either (they’re… complicated; the xyzw components don’t actually describe axis rotations, they indirectly describe an angle around an axis via a bunch of trig functions; you can’t really edit the components specifically and expect it to work).

If you precalculated the Quaternions, you might be able to do it… something like this?

Quaternion rot; //Quaternion we'll be storing the rotation in
float angle = 180; //rotation each keyframe
int time = 20; //time between keyframes
Vector3 axis = Vector3.forward; //define the axis of rotation
int keyframes = 6; //how many keys to add

//the four curves; one for each quaternion property
AnimationCurve xCurve = new AnimationCurve(), yCurve = new AnimationCurve(), zCurve = new AnimationCurve(), wCurve = new AnimationCurve();

for(int k = 0; k < keyframes; k++){
   rot = Quaternion.AngleAxis(angle*k, axis); //create our quaternion key for this keyframe
  
   //create the keys
   xCurve.AddKey(time*k, rot.x);
   yCurve.AddKey(time*k, rot.y);
   zCurve.AddKey(time*k, rot.z);
   wCurve.AddKey(time*k, rot.w);
}

//set the curves on the clip  
clip.SetCurve(go.name, typeof (Transform), "localRotation.x", xCurve);
clip.SetCurve(go.name, typeof (Transform), "localRotation.y", yCurve);
clip.SetCurve(go.name, typeof (Transform), "localRotation.z", zCurve);
clip.SetCurve(go.name, typeof (Transform), "localRotation.w", wCurve);
1 Like