Unity3D【火星大战四】

利用协程使敌机和圆盘重复出现

  1. using UnityEngine;
  2. using System.Collections;
  3. public class RandomSp : MonoBehaviour {
  4.     public GameObject[] enemy2;//圆盘
  5.     public GameObject[] enemy1;//敌机 
  6.     public GameObject player;//我机
  7.     void Start()
  8.     {
  9.         player = GameObject.FindWithTag("Player");//获取我机的名称
  10.         NextTime();
  11.         StartCoroutine(RandomSpNext());   //开始协程
  12.     }
  13.     IEnumerator RandomSpNext() {
  14.         yield return new WaitForSeconds(10f);
  15.         while(true){
  16.             if(player==null){//当我机销毁时,敌机圆盘不在出现
  17.                 break;
  18.             }
  19.             NextTime();
  20.             yield return new WaitForSeconds(10f);//停10秒
  21.         }
  22.     }
  23.     void NextTime() {//产生第一波
  24.         EnemyRandomSp();//随机产生三个圆盘
  25.         StartCoroutine(Enemy1());//停3秒后随机产生三个敌机
  26.     }
  27.     void EnemyRandomSp() {//圆盘
  28.         for (; ; )
  29.         {
  30.             int index = Random.Range(0, enemy2.Length);//随机产生圆盘
  31.             float posX = Random.Range(-7f, 6f);//x轴随机产生
  32.             int index2 = Random.Range(0, enemy2.Length);
  33.             float posX2 = Random.Range(-7f, 6f);
  34.             int index3 = Random.Range(0, enemy2.Length);
  35.             float posX3 = Random.Range(-7f, 6f);
  36.             if (Mathf.Abs(posX - posX2) > 3f && Mathf.Abs(posX - posX3) > 3f && Mathf.Abs(posX3 - posX2) > 3f)
  37.             {//位置不重叠时克隆圆盘
  38.                 Instantiate(enemy2[index], new Vector3(posX, 1.2f, -8f), Quaternion.identity);
  39.                 Instantiate(enemy2[index], new Vector3(posX2, 1.2f, -8f), Quaternion.identity);
  40.                 Instantiate(enemy2[index], new Vector3(posX3, 1.2f, -8f), Quaternion.identity);            
  41.                 break;
  42.             }           
  43.         }
  44.     }
  45.     IEnumerator Enemy1() {//敌机
  46.         yield return new WaitForSeconds(3f);
  47.         for (; ; )
  48.         {
  49.             int index = Random.Range(0, enemy1.Length);//随机产生敌机
  50.             float posX = Random.Range(-7f, 6f);//x轴随机产生
  51.             int index2 = Random.Range(0, enemy1.Length);
  52.             float posX2 = Random.Range(-7f, 6f);
  53.             int index3 = Random.Range(0, enemy1.Length);
  54.             float posX3 = Random.Range(-7f, 6f);
  55.             if (Mathf.Abs(posX - posX2) > 3f && Mathf.Abs(posX - posX3) > 3f && Mathf.Abs(posX3 - posX2) > 3f)
  56.             {//位置不重叠时克隆敌机
  57.                 Instantiate(enemy1[index], new Vector3(posX, 1.2f, -8f), Quaternion.identity);
  58.                 Instantiate(enemy1[index], new Vector3(posX2, 1.2f, -8f), Quaternion.identity);
  59.                 Instantiate(enemy1[index], new Vector3(posX3, 1.2f, -8f), Quaternion.identity);              
  60.                 break;
  61.             }          
  62.         }       
  63.     }
  64. }

更多精彩请点击 http://www.gopedu.com/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值