Volver a la página principal
sábado 15 febrero 2025
3

Cómo detectar la tecla shift izquierdo en unity

En Unity, puedes detectar la tecla Shift izquierdo (LeftShift) utilizando Input.GetKey() o Input.GetKeyDown() dentro del método Update().

Ejemplo de detección de Shift Izquierdo

void Update()
{
    if (Input.GetKey(KeyCode.LeftShift))
    {
        Debug.Log("Left Shift is being held down");
    }

    if (Input.GetKeyDown(KeyCode.LeftShift))
    {
        Debug.Log("Left Shift was just pressed");
    }

    if (Input.GetKeyUp(KeyCode.LeftShift))
    {
        Debug.Log("Left Shift was released");
    }
}

Explicación de las funciones

  • Input.GetKey(KeyCode.LeftShift): Detecta si la tecla está siendo presionada en este frame.
  • Input.GetKeyDown(KeyCode.LeftShift): Detecta cuando la tecla fue presionada en este frame (solo una vez).
  • Input.GetKeyUp(KeyCode.LeftShift): Detecta cuando la tecla fue soltada en este frame.

Alternativa con el sistema de entrada "New Input System"

Si estás usando el nuevo sistema de entrada (UnityEngine.InputSystem), puedes hacerlo así:

using UnityEngine;
using UnityEngine.InputSystem;

void Update()
{
    if (Keyboard.current.leftShiftKey.isPressed)
    {
        Debug.Log("Left Shift is being held down (New Input System)");
    }
}
Etiquetas:
csharp unity
Compartir:
Creado por:
Author photo

Jorge García

Fullstack developer