Summary - Advanced input
- Game Development course
- Advanced
- Lesson 60/71
- 4 min
Combine the advanced input project with a single summary task.
Recap of the advanced input project
In this project you built the input stack from the package up: install Input System from the Package Manager, then define actions and bindings with a .inputactions asset. The PlayerInput component listens to the actions, and Generate C# Class gives a compile-time checked wrapper.
InputAction jump = new InputAction(name: "Jump", type: InputActionType.Button);
jump.AddBinding("<Keyboard>/space");
jump.Enable();
Debug.Log("Hazir");With gamepad bindings and control schemes the same game runs on keyboard and gamepad.
Practice Print the input summary
Write a MonoBehaviour named GirdiOzeti. In Start declare a string array named eylemler holding Move and Jump. Fill a second array named baglamalar with <Gamepad>/leftStick and <Keyboard>/space in order. Walk both arrays with a for loop and print each pair with Debug.Log(eylemler[i] + " -> " + baglamalar[i]). After the loop call Debug.Log("Toplam: " + eylemler.Length).
Starter code
using UnityEngine;
public class GirdiOzeti : MonoBehaviour
{
void Start()
{
// eylemler dizisini Move ve Jump degerleriyle doldur
// baglamalar dizisini sirasiyla sol cubuk ve bosluk tusu yollariyla doldur
// for dongusuyle iki diziyi birlikte dolas ve her cifti yazdir
// toplam eylem sayisini yazdir
}
}Sample solution
using UnityEngine;
public class GirdiOzeti : MonoBehaviour
{
void Start()
{
string[] eylemler = { "Move", "Jump" };
string[] baglamalar = { "<Gamepad>/leftStick", "<Keyboard>/space" };
for (int i = 0; i < eylemler.Length; i++)
{
Debug.Log(eylemler[i] + " -> " + baglamalar[i]);
}
Debug.Log("Toplam: " + eylemler.Length);
}
}Expected output
Move -> <Gamepad>/leftStick Jump -> <Keyboard>/space Toplam: 2
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which event fires when an action actually happens?
- performed Correct answer
- started
- canceled
- enabled
Explanation: started is the beginning of the input, performed is the action happening, and canceled is the input being released; enabled is not an event but the name of the Enable method.
2 Write the action type
InputAction jump = new InputAction(name: "Jump", type: InputActionType.____);
InputAction jump = new InputAction(name: "Jump", type: InputActionType.Button);
Explanation: Jumping is a one-shot trigger so the type is Button; Value is for continuous analog values, and Axis is not a valid type.
3 Advanced input recap
| Term | Meaning |
|---|---|
Input System |
Package keeping actions device-free |
InputAction |
Object for a single input function |
OnJump |
Callback that runs when Jump triggers |
2DVector |
Merge of WASD keys into one Vector2 |