AnimationCurves in ScriptableObjects?

Hi, folks!
I’m messing around with different jump arcs for my 2D platformer, and I’m thinking of storing different jump arcs as ScriptableObjects. The format would look like this:

//Stores two curves: one representing the x value along a normalized curve, and the other representing the y value.
//Get(float) returns a Vector2 consisting of the output of two curves
[CreateAssetMenu(menuName = "Curves/2D Curve")]
public class Curve2D : ScriptableObject
{
    public AnimationCurve xcurve;
    public AnimationCurve ycurve;

    public Vector2 Get(float t)
    {
        if (t < 0) { return new Vector2(xcurve.Evaluate(0),ycurve.Evaluate(0)); }
        if (t > 1) { return new Vector2(xcurve.Evaluate(1), ycurve.Evaluate(1)); }
        return new Vector2(xcurve.Evaluate(t), ycurve.Evaluate(t));
    }
}

The idea would be to use these Scriptable objects as a lookup table of sorts. I could say something like, “What force should be applied at time _____?” and then, I guess Unity would interpolate along the curves to return a corresponding Vector2.

Is this a silly approach? If so, could someone direct me to a better alternative?

Also, is this the right place to post this kind of question? Unsure of which part of the forum to use.

Thanks, everyone!

2 Likes

This is completely reasonable and a great way to organize this type of information in Unity!

Welcome to the forums.

1 Like