- Home /
how do I add a non linear curve to an axis input
I have a steering input that I want to make non-linear, so with more lock the turn rate increases.
It is based on an xBox controller, currently this is on a input.getaxis which is multiplied by a speed constant and then clamped between -1 and 1.
Could I increase the speed for a certain angle or can I map it to a curve?
Thank you
Answer by AlwaysSunny · Dec 26, 2016 at 04:49 PM
I have found that the easiest way to work with curves with native Unity is the AnimationCurve type.
public AnimationCurve myAnimationCurve;
This may not always satisfy every need, but it sounds appropriate for your stated requirement. Design the curve in the inspector, or optionally build or edit the curve handles at runtime with the API.
The argument used to evaluate your current position on the curve expects a normalized value (zero-to-one range). This can be derived from whatever variable you like, such as the normalized percentage of "lock" your vehicle is experiencing.
// assumes lock is positive and minimum lock is zero
float lockPercentage = myCurrentLockValue / maximumLockValue;
float steeringSensitivity = myAnimationCurve.Evaluate( lockPercentage );
Thank you for this - I'm still trying to work out how to add the animation curve lol
I need to be able to have a negative valu e(steering left to right for example) - will this still work?
When you declare a variable whose type is AnimationCurve as public, it'll expose it in the inspector. You can click this field to open Unity's graph editor. It's not the best one in the world, but I'm sure you can create the kind of curve you need, including negative values.
When you read from that curve with your call to myAnimationCurve.Evaluate(), your argument tells Unity where along the curve to read from. The left of the curve corresponds to an argument of zero, the right corresponds to one, and all points in between. Check out the AnimationCurve documentation for more details. :)
Answer by katielawrence36 · Jan 19, 2017 at 12:07 AM
Hello
I added the following code:
SteerAngle = Input.GetAxis("Wheel") * 0.08f;
SteerAngle = Mathf.Clamp(SteerAngle, -1.0f, 1.0f);
//Code added by KL as a test
float SteeringSensitivity = SteeringCurve.Evaluate(SteerAngle);
As well as the public AnimationCurve SteeringCurve
I am debugging in unity now but it still does not work. I created the attached curve.
Any ideas much appreciated :-)
Thank you
,Hi
I added the
public AnimationCurve SteeringCurve;
and the following code
SteerAngle = Input.GetAxis("Wheel") * 0.08f;
SteerAngle= Mathf.Clamp(SteerAngleAngle, -1.0f, 1.0f);
//Code added by KL as a test
float SteeringSensitivity = SteeringCurve.Evaluate(SteerAngle);
And also the attached curve. I'm currently debugging in Unity but the xBox controller input for steering no longer seems to work, have I missed something?
Thank you
Your answer
![](https://koobas.hobune.stream/wayback/20220612103245im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Control object (character) rotation with 360 analogue. 1 Answer
Input manager is conflicted, help? 0 Answers
Xbox 360 gamepad numbers mixed up 0 Answers
3rd Person Free Roam...Where to Start? 1 Answer
How do you publish your game? 1 Answer