- I need to set Animation Curve Interpolation via Scripting
You are trying to set animation curves by script and when interpolation is done you get the following result:
To solve this issue do the following two things:
- Call AnimationClip.EnsureQuaternionContinuity after you have manually set via script the Animation Curve properties.
- 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 ();
This article applies to Unity versions 5.x
thanks ,it helped alot
Thanks everyone for help, it's really good and useful.
SetCurve() will work in the Editor, but it still doesn't work in a Build. Tested with Unity version 2020.3 (LTS). This has been an open issue since 1996.
Please sign in to leave a comment.