Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Input System | Write your First Script
Unity for Beginners

bookInput System

Sveip for å vise menyen

Let's talk about the Unity Input System, helping you capture everything from keyboard taps to joystick tilts and turn them into exciting gameplay actions.

Understanding Key Methods

  • Input.GetAxis;
  • Input.GetButton;
  • Input.GetKey.

These three methods are your primary tools for capturing user input in Unity. Let's explore their strengths, weaknesses, and ideal use cases:

Input.GetAxis

  • Purpose: reads the magnitude and direction of an axis-based input device, such as an analog joystick, keyboard keys (WASD), or mouse movement;
  • Returns: a value between -1 (full negative) and 1 (full positive). 0 indicates no input, positive values represent positive movement on the axis, and negative values represent negative movement;
  • Applications: useful for continuous actions like movement, steering, camera rotation, or gradual changes in intensity (e.g., accelerating a car gradually).

Example: captures left/right movement on a keyboard or joystick.

private void Update()
{
    float horizontalInput = Input.GetAxis("Horizontal");
}

Input.GetButton

  • Purpose: detects whether a discrete button (e.g., keyboard spacebar, gamepad A button, touch screen tap) is being held down (true) or not (false);
  • Returns: a boolean value (true or false);
  • Applications: ideal for toggling actions like jumping, firing a weapon, using an ability, or activating menus.

Example: checks for the jump button being pressed.

private void Update()
{
    bool isJumping = Input.GetButton("Jump");
}

Input.GetKey

  • Purpose: detects whether a specific keyboard key is being held down (true) or not (false);
  • Returns: a boolean value (true or false);
  • Applications: primarily used for keyboard-based controls, for actions like inventory management, hotkeys, or character abilities.

Example: checks for the Shift key being held down.

private void Update()
{
    bool isShiftPressed = Input.GetKey(KeyCode.LeftShift);
}

Choosing the Right Input Method

Choosing the correct input method is crucial for creating intuitive and responsive gameplay. Here's a quick guide:

  • Use Input.GetAxis for continuous, direction-sensitive control, such as character movement or camera rotation;
  • Use Input.GetButton for simple on/off toggle actions, like jumping or firing a weapon;
  • Use Input.GetKey for keyboard-specific controls, ideal for actions like inventory management or activating hotkeys.

Remember:

  • Efficiency: avoid calling these methods every frame unnecessarily, as it can impact performance. Instead, use conditional checks or event-based handling when possible;
  • Experimentation: try different combinations and discover what works best for your game's design and target audience;

Experimenting will help you find the most intuitive and responsive controls.

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 5

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Seksjon 2. Kapittel 5
some-alt