问题
- 我想在脚本中设置动画曲线插值
原因
直接通过脚本设置动画曲线插值,会得到如下的结果
不正确插值 正确(修复后)插值
解决方案
要解决这种情况,需要按如下两步操作:
1. 在脚本中设置动画曲线参数后, 调用AnimationClip.EnsureQuaternionContinuity
2. 用AssetDatabase.SaveAssets保存处理后的资源。
请参考下面的示例代码:
AnimationClip clip = new AnimationClip ();
//在使用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);
//此处可以确保插值结果平滑
clip.EnsureQuaternionContinuity ();
String newCName = "Comp1_L_1";
AssetDatabase.CreateAsset (clip,ANIM_CLIP_PATH+newCName+".anim");
AssetDatabase.SaveAssets ();
更多相关信息
http://docs.unity3d.com/Manual/animeditor-AnimationCurves.html
适用版本: Unity5