Unity Input System πΉοΈ
There are 3 ways of handling user input in Unity Games:
- Manually
- Using the new input system
- Using the old input system
β οΈ Todo: using input to move a character is not a good practice?
Delta Time
As per what is explained about the Game Loop, the time between updates is inconsistent. To ensure something moves at 10 m/s
, we use Time.deltaTime
in the update method:
// defaultSpeed is an attribute with the value "10"
var speed = defaultSpeed * Time.deltaTime;
Old Input System
Input System Configuration
Navigate to Edit > Project Settings > Input Manager. Expand Axes.
You will define each "control" of the game. For instance, you can define that "Jump" is done by pressing the left-click of the mouse. From the code, only the control name is referenced, so the key can be changed relatively easily over time.
-
Horizontal: you can see that there is
left
/right
to move, whileA
andD
are marked as alternative keys. -
Vertical: you can see that there is
down
/up
to move, whileS
andW
are marked as alternative keys. -
Jump: the default key is
space
To add more keys, increase the size of the array.
β‘οΈ The "dead" property is a range where if a value is within that range, then it's considered as "null"/0.
β‘οΈ The "negative" means the value will be negative in "GetAxis".
β‘οΈ The "sensitivity" is the response time (low=smooth, great=fast)
Raw Input
When using non-raw input, for instance, when moving a character, the character will still move a bit after the key is released. If this behavior is not acceptable, we can use:
float axisX = Input.GetAxisRaw("Horizontal");
Discontinuous Input
User presses a key and releases it.
// <0 (left) or 0 (none) or 0> (right)
float axisX = Input.GetAxis("Horizontal");
// <0 (down) or 0 (none) or 0> (up)
float axisY = Input.GetAxis("Vertical");
if (Input.GetButton("Jump")) // if "Jump" triggered
{
// do action
}
Continuous Input
User holds a key.
if (Input.GetButtonDown("Jump"))
{
_isJumpPressed = true; // change the state
}
if (Input.GetButtonUp("Jump"))
{
_isJumpPressed = false; // revert the state
}
Manual Input Handling
For testing purposes, as it's a bad practice. Use an input system.
Keyboard Input
bool pressed = Input.GetKey(KeyCode.Space);
bool down = Input.GetKeyDown(KeyCode.Space);
bool up = Input.GetKeyUp(KeyCode.Space);
Mouse Input
// b is 0 = Left-Click, 1 = Right-Click, 2 = Middle-Click
Input.GetMouseButton(b);
Input.GetMouseButtonDown(b);
Input.GetMouseButtonUp(b);
Random
Mouse Events
Each game object has these methods that you can override:
private void OnMouseUp() {}
private void OnMouseDown() {}
private void OnMouseOver() {}
private void OnMouseEnter() {}
private void OnMouseExit() {}
Hold Events
You may use the following interfaces:
using UnityEngine.EventSystems;
public class XXX :
IPointerUpHandler, IPointerDownHandler {}
Drag Events
You may use the following interfaces:
using UnityEngine.EventSystems;
public class XXX :
IDragHandler, IBeginHandler, IEndDragHandler,
IPointerEnterHandler IPointerExitHandler {}
π» To-do π»
Stuff that I found, but never read/used yet.
EventSystem.current.IsMouseOverGameObject()