How to make new Axis?

Hello,

I was wandering if it is possible to make a new axis via script.

Like having

var moveRight : KeyCode;
var moveLeft : KeyCode;

And then combining them to a new axis like:

Input.GetAxis("MyAxis");

Thanks! :slight_smile:

You can put “MyAxis”, but in order for it to work you have to go into edit at the top of the screen, then go to input and then it will say axis. You will have to put a different number of axis for example if you have 22 axis then you have to put 23. Then you can change the name of the axis to your own name which would be “MyAxis” then assign a button for it. Once thats done then you can make your axis work for whatever you want.

No, it’s not possible through code.

I don’t know how to add a new axis to the input manager from code but you can make your own virtual axis like this. Works pretty good but not perfect when going from opposite ends.

public float axisSpeed = 3; //I did some tests and 3 seems to be the closest to the real axis.

if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
{
    if (Input.GetKey(KeyCode.A))
    {
        test -= Time.deltaTime * axisSpeed;
    }
    if (Input.GetKey(KeyCode.D))
    {
        test += Time.deltaTime * axisSpeed;
    }
    test = Math.Clamp(test, -1, 1);
}
else
{
    driftToZero();
}
if (test != temp2 && (test == 1 || test == 0 || test == -1))
{
    Debug.Log("Test Axis: " + test);
}
temp2 = test;

void driftToZero()
{

    if (test < 0)
    {
        test += Time.deltaTime * axisSpeed;
        if (test > 0)
        {
            test = 0;
        }
    }
    else if (test > 0)
    {
        test -= Time.deltaTime * axisSpeed;
        if (test < 0)
        {
            test = 0;
        }
    }

}