我正在为两个玩家制作游戏。他们都有一个按钮,可以按下该按钮为电表充电,然后根据他们为电表充电的时间松开以执行操作。
我使用左移和右移作为两个播放器按钮。
问题是,如果它们同时充电,当其中一个释放按钮时,该Input.GetButtonUp
事件直到半秒后才会触发。如果只有一名玩家在冲锋,则在他释放时立即触发该事件。
任何想法为什么以及如何解决它?它发生在两台不同的PC上。
编辑:一些代码
using UnityEngine;
using System.Collections;
public class ObjectScript : MonoBehaviour {
void Update () {
for (int i = 0; i < 2; i++) {
if (isButtonDown(i)) // button first pressed
buttonDown(i);
if (isButtonUp(i)) // button released
buttonUp(i);
if (isButtonPressed(i)) // while button held pressed
buttonIsPressed(i);
}
}
// checks whether button has just been pressed down for given player
private bool isButtonDown(int player) {
return Input.GetButtonDown(getPlayerKeyName(player));
}
// checks whether button is held pressed down for given player
private bool isButtonPressed(int player) {
return Input.GetButton(getPlayerKeyName(player));
}
// checks whether has just been released for given player
private bool isButtonUp(int player) {
return Input.GetButtonUp(getPlayerKeyName(player));
}
// get the name of the key per player
private string getPlayerKeyName(int player) {
return "Player" + (player+1).ToString() + "Key";
}
// when button is first pressed
private void buttonDown(int player) {
Debug.Log("Button pressed: " + player.ToString());
}
// while button is pressed
private void buttonIsPressed(int player) {
}
// when button is first released
private void buttonUp(int player) {
Debug.Log("Button released: " + player.ToString());
}
}