unity中检测不同按钮连续被按下
在Unity中,可以使用以下方法来检测不同按钮的连续按下:
-
在脚本中定义一个变量来存储上一次按下的按钮:
private KeyCode lastPressedButton; -
在Update函数中使用Input.GetKeyDown()函数来检测当前按下的按钮:
void Update() { if (Input.GetKeyDown(KeyCode.A)) { // 当按下A按钮时的逻辑 lastPressedButton = KeyCode.A; } else if (Input.GetKeyDown(KeyCode.B)) { // 当按下B按钮时的逻辑 lastPressedButton = KeyCode.B; } } -
在Update函数中使用Input.GetKey()函数来检测连续按下的按钮:
void Update() { if (Input.GetKey(lastPressedButton)) { // 当上一次按下的按钮仍然被按下时的逻辑 } }
通过以上方法,你可以在Unity中检测不同按钮的连续按下,并根据需要执行相应的逻辑
原文地址: http://www.cveoy.top/t/topic/h5qd 著作权归作者所有。请勿转载和采集!