Summary - Input
- Game Development course
- Beginner
- Lesson 48/83
- 4 min
Reinforce Input usage with a summary task.
Input Management Summary
Input handling in Unity directly determines the game's responsiveness and feel. For key states, Input.GetKey, Input.GetKeyDown, and Input.GetKeyUp are used, while Input.GetAxis is preferred for analog and directional movement. Mouse clicks and screen coordinates are accessed via GetMouseButton and mousePosition.
if (Input.GetKeyDown(KeyCode.Space)) Jump();
float h = Input.GetAxis("Horizontal");Practice Comprehensive Input Controller
Complete the character input logic: if Input.GetKeyDown(KeyCode.Space) is true, print Debug.Log("Zipla");. Then assign Input.GetAxis("Horizontal") to variable yatay and print Debug.Log("Eksen: " + yatay);.
Starter code
using System;
public enum KeyCode { Space, Escape }
public static class Input
{
public static bool GetKeyDown(KeyCode key) => key == KeyCode.Space;
public static float GetAxis(string axis) => 1.0f;
}
public static class Debug
{
public static void Log(object message) => Console.WriteLine(message);
}
public class Program
{
public static void Main()
{
// Space kontrolu yap ve "Zipla" yazdir
// Horizontal eksenini oku ve "Eksen: " ile yazdir
}
}Sample solution
using System;
public enum KeyCode { Space, Escape }
public static class Input
{
public static bool GetKeyDown(KeyCode key) => key == KeyCode.Space;
public static float GetAxis(string axis) => 1.0f;
}
public static class Debug
{
public static void Log(object message) => Console.WriteLine(message);
}
public class Program
{
public static void Main()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("Zipla");
}
float yatay = Input.GetAxis("Horizontal");
Debug.Log("Eksen: " + yatay);
}
}Expected output
Zipla Eksen: 1
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which of the following returns true only on the first frame a key is pressed, rather than while being held?
- Input.GetKey
- Input.GetKeyDown Correct answer
- Input.GetAxis
- Input.mousePosition
Explanation: GetKeyDown returns true only on the initial frame the key is pressed; GetKey continues to return true as long as it is held.
2 Summary Fill in the Blank
if (Input.____(KeyCode.Escape))
if (Input.GetKeyDown(KeyCode.Escape))
Explanation: To detect a single press of a menu or exit key, GetKeyDown is used.
3 All Input Concepts
| Term | Meaning |
|---|---|
Input.GetKeyDown |
Catches first frame a key is pressed |
Input.GetAxis |
Returns axis input value as float |
Input.GetMouseButton |
Checks if mouse button is held down |
new Vector3(x, 0, z) |
Creates movement vector from axes |