- Home /
The question is answered, right answer was accepted
Weird Animation clip curves tangents
When i create an animation curve with custom keyframes in C#, the result curve has strange tangents. Here's an image which rapresents the problem:
The final result show be:
I want to know how can i solve this problem... Thanks for your replies...
EDIT:
This is a piece of code:
currKeyframe = -frameStep; nextKeyframe();
Keyframe[] keysPosX = new Keyframe[totalFrames], keysPosY = new Keyframe[totalFrames], keysPosZ = new Keyframe[totalFrames];
Keyframe[] keysRotX = new Keyframe[totalFrames], keysRotY = new Keyframe[totalFrames], keysRotZ = new Keyframe[totalFrames], keysRotW = new Keyframe[totalFrames];
float frameRate = animation.clip.frameRate;
for(int k = 0; k < totalFrames; k++)
{
keysPosX[k] = new Keyframe(frameStep * k / frameRate, currTransform.localPosition.x);
keysPosY[k] = new Keyframe(frameStep * k / frameRate, currTransform.localPosition.y);
keysPosZ[k] = new Keyframe(frameStep * k / frameRate, currTransform.localPosition.z);
keysRotX[k] = new Keyframe(frameStep * k / frameRate, currTransform.localRotation.x);
keysRotY[k] = new Keyframe(frameStep * k / frameRate, currTransform.localRotation.y);
keysRotZ[k] = new Keyframe(frameStep * k / frameRate, currTransform.localRotation.z);
keysRotW[k] = new Keyframe(frameStep * k / frameRate, currTransform.localRotation.w);
nextKeyframe();
}
AnimationCurve cPosX = new AnimationCurve(keysPosX), cPosY = new AnimationCurve(keysPosY), cPosZ = new AnimationCurve(keysPosZ), cRotX = new AnimationCurve(keysRotX), cRotY = new AnimationCurve(keysRotY), cRotZ = new AnimationCurve(keysRotZ), cRotW = new AnimationCurve(keysRotW);
animation.clip.SetCurve(AnimationUtility.CalculateTransformPath(currTransform, transform), typeof(Transform), "localPosition.x", cPosX);
animation.clip.SetCurve(AnimationUtility.CalculateTransformPath(currTransform, transform), typeof(Transform), "localPosition.y", cPosY);
animation.clip.SetCurve(AnimationUtility.CalculateTransformPath(currTransform, transform), typeof(Transform), "localPosition.z", cPosZ);
animation.clip.SetCurve(AnimationUtility.CalculateTransformPath(currTransform, transform), typeof(Transform), "localRotation.x", cRotX);
animation.clip.SetCurve(AnimationUtility.CalculateTransformPath(currTransform, transform), typeof(Transform), "localRotation.y", cRotY);
animation.clip.SetCurve(AnimationUtility.CalculateTransformPath(currTransform, transform), typeof(Transform), "localRotation.z", cRotZ);
animation.clip.SetCurve(AnimationUtility.CalculateTransformPath(currTransform, transform), typeof(Transform), "localRotation.w", cRotW);
Can you post your C# code? There isn't much to go on here.
For the problem on the left, can the curve actually change instantly like you want it to, without extra control points there?
For the problem on the right, I would try printing out the values you are passing in and looking in particular at those two points to the left and right of the glitch. Is there something strange about them?
I think that animation is written as well without any problem... i can say this because when i try to re-set manually a keyframe of weird animation with same value, it reupdates the animation with correct tangents... this happens only with the rotation curve (the position curve works well) and would be an interpolation problem or a unity bug... thanks for your patience
Answer by qwee · Apr 25, 2014 at 03:08 PM
Finally i have found the solution i had to implement the function EnsureQuaternionContinuity in the code (at the end):
animation.clip.EnsureQuaternionContinuity();
And then save assets:
AssetDatabase.SaveAssets();
This worked for me, i hope it can helps someone!!! :)