Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question Wrong curve when made with a script

Discussion in 'Animation' started by Emilien2005, Feb 15, 2024.

  1. Emilien2005

    Emilien2005

    Joined:
    May 12, 2021
    Posts:
    1
    i have a curve that is made by a script, and put in a animation, im not sure why, but if i dont manually set the tangent to constant (even tho the script does it) it becomes all weird and dosent appear as its supposed to.

    Am i missing something or is this a bug? im on 2022.3.8f1

    this is a project for VrChat, i know its bad but it because i cant use a script on a avatar, this is the best solution i found

    Code (CSharp):
    1.     void CreateAnimation()
    2.     {
    3.         AnimationClip clip = new();
    4.         clip.wrapMode = WrapMode.Once;
    5.  
    6.         for (int i = 0; i < AudioClipList.Count; i++) // do all the curves
    7.         {
    8.             AnimationCurve curve = new();
    9.             if (i == 0)
    10.             curve.AddKey(0, 1);
    11.             else
    12.             curve.AddKey(0, 0);
    13.             AnimationUtility.SetKeyBroken(curve, 0, false);
    14.             AnimationUtility.SetKeyLeftTangentMode(curve, 0, AnimationUtility.TangentMode.Constant);
    15.             AnimationUtility.SetKeyRightTangentMode(curve, 0, AnimationUtility.TangentMode.Constant);
    16.  
    17.             for (int j = 1; j < AudioClipList.Count + 2; j++) // do all the keyframes on each curves
    18.             {
    19.                 if (i == j)
    20.                     curve.AddKey(j, 1);
    21.                 else
    22.                     curve.AddKey(j, 0);
    23.  
    24.                 AnimationUtility.SetKeyBroken(curve, j , false);
    25.                 AnimationUtility.SetKeyLeftTangentMode(curve, j , AnimationUtility.TangentMode.Constant);
    26.                 AnimationUtility.SetKeyRightTangentMode(curve, j , AnimationUtility.TangentMode.Constant);
    27.             }
    28.  
    29.             curve.AddKey(AudioClipList.Count, 1);
    30.             AnimationUtility.SetKeyBroken(curve, AudioClipList.Count, false);
    31.             AnimationUtility.SetKeyLeftTangentMode(curve, AudioClipList.Count, AnimationUtility.TangentMode.Constant);
    32.             AnimationUtility.SetKeyRightTangentMode(curve, AudioClipList.Count, AnimationUtility.TangentMode.Constant);
    33.  
    34.             clip.SetCurve(FindHierarchyPath(gameObject, audioSources[i].transform.gameObject), typeof(GameObject), "m_IsActive", curve);
    35.         }
    36.  
    37.         AssetDatabase.CreateAsset(clip, "Assets/AudioAnimation.anim");
    38.     }
    Some explanary images

    https://imgur.com/a/ZOsCiKo