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);

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!!! :slight_smile: