1. 程式人生 > >Unity-實現在防禦塔在範圍內只攻擊先進來的

Unity-實現在防禦塔在範圍內只攻擊先進來的

public Queue enemyQue;
GameObject take_aim;
float second;
// Use this for initialization
void Start () {
    enemyQue = new Queue();
}

// Update is called once per frame
void Update () {
    second += Time.deltaTime;
    if (enemyQue.Count != 0)
    {
        take_aim = enemyQue.Peek() as GameObject;
        //transform.LookAt
(take_aim.transform.position, Vector3.up); transform.forward = take_aim.transform.position - transform.position; if (second > CreateTime) { take_aim.GetComponent<Move_enemy>().Hp -= Attack; second -= CreateTime; } if (take_aim.GetComponent
<Move_enemy>().Hp <= 0) { Destroy(take_aim); enemyQue.Dequeue(); } } } void OnTriggerEnter(Collider enemy) { if (enemy.tag == "Enemy") { enemyQue.Enqueue(enemy.gameObject); //Debug.Log(enemyQue.Count + "入隊"); } } void OnTriggerExit(Collider enemy) { if (enemyQue.Count
!= 0) { enemyQue.Dequeue(); //Debug.Log(enemyQue.Count + "結束"); } }