Introduction to Input Handling
Avatar Movement Using On-screen Joystick
Introduction to Input Handling
Before we discuss the integration of joystick UI to control our avatar, we needed to obtain a basic understanding of input handling in Unity.
Input handling in Unity refers to the process of managing and responding to user input from various devices such as keyboards, mice, touch screens, and game controllers. Unity provides a built-in system for handling input, making it easier for developers to create interactive and responsive games and applications.
Unity provides a number of ways to handle the user inputs. Take a look at the following code:
if (Input.GetKey(KeyCode.A)) print("A hold");
if (Input.GetKeyDown(KeyCode.A)) print("A down");
if (Input.GetKeyUp(KeyCode.A)) print("A up");
if (Input.GetMouseButton(0)) print("left click hold");
if (Input.GetMouseButtonDown(0)) print("left click down");
if (Input.GetMouseButtonUp(0)) print("left click up");
The GetKey() / GetKeyDown() / GetKeyUp() methods handle the user input from the keyboard.
GetKey() method will be called as long as the specified key is being held down.