Follow

How can I set Animation Curve Interpolation via Scripting?

Symptoms

  • I need to set Animation Curve Interpolation via Scripting

Cause

You are trying to set animation curves by script and when interpolation is done you get the following result: 

Resolution

To solve this issue do the following two things:

  1. Call AnimationClip.EnsureQuaternionContinuity after you have manually set via script the Animation Curve properties.
  2. Save the assets with AssetDatabase.SaveAssets

Please see the example code below for guidance: 

 AnimationClip clip = new AnimationClip ();
 //If using Unity 5
 clip.legacy = true;
 
         
 // Rotation
 AnimationCurve CurveRot = new AnimationCurve ();
 AnimationCurve CurveRotY = new AnimationCurve ();
 AnimationCurve CurveRotZ = new AnimationCurve ();
 AnimationCurve CurveRotW = new AnimationCurve ();
         
 
 float time = 0.5f;
 //stepValues [0,1,0,10]
 Quaternion angle = Quaternion.Euler (0, 0, stepValues[i] * 180.0f);
 CurveRot.AddKey (time, angle.x);
 CurveRotY.AddKey (time, angle.y);
 CurveRotZ.AddKey (time, angle.z);
 CurveRotW.AddKey (time, angle.w);    
 
         
 clip.SetCurve ("", typeof(Transform),"localRotation.z", CurveRot);
 clip.SetCurve ("",typeof(Transform),"localRotation.y", CurveRotY);
 clip.SetCurve ("",typeof(Transform),"localRotation.z", CurveRotZ);
 clip.SetCurve ("",typeof(Transform),"localRotation.w", CurveRotW);
 
 //This ensures a smooth interpolation
 clip.EnsureQuaternionContinuity ();
         
 String newCName = "Comp1_L_1";
 AssetDatabase.CreateAsset (clip,ANIM_CLIP_PATH+newCName+".anim");
 AssetDatabase.SaveAssets ();

More Information

http://docs.unity3d.com/Manual/animeditor-AnimationCurves.html 

This article applies to Unity versions 5.x

 

Was this article helpful?
1 out of 1 found this helpful
Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.