【问题标题】:Detect When Key Lifted - New Unity Input System检测何时解除键 - 新的 Unity 输入系统
【发布时间】:2023-01-30 14:29:07
【问题描述】:

我目前正在使用 Unity 开发平台视频游戏,但不确定如何使用新的输入系统检测何时按下了键。

我想知道,如果可能的话,您能否使用我当前的设置检测到何时按下键并将值分配给布尔值。

到目前为止,我有这段代码:

    public void Jump(InputAction.CallbackContext context)
    {
        if (context.performed)
        {
            jump = true;
            wasJumpLifted = true;
        }

        if (context.canceled)
        {
            wasJumpLifted = false;
        }
        else
        {
            wasJumpLifted = true;
        }
    }

这段代码的问题在于,如果您再次按下跳转键,它只会将 wasJumpLifted 设置为 true,因为如果我不按下跳转键,整个函数将不会被调用。

【问题讨论】:

    标签: c# unity3d input


    【解决方案1】:

    这是解决方案

    public bool isPressed;
    
    public void Jump(InputAction.CallbackContext context)
    {
        // To keep the state in a boolean.
        isPressed = (!context.started || context.performed) ^ context.canceled;
        
        // When the key is pressed down.
        if (context.started && !context.performed)
            Debug.Log("Pressed Down");
        
        // When the key is lifted up.
        if (context.canceled && !context.performed)
            Debug.Log("Lifted Up");
    }
    

    在我的项目中,我使用单线:

    public void Jump(InputAction.CallbackContext context) => isPressed = (!context.started || context.performed) ^ context.canceled;
    

    这可能很难阅读,但您什么时候需要阅读代码来处理这些操作?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-31
      • 2021-08-20
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 2012-02-03
      • 2022-07-06
      相关资源
      最近更新 更多