I’m implementing some car mechanics and I need to add a lookup table for the torque curve of the engine (it’s a non linear mapping based on samples).
Currently I’m thinking I’ll just have an array of key-values, where the key is the rpm, and the value is the torque.
I’m just wondering if unity has any better ways of handling this/maybe it provides some form of graph editing to add this info?
You could make an array of key values, then find the closest two keys to your current value and Lerp between them…
but I’ve been using this to handle my engine torque. Basically it uses the animation curve editor to handle torque curves. It is super handy!
It might be worth looking into animation curves and seeing if they are a good solution to what you’re trying to do. You can add a public AnimationCurve object to a script and then edit it through the inspector. During runtime you can query the Y value of the curve at any give X value.
Editing Curves
For things like that you want to use an AnimationCurve. Just declare a public variable like this:
public AnimationCurve curve;
You can click on the curve in the inspector. This will open up the curve editor. There you can setup the curve the way you like. Inside your code you would simply do this:
float v = curve.Evaluate(t);
Where “t” is the x axis value and the returned value v is the y-axis value.
The curve can be as complex as you like. You can simply add keyframes and adjust the tangents any way you like.
Usually the input and output values are between 0.0 and 1.0 but you can choose whatever you like.