Unity is a powerful engine for game development that supports game development across various platforms.
The input handling system of a game is essential for interacting with players, and it is very important to learn how to recognize the player’s commands through keyboard and mouse input.
This course will provide a detailed explanation of how to detect input signals from the keyboard and mouse in Unity.
Through this, you will learn how to handle basic user input and implement various features of the game based on this knowledge.
1. Overview of Unity Input System
Unity provides a basic input system that helps developers easily manage input.
The input system collects input from various devices such as keyboards, mice, and gamepads and converts it into commands.
The flow of input in Unity is as follows:
- Input Event Detection: The user sends commands using input devices.
- Input Processing: These input events are detected by Unity’s input processing system.
- Interacting with Game Objects: The input events allow interaction with objects in the game.
2. Detecting Keyboard Input
In Unity, the Input
class is used to detect keyboard input. This class provides various methods to check if a specific key is pressed or being held down.
The main methods are:
Input.GetKey(KeyCode)
: Detects if a specific key is being held down.Input.GetKeyDown(KeyCode)
: Detects the moment a specific key is pressed for the first time.Input.GetKeyUp(KeyCode)
: Detects the moment a specific key is released after being pressed.
2.1 Keyboard Input Example
Below is a simple script example that detects keyboard input. This script outputs a message to the console when a specific key is pressed.
using UnityEngine;
public class KeyboardInputExample : MonoBehaviour
{
void Update()
{
if (Input.GetKeyDown(KeyCode.W))
{
Debug.Log("W key has been pressed.");
}
if (Input.GetKeyUp(KeyCode.W))
{
Debug.Log("Released the W key.");
}
if (Input.GetKey(KeyCode.S))
{
Debug.Log("S key is being held down.");
}
}
}
2.2 Key Input Response
Let’s add an example where the player object moves in response to key input.
The code below moves the player character using the W, A, S, D keys.
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5f;
void Update()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
transform.Translate(movement * moveSpeed * Time.deltaTime);
}
}
3. Detecting Mouse Input
Mouse input can be used in various ways in Unity, including click detection, mouse movement, and scrolling.
The most basic way to detect mouse input is by using Input.mousePosition
and Input.GetMouseButton
methods.
3.1 Detecting Mouse Clicks
To detect mouse clicks, you can use Input.GetMouseButton(int button)
. The button
parameter accepts 0 (left button), 1 (middle button), and 2 (right button).
using UnityEngine;
public class MouseClickExample : MonoBehaviour
{
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Debug.Log("Left mouse button has been clicked.");
}
}
}
3.2 Mouse Position and Dragging
Let’s create an example that tracks the mouse position and adds functionality to drag the mouse to move an object.
using UnityEngine;
public class MouseDragExample : MonoBehaviour
{
private Vector3 offset;
private Camera mainCamera;
void Start()
{
mainCamera = Camera.main;
}
void OnMouseDown()
{
offset = transform.position - GetMouseWorldPosition();
}
void OnMouseDrag()
{
transform.position = GetMouseWorldPosition() + offset;
}
private Vector3 GetMouseWorldPosition()
{
Vector3 mouseScreenPosition = Input.mousePosition;
mouseScreenPosition.z = mainCamera.nearClipPlane; // Camera's near clipping plane
return mainCamera.ScreenToWorldPoint(mouseScreenPosition);
}
}
4. Integrating Input System into Your Game
The examples so far have shown how to detect and respond to inputs individually. In practice, games need to integrate various inputs to create more complex responses.
By integrating the input system, you can make player actions more natural and intuitive.
4.1 Interacting with User Interface (UI)
Interaction with the UI is an important part of the input system. For example, you can perform specific actions when a button is clicked.
Let’s look at how to create a button using Unity’s UI system and interact with that button.
using UnityEngine;
using UnityEngine.UI;
public class ButtonClickExample : MonoBehaviour
{
public Button myButton;
void Start()
{
myButton.onClick.AddListener(OnButtonClick);
}
void OnButtonClick()
{
Debug.Log("Button has been clicked.");
}
}
4.2 Complex Input Processing
Learn how to handle keyboard, mouse, and UI inputs together in a game.
A simple example is to move a game object by clicking on it while simultaneously using the keyboard to perform other actions.
using UnityEngine;
public class CombinedInputExample : MonoBehaviour
{
public float moveSpeed = 5f;
void Update()
{
if (Input.GetMouseButton(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
transform.position = Vector3.MoveTowards(transform.position, hit.point, moveSpeed * Time.deltaTime);
}
}
if (Input.GetKey(KeyCode.W))
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
}
}
5. Conclusion
In this course, we explored how to detect keyboard and mouse input in Unity.
We learned the basic methods for handling user input and integrating this into various features of the game.
Now you are ready to explore creative ideas based on these input systems.
As the next step, it is recommended that you study more complex input processing and how to optimize user experience.
By gaining more information and hands-on experience, I hope you will develop a deeper understanding of Unity’s input system.
Now, start your game development journey!