Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Keyframe (AnimationCurve) won't update!

Discussion in 'Scripting' started by Juice-Tin, Apr 16, 2015.

  1. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    229
    Hello, I have the following 2 values:
    Code (CSharp):
    1. Vector3[] pos;
    2. AnimationCurve curveY;
    In the Start method I set 10 keys in the animation curve

    Then in a for loop, I am trying to update update those keys with the new position
    Code (CSharp):
    1. for(int i=0; i<complexity; i++){
    2.     curveY.keys[i].value = pos[i].y;
    3.     Debug.Log(curveY.keys[i].value +", "+ pos[i].y);
    4. }
    However, the debug shows the pos is constantly changing, but the curveY never changes! It always stays at the exact same value that was set in at the beginning.

    I even tried this way but it doesn't work either!
    Code (CSharp):
    1. for(int i=0; i<complexity; i++){
    2.     curveY.keys[i] = new Keyframe(curveY.keys[i].time, pos[i].y);
    3.     Debug.Log(curveY.keys[i].value +", "+ pos[i].y);
    4. }
    Any ideas? :(
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    http://docs.unity3d.com/ScriptReference/AnimationCurve-keys.html
    Note that the array is "by value", i.e. getting keys returns a copy of all keys and setting keys copies them into the curve.

    You can't just modify curve.keys[0] because that is not the key in the curve
    get the whole array (copies by value), modify that, then set the whole array
     
  3. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    229
    Problem is there doesn't seem to be a way to set the array aside from creating a new animation curve.

    However I found a solution that works, which involves having my own array of keys, modifying that, and then using the curve's built in function to adjust an existing key.

    Code (CSharp):
    1. keysY[i].value = pos[i].y;
    2. curveY.MoveKey(i, keysY[i]);
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    except for curve.keys = myKeys ...?