我在RPG上工作,並開始研究能力冷卻時間。我有一個由用戶(player1)控制的播放器和一臺由電腦(player2)控制的播放器用於測試目的。技能的冷卻時間對於用戶控制的玩家來說是完美的,但是當談到人工智能時,除非電腦先行(當能力默認關閉時冷卻時),否則電腦將不會執行該技能。以下是我認爲與問題相關的代碼,但如果需要更多信息,我會更新該帖子。C#RPG - 技能冷卻問題
的能力類:
using UnityEngine;
public class attackList : MonoBehaviour{
public int dmg;
public float coolDown;
public float _attackTimer;
public void Attack1(basePlayer user, basePlayer target, int D)
{
coolDown = 2.5f;
if (user._attackTimer == 0)
{
dmg = D;
user._attackTimer = coolDown;
target.curHealth -= dmg;
}
}
public void cooldown(basePlayer user)
{
if (user._attackTimer > 0) {
user._attackTimer -= Time.deltaTime;
if (user._attackTimer < 0) {
user._attackTimer = 0;
}
}
}
}
攻擊邏輯類:
public class attackLogic : MonoBehaviour {
private attackList _attackList = new attackList();
private playerList _playerList = new playerList();
public bool player1_turn = false; //player1 is not allowed to go first by default
public bool player2_turn = false; //player2 is not allowed to go first by default
void Start()
{
int rnd = Random.Range (1,3); // random value between 1 and 2 generated, which will determine what player goes first
if (rnd == 1)
{
player1_turn = true;
}
if (rnd == 2)
{
player2_turn = true;
}
//current health set to the maximum health at the beginning of each fight
_playerList.player1.curHealth = _playerList.player1.maxHealth;
_playerList.player2.curHealth = _playerList.player2.maxHealth;
}
void Update()
{
Logic(); //run through the logic of the battle
}
public void Logic()
{
if (player1_turn == true)
{
_attackList.cooldown(_playerList.player1);
if (Input.GetKeyUp("1"))
{
_attackList.Attack1(_playerList.player1, _playerList.player2, 5);
player1_turn = false;
player2_turn = true;
}
}
if (player2_turn == true)
{
_attackList.cooldown(_playerList.player2);
_attackList.Attack1(_playerList.player2, _playerList.player1, 5);
player1_turn = true;
player2_turn = false;
}
}
}
我不知道這個問題是否可能與你設置「if」塊的方式有關。也就是說,在第一個模塊中,您設置了'player2_turn = true',然後在第二個模塊中,您立即檢查'player2_turn == true',這樣您最終會在同一次更新中評估兩個玩家的輪流。 – Abion47