r/Unity3D•
Script256

Using an interactive graph in the editor.

Question

Hey there! On I script I'm writing I have a variable that depends on another one's value. The dependency can't be described with a mathematical function (afaik) due to its complexity, and I want to make it as easy to edit as possible. After some googling and reading the documentation I found the class AnimationCurve, but I just can't get it to work because I don't really understood how it is supposed to be used. I'm fairly new to C#, the last time I used unity was like three years ago and I only knew UnityScript.

Posted Oct 22, 2019, 7:00 PM UTC

17
The_Humble_Frank•

public AnimationCurve curve;

void GetYValueFromTheCurve(float x){

float y =  curve.Evaluate(x);

return y;

}

2

You can use the Evaluate function on the curve to get a value on the Y axis at a given X axis value. I usually make my curves go from 0 to 1 on the X axis for simplicity but you can make the curve as long as you want.

1
Script256OP•

Sorry I didn't make the post clear, I don't know how to make a curve, and is it possible to edit in the inspector?

1

Oh, sorry! AnimationCurve objects can be serialized in the inspector like other variables. Add:

public AnimationCurve curve;

And then you can add keys to the curve in the inspector to get the shape you want. Then:

float curveValue = curve.Evaluate(someInput);

To get a result out.

1
Script256OP•

Thanks for the answer!

you can add keys to the curve in the inspector to get the shape you want.

This is my main problem. The animation curve shows in the inspector but I don't see any input that allows me to create or modify the curve

1

Clicking on the animation curve box in the inspector will open the animation curve editor window. From there I usually click one of the presets at the bottom of the window, you'll see a bunch of little graph icons. Then you can drag around or add/delete keys.

1

Try one of the presets down below first. Just click on one. Also this should help:

https://docs.unity3d.com/Manual/TimelineAddingKeys.html

1