- Home /
"left is not set up when it is set up under input. / Axes / horizontal...."
Hi, Im trying to make a control set up for my Submerine. however input is not set up when i go into Input and is set up under Axes vertical and horizontal by unity default.
public static float SubmerineSpeed = 12f;
public static float SubmerinePower = 100;
public static float SubmerineDrivePower = 10f;
void Update () {
//_____________________________________________________________________________________________
/// USER CONTROLLS HERE.........
if(Input.GetButtonDown("left"))
{
transform.Translate(Vector3.up * SubmerineDrivePower * Time.deltaTime);
}
if(Input.GetButtonDown("right"))
{
transform.Translate(-Vector3.up * SubmerineDrivePower * Time.deltaTime);
//.................................................................................
}
if(Input.GetButtonDown("up"))
{
transform.Translate(Vector3.right * SubmerineDrivePower *Time.deltaTime);
}
if(Input.GetButtonDown("down"))
{
transform.Translate(-Vector3.right * SubmerineDrivePower * Time.deltaTime);
}
} // end of update....
Answer by Eno-Khaon · Sep 10, 2015 at 09:08 PM
GetKey()-type functions are strictly limited to a single key at a time. GetButton()-type functions are based around defined keys in Unity. Unless you made a version for each direction pressed, they are separate from GetAxis() functions and provide less granularity.
By contrast, GetAxis() and GetAxisRaw() are a primary intended movement scheme. GetAxis() permits partial input (namely, from a game controller's analog stick(s)) with an input range from -1 to 1.
This means you would use them as follows (matching your up/down = Vector3.right and right/left = Vector3.up scheme):
Vector3 inputDirection = (Vector3.right * Input.GetAxis("Vertical")) + (Vector3.up * Input.GetAxis("Horizontal"));
transform.Translate(inputDirection * SubmarineDrivePower * Time.deltaTime);
As an additional note, GetButtonDown() is limited to only taking place on the first frame it is pressed, whereas GetButton() is called every frame the key is held. Because of this difference, multiplying by Time.deltaTime greatly influences input when paired with single-frame input instructions.
As an alternative, if you would prefer to create your own control-handling scheme, calling Events could be used to determine and define keys for specific purposes.
Your answer