[Unity]Input & Controller Notes
Keywords: Unity, Input, Controller
Quick Start
Ensure Input System
was installed in Package Manager
.
1, Select Player Object.
2, Add Player Input
Component.
3, Create default actions.
when Save
window pop up, choose directory for saving.
4, Open Input Action Asset. Now you would see default actions.
5, Check Generate C# Class
in Inspector
panel of Input Action Asset, then click Apply
.
Now you would see the C# script.
There’re codes which were generated automatically.
6, Then we add callback functions of Move
and Fire
actions.
Add a script named PlayerMovement
for Player
first.
Remove PlayerInput
Component which was added previously.
Then add code of action callback:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
private TestInput Input;
private Vector2 movementInput;
void Awake()
{
Input = new TestInput();
Input.Player.Move.performed += ctx => OnMovementChanged(ctx);
Input.Player.Fire.performed += ctx => OnFire();
}
void OnMovementChanged(InputAction.CallbackContext Context)
{
movementInput = Context.ReadValue<Vector2>();
Debug.Log("Move Direction: " + movementInput.ToString());
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void FixedUpdate()
{
}
private void OnEnable()
{
Input.Enable();
}
private void OnDisable()
{
Input.Disable();
}
void OnFire()
{
Debug.Log("Fire+++++++");
}
}
Now when press Arrow Key or click left mouse button, OnMovementChanged()
and OnFire()
would be fired.
Another way to register callback functions.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
[Space]
[SerializeField]
private InputActionAsset InputAssetRef;
private Vector2 movementInput;
void Awake()
{
InputActionMap PlayerInputMap = InputAssetRef.FindActionMap("Player");
InputAction MoveAction = PlayerInputMap.FindAction("Move");
MoveAction.performed += ctx => OnMovementChanged(ctx);
InputAction FireAction = PlayerInputMap.FindAction("Fire");
FireAction.performed += ctx => OnFire();
}
void OnMovementChanged(InputAction.CallbackContext Context)
{
movementInput = Context.ReadValue<Vector2>();
Debug.Log("Move Direction: " + movementInput.ToString());
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void FixedUpdate()
{
}
private void OnEnable()
{
InputAssetRef.Enable();
}
private void OnDisable()
{
InputAssetRef.Disable();
}
void OnFire()
{
Debug.Log("Fire+++++++");
}
}
Then specify the Input Action Asset in Inspector
panel
Input Issues
GetKeyUp not registering after a long hold
Case:
Input.GetKeyUp()
would not work after holding ended.
Caused by:
If hold key, Input.GetKey()
would return true
, and return false
after holding ended (key up), and GetKeyUp
would not work on key up.
Solution:
Using Input.GetKey()
to check if key had been held.
GetKey = machine gun
function Update () {
if (Input.GetKey (KeyCode.Space))
print ("space key is being held down");
}
GetKeyDown = single shot gun
function Update () {
if (Input.GetKeyDown (KeyCode.Space))
print ("space key was pressed once");
}
finally we have the key released handler
function Update () {
if (Input.GetKeyUp (KeyCode.Space))
print ("space key was released");
}
Reference:
http://johnstejskal.com/wp/understanding-getbutton-and-getkey-inputs-in-unity/
Input Reference
New Input (from 2019.1)
NEW INPUT SYSTEM in Unity - Brackeys (Recommended)
https://www.youtube.com/watch?v=Pzd8NhcRzVo
New Unity INPUT SYSTEM - Getting Started (Recommended)
https://www.youtube.com/watch?v=KNiM53UbGfA
Unity Input System | How to Use Input Action Assets (Recommended)
https://www.youtube.com/watch?v=mvuXOyKz7k4
Introducing the new Input System
https://blogs.unity3d.com/2019/10/14/introducing-the-new-input-system/
Quick start guide
https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/QuickStartGuide.html?_ga=2.151614137.1431303805.1581172995-179986836.1581172995
An efficient and versatile input system for Unity.
https://github.com/Unity-Technologies/InputSystem
Input - Joystick
Keyboard and Joystick Controls in Unity 2019! (New Input System Tutorial)
https://www.youtube.com/watch?v=Gz0YcjXBJ3U
https://github.com/dawnarc/UnityExamples/tree/master/TopDownShooter
Controller Reference
Controller - Camera Controller
Unity3D 8 Directional Character System
https://www.youtube.com/watch?v=cVy-NTjqZR8
In heaven, all the interesting people are missing. ― Friedrich Nietzsche