[英]pickup and drop with space unity
我試圖統一使用相同的鍵“空格”來拾取和放下角色,但是由於空更新發生得如此之快,所以當我放下一個怪物時,它會立即將其拾取。
void Update() {
if (Input.GetKeyDown("space") /*( || Input.GetMouseButtonDown(1))*/) {
if (map.selectedUnit.GetComponent<Unit>().carrying) {
print("DROP space");
map.selectedUnit.GetComponent<Unit>().carrying = false;
}
// pick up monster while on top of it
else {
for(int i = 0; i < map.monsterList.Count; i++) {
if (map.monsterList[i].GetComponent<Monsters>().tileX == map.selectedUnit.GetComponent<Unit>().tileX &&
map.monsterList[i].GetComponent<Monsters>().tileY == map.selectedUnit.GetComponent<Unit>().tileY &&
map.occupationArray [map.monsterList[i].GetComponent<Monsters>().tileX, map.monsterList[i].GetComponent<Monsters>().tileY] == true) {
print("PICKUP space");
map.selectedUnit.GetComponent<Unit>().carrying = true;
}
}
}
}
}
有沒有一種方法可以解決此問題,或者我必須將提取鑰匙作為其他鑰匙。 我一直在思考是否在每個“空格”鍵之間添加1秒。 因此,您只能在一秒鍾內按一下空格鍵,並且可以以某種方式解決此問題。
如何使用GetKeyUp代替。
釋放密鑰時,只會調用一次GetKeyUp。
請參閱此文檔https://docs.unity3d.com/ScriptReference/Input.GetKeyUp.html
我最終在代碼中使用了seconds方法。 這意味着您需要等待0.25秒才能再次上落。 現在,這可以防止它一直循環播放。 如果其他人有更好的解決方案,請提供。 現在,我正在使用它。
void Update() {
// there is 0.25 second between that player can PickUp again or Drop again
if (!map.selectedUnit.GetComponent<Unit>().carrying) {
timeLeftToPressAgain -= Time.deltaTime;
timeLeftToPressAgain = Mathf.Max(timeLeftToPressAgain, -0.25f);
}
else {
timeLeftToPressAgain += Time.deltaTime;
timeLeftToPressAgain = Mathf.Min(timeLeftToPressAgain, 0.25f);
}
if (Input.GetKeyDown("space") /*( || Input.GetMouseButtonDown(1))*/) {
// while carrying the time builds up for player to Drop and vice versa in PickUp
if (map.selectedUnit.GetComponent<Unit>().carrying && timeLeftToPressAgain > 0) {
map.selectedUnit.GetComponent<Unit>().carrying = false;
timeLeftToPressAgain = 0.25f;
}
// pick up monster while on top of it
else if (!map.selectedUnit.GetComponent<Unit>().carrying && timeLeftToPressAgain < 0) {
for(int i = 0; i < map.monsterList.Count; i++) {
if (map.monsterList[i].GetComponent<Monsters>().tileX == map.selectedUnit.GetComponent<Unit>().tileX &&
map.monsterList[i].GetComponent<Monsters>().tileY == map.selectedUnit.GetComponent<Unit>().tileY &&
map.occupationArray [map.monsterList[i].GetComponent<Monsters>().tileX, map.monsterList[i].GetComponent<Monsters>().tileY] == true) {
map.selectedUnit.GetComponent<Unit>().carrying = true;
timeLeftToPressAgain = -0.25f;
}
}
}
}
}
只是為了完成它:您可以做這樣的事情:
bool pressedSpace = false;
void Update() {
pressedSpace = true; // Only called once
if (!pressedSpace && Input.GetKeyDown("space") /*( || Input.GetMouseButtonDown(1))*/) {
if (map.selectedUnit.GetComponent<Unit>().carrying) {
print("DROP space");
map.selectedUnit.GetComponent<Unit>().carrying = false;
}
// pick up monster while on top of it
else {
for(int i = 0; i < map.monsterList.Count; i++) {
if (map.monsterList[i].GetComponent<Monsters>().tileX == map.selectedUnit.GetComponent<Unit>().tileX &&
map.monsterList[i].GetComponent<Monsters>().tileY == map.selectedUnit.GetComponent<Unit>().tileY &&
map.occupationArray [map.monsterList[i].GetComponent<Monsters>().tileX, map.monsterList[i].GetComponent<Monsters>().tileY] == true) {
print("PICKUP space");
map.selectedUnit.GetComponent<Unit>().carrying = true;
}
}
}
}
}
然后稍等片刻,您可以將pressedSpace重置為false。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.