Unity 未检测到 F1 和 F2 按键

Unity not detecting F1 and F2 key presses

我在 6 个月后回归统一。我昨天开始了我的 fps 游戏。今天我试图制作 C4 炸弹,其工作方式如下:c4 已经放置,您只需使用遥控器激活即可。开始时遥控器不会在您手中,您必须按 F1 将其设置为激活状态。要再次收起遥控器,我们必须按 F2。但问题是它不会检测到我的 F1 按键。这是代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class C4Script : MonoBehaviour
{
    public GameObject remote;
    private bool remoteHand;
    // Start is called before the first frame update
    void Start()
    {
        remote.SetActive(false);
        remoteHand = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.F1) && !remoteHand)
    {
        remote.SetActive(true);
        remoteHand = true;
    }
    if (Input.GetKeyUp(KeyCode.F2) && remoteHand)
    {
        remote.SetActive(false);
        remoteHand = false;
    }
  }
} 

代码格式可能有误,对此深表歉意。 如果我没有正确解释,也很抱歉。 谢谢!

该代码在我的机器上运行,您是否禁用了它所附加的 C4Script 或游戏对象?

看起来脚本从未被调用,而是它没有检测到输入。您可以在启动方法中添加一个调试日志或调试断点来检查它是否被调用过。

苏,问题是我笨。我把这个脚本放在遥控器上,并在开始时禁用遥控器,这就是为什么它没有检测到我的按键。我将脚本放在我的播放器对象上,现在可以正常工作了。