Unity Input System: Control Schemes

Hal Brooks
3 min readMar 20, 2022

--

Objective: Create a keyboard and Xbox controller scheme for input actions.

Using the keyboard WASD or arrow keys the cube is blue, turning yellow when using the Xbox controller.

Input control schemes are different input systems which can be linked to actions based upon hardware being used. For this article, add a control scheme for Keyboard and Xbox controller. Each path (denoted by pink) within a binding (blue) for an action (green) has a toggle for either Keyboard or Xbox controller. Notice that multiple commands can be used to trigger the same Move action within the Player map. WASD and Arrows can be used to move, and both are part of the Keyboard control scheme. Whereas, Xbox LeftStick is used solely for the Xbox controller movement. Regardless of which input is used they are part of the Move action, using common methods.

Create a input control scheme and define requirements.

The beauty of the new Unity input system is that all paths within an action will share common implementation, for example Move or Rotate. Implementation will occur within the PlayerInput script on the Player game object. Six variables will be used to control _input for PlayerInputActions, _renderer to store the MeshRenderer, three floats to control rotation euler, _rotationSpeed and _rotationInput, and a Vector2 _moveInput and float _speed to control Player movement.

Variable definitions in PlayerInput script.

The _renderer is assigned in voidStart and color set to yellow. The _input is set to the PlayerInputScript and the Player map is enabled. A single method, Rotate_performed, are subscribed to _input.Player.Rotate for all controllers, as can the Rotate_canceled. Similarly a single Move_performed and Move_canceled are used subscribed to _input.Player.Move.

Initializing variables and _input in PlayerInput script.

The Rotate_performed and Rotate_canceled are shown below. Both modify _rotationInput.

Rotate_performed and Rotate_canceled method in PlayerInput script.

The Move_performed modifies _moveInput, but also defines control as obj.control. The control is then spilt into a string[] words array based upon the ‘/’ character. The controller used to provide input be identified from words[1]. Move_canceled sets _moveInput to Vector2.zero. The color is changed based upon the type of controller. This is done to visualize when I change controller input, and not needed for game input.

Move_performed and Move_canceled method in PlayerInput script.

If player input has been received then the Movement() method is called from void Update(). Within Movement(), the _euler angle is tracked independent of the quaternion to avoid gimbal lock, and is modified based upon _rotationInput and _rotationSpeed. The _moveInput is converted to the Vector3 direction. The direction is used to translate the Player, in combination with _moveInput and _speed.

Movement method within PlayerInput script.

One useful tool is the input debugger which is accessed from Unity’s menu window > analysis, which shows all input devices and their names.

Input debug shows input devices.

--

--