unity2D打字游戏 键盘事件

unity里,我定义了一个char类型,生成了随机26个字母,然后想用键盘事件,按下这些随机字母,则摧毁主体,但是Input。GetKeyDown()括号里不能直接写char,会报错:cannot convert from 'char' to 'UnityEngine.KeyCode'。应该怎么转换呢?2D的打字游戏。
public class people : MonoBehaviour

{
GameObject peopleGam;
public Text peopleText;
char a;

void Start()
{
    peopleGam = this.gameObject;
    char ch=(char)('A'+Mathf.RoundToInt(Random.Range(0,26)));  //产生随机数 转换为char类型
    peopleText.text = ch.ToString();
    if (Input.GetKeyDown(a))
     {
        Destroy(peopleGam);
    }
}

首先Random.Range是有int类型参数的重载的,不需要Mathf.RoundToInt去强转。
其次KeyCode枚举中的A-Z键是从97-122,你需要用随机得到的数值+97,然后转为KeyCode。

GameObject peopleGam;
public Text peopleText;
void Start()
{
    peopleGam = this.gameObject;
    int random = Random.Range(0, 26);
    char ch = (char)('A' + random);
    peopleText.text = ch.ToString();
    int targetKey = random + 97;
    if (Input.GetKeyDown((KeyCode)targetKey))
    {
        Destroy(peopleGam);
    }
}

这块监控键盘输入,应该写在Update函数里面

img

试试强制转换呢
(UnityEngine.KeyCode)a