Search Unity

Key frames

Discussion in 'Scripting' started by Kre8or, Aug 20, 2014.

  1. Kre8or

    Kre8or

    Joined:
    Mar 15, 2014
    Posts:
    31
    The docs for scripting key frames are vague. I need to understand how to set keys for curve properties. You would need to point the scripting to a property in a curve(like Material._Color.r) right?How do you pass them to a specific curve or animation file? (not sure how it works and it doesn't say.)

    Example: I want to set a key for the Material._Color.r but the docs don't show any property names being passed to, that I could find. Can this be done in the script?

    I'm trying to effect the time of the key and the value of the property if it matters. Thanks
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    material.Color.r is not a curve property, just a float value.
    You can sample a curve { someCurve.Evaluate(t) } to get a float value, then pass that into some other float property, but can not directly assign an animtionCurve to another property.

    Set new keys with addKey
    http://docs.unity3d.com/ScriptReference/AnimationCurve.AddKey.html
    or just Get, modify and Set them (ie you cannot modify them in-place with someCurve.keys[0] = xxx )
     
  3. Kre8or

    Kre8or

    Joined:
    Mar 15, 2014
    Posts:
    31
    Thx for the reply hpjohn. Are you saying you cannot change the float value for Material._Color.r? I wanted to script a keyframe for it with one value and then another later, with a dif value. Not possible? I see some things are read only so wasn't sure.
     
    Last edited: Aug 27, 2014
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    You can change the color, sure.
    But you have to do it the long way
    Something like
    Code (CSharp):
    1. AnimationCurve curve
    2. float val = curve.Evaluate(time)
    3. Color col = material.color
    4. col.r = val
    5. material.color = col
     
  5. Kre8or

    Kre8or

    Joined:
    Mar 15, 2014
    Posts:
    31
    Does curve.Evaluate(time) change float val incrementally and = to the amount of time that goes by? If so I think this is what I'm looking for. Rather than bother with the key frame scripting this may work for what I'm trying to do.

    I would set the value for the color.r and the rest to 0 to make the thing black (a quad used for a fade in out,) and change the color.a to = val and use if statements to control it. To reverse the fade for a fade in I would have to make a var that is subtracting time instead somehow.

    Does this make sense? Do you think it's the way to go about it?
     
  6. Kre8or

    Kre8or

    Joined:
    Mar 15, 2014
    Posts:
    31
  7. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    If all you want is to fade something in or out, you don't need animationCurve at all. (unless you specifically want the fade to occur at a non-linear rate)
    Better to make a coroutine