团结转向敌人的基础脚本

时间:2016-02-16 03:47:04

标签: c# unity3d unityscript yield-return

我有一份敌人名单。所以我希望每个敌人轮到他们。 首先 : 玩家转身 - >敌人转身("这里每个敌人一个接一个地移动直到结束然后玩家再次移动")。我如何在这里等待时间和敌人的转弯? 任何帮助将不胜感激。

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;
    }
}

2 个答案:

答案 0 :(得分:0)

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

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

如果您想使用它,请使用&#34; StartCoroutine()&#34;

StartCoroutine(EnemyTurn(i));

更多详情here

答案 1 :(得分:0)

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

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()
    {
        //
    }
}