【问题标题】:unity turn base script for enemy统一转向敌人的基础脚本
【发布时间】:2016-05-27 04:30:32
【问题描述】:

我有一个敌人列表。所以我希望每个敌人都轮到他们。 首先 : 玩家回合 --> 敌人回合(“这里每个敌人一个接一个地移动直到结束,然后玩家再次移动”)。我如何在这里等待一些时间并强制敌人转弯? 任何帮助将不胜感激。

void Start()
{
     // find list enemy
    enemy = GameObject.FindGameObjectsWithTag("Enemy");

}
void Update()
{
    //enemy turn reference to player. after move all enemy we change it to false to change the player turn.
    if(StaticClass.enemyTurn == true )
    {
       for(int i=0;i<enemy.length;i++)
        {
           // how do i making some waiting time here and forcus on enemy turn?
           EnemyTurn(i);
        }
    }
}


 public void EnemyTurn(int id)
{
    ChessMoveMent chessMoveScript = enemy[id].GetComponent<ChessMoveMent>();
    chessMoveScript.ProcessMove();
    id++;
    if(id>=enemy.Length)
    {
        isMove = false;
    }
}

【问题讨论】:

    标签: c# unity3d unityscript yield-return


    【解决方案1】:

    在这种情况下,我通常使用 StartCoroutine。 请尝试以下代码:

    public IEnumerator EnemyTurn(int id)
    {
      yield return null;
      ChessMoveMent chessMoveScript = enemy[id].GetComponent<ChessMoveMent>();
      chessMoveScript.ProcessMove();
      id++;
      if(id>=enemy.Length)
      {
        isMove = false;
      }
    }
    

    要使用时,请配合“StartCoroutine()”使用

    StartCoroutine(EnemyTurn(i));
    

    更多详情here

    【讨论】:

      【解决方案2】:

      您可能有一个协调员,他会在轮到参与者时告诉他们。

      public class GameCoordinator : MonoBehaviour
      {
          public List<Participant> participants;
          private int currentParticipantIdx = -1;
      
          private Participant CurrentParticipant
          {
              get { return participants[currentParticipantIdx]; }
          }
      
          private void Start()
          {
              PlayNextMove();
          }
      
          private void PlayNextMove()
          {
              ProceedToNextParticipant();
      
              CurrentParticipant.OnMoveCompleted += OnMoveCompleted;
              CurrentParticipant.BeginMove();
          }
      
          private void OnMoveCompleted()
          {
              CurrentParticipant.OnMoveCompleted -= OnMoveCompleted;
              StartCoroutine(PlayNextMoveIn(2.0f));
          }
      
          private IEnumerator PlayNextMoveIn(float countdown)
          {
              yield return new WaitForSeconds(countdown);
              PlayNextMove();
          }
      
          private void ProceedToNextParticipant()
          {
              ++currentParticipantIdx;
              if (currentParticipantIdx == participants.Count)
                  currentParticipantIdx = 0;
          }
      }
      
      public class Participant : MonoBehaviour
      {
          public event Action OnMoveCompleted;
      
          public void BeginMove()
          {
              //
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2023-02-09
        • 2023-03-22
        • 2022-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-21
        • 2022-01-23
        相关资源
        最近更新 更多