Search Unity

Linear curves from script at runtime?

Discussion in 'Animation' started by Zee-Play, Feb 22, 2020.

  1. Zee-Play

    Zee-Play

    Joined:
    Jun 13, 2015
    Posts:
    16
    Hi,
    We're loading model data, including animation from a custom format, and this implies creating Keyframes and Curves and Clips at runtime as the data is streamed on demand.

    The issue I'm having is, I have not found a single working solution yet for setting specific keyframe tangents to linear.
    Setting in and out tangent values doesn't change the fact that it remains a curve, we need to be able to change the tangent type
    Is there any working solution for this?
    upload_2020-2-22_3-40-59.png
    This doesn't seem to work : https://forum.unity.com/threads/how-to-set-an-animation-curve-to-linear-through-scripting.151683/

    Thanks.

    Edit: Currently using a "hack", by duplicating every key one time forward, but this is not a perfect solution, while it does make the curves look more linear, it's a performance loss.
     
    Last edited: Feb 22, 2020
  2. Varaughe

    Varaughe

    Joined:
    Apr 22, 2013
    Posts:
    40
  3. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
    I was able to make it tangent:
    Code (CSharp):
    1.  
    2.        AnimationCurve myCurve = new AnimationCurve(myKeyframes.ToArray());
    3.  
    4.         // Set here the keyframe tangents to Auto.
    5.         for (int i = 0; i < myCurve.length; i++)
    6.         {
    7.             myCurve.SmoothTangents(i, 0);
    8.         }
     
  4. afshin_a_1

    afshin_a_1

    Joined:
    Apr 26, 2015
    Posts:
    51
    what is myKeyframes?