The Input Manager window allows you to define input axes and their associated actions for your Project. To access it, from Unity’s main menu, go to Edit > Project Settings, then select Input Manager from teh navigation on the right.
The Input Manager uses the following types of controls:
Every Project you create has a number of input axes created by default. These axes enable you to use keyboard, mouse, and joystick input in your Project straight away.
To see more about these axes, open the Input Manager window, and click the arrow next to any axis name to expand its properties.
Each input axis has the following properties:
|Nombre||Axis name. You can use this to access the axis from scripts.|
|Descriptive Name, Descriptive Negative Name||These values are deprecated and do not work. Previously, they displayed for the user on the Rebind Controls screen at startup, but this screen has also been deprecated.|
|Negative Button, Positive Button||The controls to push the axis in the negative and positive direction respectively. These can be keys on a keyboard, or buttons on a joystick or mouse.|
|Alt Negative Button, Alt Positive Button||Alternative controls to push the axis in the negative and positive direction respectively.|
|Gravity||Speed in units per second that the axis falls toward neutral when no input is present.|
|Dead||How far the user needs to move an analog stick before your application registers the movement. At runtime, input from all analog devices that falls within this range will be considered null.|
|Sensitivity||Velocidad en unidades por segundo en el que el eje se moverá hacia el valor objetivo. Esto es solo para dispositivos digitales.|
|Snap||If enabled, the axis value will reset to zero when pressing a button that corresponds to the opposite direction.|
|Tipo||The type of input that controls the axis. Select from these values:
- Key or Mouse button
- Mouse Movement
- Joystick Axis
|Axis||The axis of a connected device that controls this axis.|
|JoyNum||The connected Joystick that controls this axis. You can select a specific joystick, or query input from all joysticks.|
Axis values can be:
To add a virtual axis, increase the number in the Size field. This creates a new axis at the bottom of the list. The new axis copies the properties of the previous axis in the list.
To remove a virtual axis, you can either:
To copy a virtual axis, right-click it and select Duplicate Array Element.
To map a key or button to an axis, enter its name in the Positive Button or Negative Button property in the Input Manager.
Key names follow these naming conventions:
|Key family||Naming convention|
Mouse buttons are named
mouse 0, mouse 1, mouse 2, and so on.
Joystick buttons follow these naming conventions:
|Button origin||Naming convention|
|A specific button on any joystick||
|A specific button on a specific joystick||
You can also query input for a specific key or button with
Input.GetKey and the naming conventions specified above. For example:
Another way to access keys is to use the
To access virtual axes from scripts, you can use the axis name.
For example, to query the current value of the Horizontal axis and store it in a variable, you can use
Input.GetAxis like this:
float horizontalInput = Input.GetAxis ("Horizontal");
For axes that describe an event rather than a movement (for example, firing a weapon in a game), use
If two or more axes have the same name, the query returns the axis with the largest absolute value. This makes it possible to assign more than one input device to an axis name.
For example, you can create two axes named Horizontal and assign one to keyboard input and the other to joystick input. If the user is using the joystick, input comes from the joystick and keyboard input is null. Otherwise, input comes from the keyboard and joystick input is null. This enables you to write a single script that covers input from multiple controllers.
You can use input from the Horizontal and Vertical axes and the
transform.Translate method to move a GameObject in XZ space (forward, back, left, or right). Add the following code to the
update() method on a script attached to the GameObject you want to move:
float moveSpeed = 10; //Define the speed at which the object moves. float horizontalInput = Input.GetAxis("Horizontal"); //Get the value of the Horizontal input axis. float verticalInput = Input.GetAxis("Vertical"); //Get the value of the Vertical input axis. transform.Translate(new Vector3(horizontalInput, verticalInput, 0) * moveSpeed * Time.deltaTime); //Move the object to XYZ coordinates defined as horizontalInput, 0, and verticalInput respectively.
Time.deltaTime represents the time that passed since the last frame. Multiplying the
moveSpeed variable by
Time.deltaTime ensures that the GameObject moves at a constant speed every frame.