Search Unity

Splitng animations in Unity modifies tangent on last key frame

Discussion in 'Scripting' started by SinisterMephisto, Jul 16, 2011.

  1. SinisterMephisto

    SinisterMephisto

    Joined:
    Feb 18, 2011
    Posts:
    179
    Have you ever imported an animation clip and attempt to split it in unity? Did you notice that all the animations that looped often looked wonky?
    The problem unfortunately is with unity and not you. Unity is modifying your tangents once you split the animation. I noticed it change the tangent on the z rotation of the last frame of every clip.

    Repro Steps
    Duplicate your animation, add it to your model.
    Select your model
    Open the animation editor window
    Check the transform of each joint especially the tangent of the last key using Unity's animation editor.

    Did you spot it? Yes thats right unity is modifying your tangents


    Mild case

    Worst case


    BTW the animations I imported had keys on every frame hence it was very easily spotted.

    You might want to check other keys in case the problem is much bigger
     
    Last edited: Jul 16, 2011
  2. brianjkd

    brianjkd

    Joined:
    Feb 3, 2009
    Posts:
    143
    Anyone know a solution to this? I have this problem as well with my animation clips. To solve the issue I subtracted the last frame from each clip and checked the box to tell unity to create a looping frame. This works but it would be nice to know why unity is messing with the tangents
     
  3. SinisterMephisto

    SinisterMephisto

    Joined:
    Feb 18, 2011
    Posts:
    179
    Well i clicked on the second to the last frame and set both tangent to linear. This might be a problem if you don't have every frame keyed or if the curve is not meant to be linear. I have over 1000 animations and i do not see myself deleting the last frame of each one or even fixing the tangents so I plan on writing an importer.
    You may have to do the same.
     
  4. SinisterMephisto

    SinisterMephisto

    Joined:
    Feb 18, 2011
    Posts:
    179
    This is turning out to be a nightmare. I hate the fact Unity often choose not to explain some of there more technical functions.
     
  5. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    What is the case number of the bug report with attached repro case?
     
  6. SinisterMephisto

    SinisterMephisto

    Joined:
    Feb 18, 2011
    Posts:
    179
    Case 412828
     
  7. GerP

    GerP

    Joined:
    Feb 9, 2012
    Posts:
    1
    Has anyone figured a way around this aside from deleting the last frame? It doesn't work for me at all. I'd like to know why Unity is doing this and has caused numerous issues with multiple projects I'm working on. What boggles my mind is is the first time I imported one particular file it worked perfectly as expected, but after importing the file into a new scene I am getting these tangent glitches no matter what I do. I've tried baking the keys in maya, I've tried turning off the animation compression, I've even reverted to an older fbx exporter for maya which worked temporarily. I'd just like to avoid having to go back and modify lots of animations which are not broken.
     
    Last edited: Feb 9, 2012
  8. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    In the reported case 412828 the problem is caused by precision errors because the source animation has keys at very high numbers in the 30000-40000 range. Splitting up the source animation into several smaller files (using the @ notation - see the docs) with fewer animations in each, and moving the keyframes so they're closer to 0 should be a workaround for the problem.

    If you have experienced the problem for a source animation that doesn't have very high frame numbers, please report a bug with a repro case that includes that source animation, because we have not seen that ourselves.
     
  9. Rodolfo-Rubens

    Rodolfo-Rubens

    Joined:
    Nov 17, 2012
    Posts:
    1,197
    sorry to necro but, I'm having this since forever, now I'm using 5.4b10 and this bug is still there, I'm cutting off the first and last frame of the animation clip on the unity animation editor (model import settings) as a workaround but then my animation gets shorter, in a walk animation the leg doesn't stretch all the way forward like it should.

    All my animations are made by hand, I keep all of them in a single .ma file, I think all my animations together are 144 frames long, even if I only had idle, walk and run (72 frames), the walk animation wont loop correct.
     
    Last edited: Mar 31, 2016
  10. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    Okay. In that case, like I wrote, please submit a bug with repro case. We cannot fix the bug if we cannot reproduce it, and as far as I'm aware, we haven't seen this ourselves so far.
     
  11. Rodolfo-Rubens

    Rodolfo-Rubens

    Joined:
    Nov 17, 2012
    Posts:
    1,197
    Hey @runevision , I just submitted, the case number is 784923.
    I included a reproducible example.
     
  12. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    Thanks! It should hit Animation Team when the bug report has been processed by QA. :)
     
  13. Rodolfo-Rubens

    Rodolfo-Rubens

    Joined:
    Nov 17, 2012
    Posts:
    1,197
    You guys were able to reproduce this? This bug came back in beta 5.5, in 5.4 in the animation's tab of the import settings's preview pane it was still playing wrongly but in play mode everything seems okay, and now in 5.5 everything is wrong, setting Anim. Compression to off seems to fix a bit but there is still an issue in the very first frame of the animations which makes the animation to not loop correctly.
    I started a thread in 5.5 beta forum, I was wondering if I could reuse the case:
    https://forum.unity3d.com/threads/weird-shaky-animations.439442/
     
    Last edited: Nov 4, 2016
  14. yanghai

    yanghai

    Unity Technologies

    Joined:
    Mar 30, 2015
    Posts:
    13
    Hi,
    After investigating this, I can confirm that this is not a bug, but rather a consequence of the following. The original animation is in Maya on a rig (IK effectors) driving the character’s joints using series of constraints (typical Maya character set-up). When this file is used in Unity, the original Maya scene is baked and exported automatically. You can reproduce this in Maya using Bake Animation. This means that curves are baked (resampled keys) on the joints, sampled at 24 fps in this case. If you play this animation “on frame” in any application, the visual result will be as expected. But if you play this same file in any application that does not play “on frame”, but rather plays as many samples per second as possible (like Unity), you will “some times” play values that are in-between frames and therefore see this interpolation issue.

    Now, in this specific case, since your animations (idle, walk, run) are being baked and are separated by a single frame, the interpolation between the last two keys of your cycle have an undesired interpolation. You can see this by using Bake Animation in Maya and looking at the curves on the joints (nothing to do with the exporter, other then it doing this for you). Creating your animations separately, or using a buffer of an extra identical frame/key before & after your cycle will avoid this issue. Bottom line, the interpolation between the last pose of one animation and the first pose of the next animation is also affecting the interpolation of the previous and next keys, which are part of your animation/cycle.

    The same issue does happen in Unity 5.4, even if it is not exactly the same/more subtile, because this depends on how fast the evaluation happens… and can vary on every play back. ie. to see this issue, there needs to be an eval precisely in the interpolation between the last, and next-to-last key frame, which does not necessarily happen on every playback loop.

    Problem: Bad interpolation is created when IK effector is baked in Maya.
    Solution: Add 1 additional frame and identical key of the first and last pose, at the beginning and end of each cycle. Keep the original duration, and the additional keys will fix the interpolation issue when baked.

    Hope this helps :)
     
    Rodolfo-Rubens and zombiegorilla like this.
  15. Rodolfo-Rubens

    Rodolfo-Rubens

    Joined:
    Nov 17, 2012
    Posts:
    1,197
    Hmm, this is bad news, now that I'm making animation clips duration multiple of 10. It will be hard to keep track of the anims even with a sheet. :/

    But I still can't understand why this is happening, I tried baking the animations in maya and I don't see this interpolation issue, also, what this toggle here does?

    It looks like it doesn't change anything! Isn't it supposed to not resample the curves left by maya if set to false?

    But I still find this too weird, for example I have this creature model, the first animation clips were made with a duration of 24 frames, ok, the first clip goes from 1 to 24 (It's the first animation, a TPose and there is no animation at all in it) and the next clip starts at 25, In this clip, the frame 1 looks ok, the frame 23 and 24 also looks ok, but there is a "bump" in it at around 23.5! Why does that happen, if the frame that is totally different starts at 25 why do I get this interpolation in the middle of the frame 23 instead of in the frame 24 onwards? Of course I understand that having one extra frame between clips fix this but having a frame "space" (in this case, the space between 24-25) shouldn't also fix it? If I set the frame to be from 1 to 24 it should end in the frame 24 (beginning of it) and not get any curve after it, and this is is working in Unity, but the problem is that the interpolation error happens before 24, like there was a key frame at around 23.5, and the values there has nothing to do with the frame 25. Just for testing a setup a clip going from 23-24 and in the middle of it you have the exact pose you have at the frame 25, again 23 ok, 24 ok, but 23.5 it's wrong!

    I don't know exactly how to explain exactly so I uploaded a video:


    Here is a summary:
     
  16. Rodolfo-Rubens

    Rodolfo-Rubens

    Joined:
    Nov 17, 2012
    Posts:
    1,197
    Ah, another thing I discovered, unity allows you to see the baked animation in the animation window as curves inside Unity (if you don't optimize the gameobject rig):

    I created an animation clip going from the infamous frame 23 to 25. As I can see, even though frame 23 and 24 (0 and 1 in the animation window) has the same values, Unity insists in modifying the tangents between them, I don't think this is an issue with maya baking the frames.

    edit: btw, thank you for taking your time for checking this issue.
     
    Last edited: Nov 8, 2016
  17. yanghai

    yanghai

    Unity Technologies

    Joined:
    Mar 30, 2015
    Posts:
    13
    I understand what you are saying, and this is exactly what I tried to explain above. Let me try and explain this with some additional details.

    The Maya fbx exporter bakes (Maya Bake Animation function), then exports the baked result without any Maya constraints on the IK rig, when a Maya file is added to a Unity Project. This is done in a hidden Maya process in order to not change your current scene in Maya. This is also why you do not see the baked result in Maya after the hidden export process. Even if you manually bake (select rig hierarchy and use Bake Animation), there are a couple reasons why you will not see the issue in Maya. First, the rig constraints are still active and even if the animation (curves) have been baked to the joints, the IK rig (constraints) are still driving the animation, not the joints. Second, Maya plays “on frame”, and the problem is in the interpolation (in-between two keys). If you export your original Maya scene with the Bake Animation option (in this case fbx export option, same function), and open the resulting file in any other application that can play both “on frame” or “free” (not on frame. play as many frames as possible per second, like Unity or any game engine, or MotionBuilder for example), you will see the problem and that it disappears when playing “on frame”. This is what Maya does, plays “on frame”, hiding any interpolation issue between two frames/keys during playback. Now, last but not least, Maya’s default key type is “Auto tangents” (exactly like Unity’s new default “Clamped Auto”), BUT (reason why disabling “Resample Curves” is not doing anything in this case) because there are pre/post rotations (joint orients) on the joints in Maya and this is not supported in Unity.

    Importing without any pre/post rotations would avoid the forced resampling and give a 1:1 result, but this is often not possible since those joint orients are needed in most cases. To import these un-supported joint orients in Unity, curves need to be pre-processed/resampled based on sample rate. The result of this resampling/bake is that the curves have the correct value on frame, but the tangents are baked (Free), not Clamped Auto.
    Here’s a simple visual example using a constant key, resampled vs. un-resampled, same issue…
    Resampled.png
    … vs. this when resample is not required…
    unResampled.png

    Said like this, it sounds like we could simply apply the original Auto Clamped tangent, but this is not possible since at this point in the FBX SDK process, we actually do not know what the original tangent was (auto, constant, linear, fixed), and it could have actually been anything, so making them Free based on Auto is the safest path for all cases.

    Now, as for the one frame gap between your animations, let me clarify what is happening exactly and why an extra frame will resolve this. In Maya, when animation on an IK controller is baked to a joint, it creates keys on every frame (based on sample rate, in this case 24fps). Because the joint orients are forcing a resample on import, you are ending up with the issue illustrated above. Resulting in something like this, in-between your end pose of one animation and the start of the next…

    Current result:
    Cures1.png

    Adding a frame as a buffer for the interpolation issue would look like this:
    Cures2.png

    There is no magical solution to this since Unity does not presently support pre/post rotations (source of this issue). Hoping these explanations help you better understand what’s currently supported and the limitations, as well as get you going in a way that allows you to avoid this issue.
     
  18. Rodolfo-Rubens

    Rodolfo-Rubens

    Joined:
    Nov 17, 2012
    Posts:
    1,197
    That was an amazing explanation! Thanks a lot for taking your time for setting this up!
    I will use this workaround for my animation clips for sure.

    And that's all, case 784923 can be closed!!
     
    budazen likes this.