我正在制作RPG,并开始研究能力冷却时间。我有一个由用户(player1)控制的玩家,以及由计算机(player2)控制的玩家用于测试目的。能力的冷却时间对于用户控制的玩家来说是完美的,但是当涉及到AI时,计算机将不会执行该能力,除非计算机先行(当默认情况下该能力为冷却时)。以下是我认为与问题相关的代码,但如果需要更多信息,我将更新帖子。
能力等级:
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;
}
}
}
答案 0 :(得分:1)
1)我怀疑实际问题是在Time.deltaTime中,但你没有显示代码,所以我无法证实这一点。
2)由于您在这里有基于时间的代码,因此当您单步执行代码时,不能指望它的行为相同。因此,它通常有助于消除一些旧技术 - 即打印报表。当然,多年来事情已经发生了很大变化,以至于它已成为写作,但这些想法仍然有效。选择与您正在测试的内容不重要的屏幕的一部分,并编写与您正在执行的操作有关的变量。我想你会发现球员2的速度非常缓慢。
3)我认为你需要更好地理解对象。您似乎只是将它们用作容器。我也不喜欢player1和player2 - 你应该只有一个玩家列表。还记得WOPR在战争游戏中是如何被击败的 - 这在平衡事物时非常有用。