随机生成敌人的方法

2D游戏下的代码

 public void RandomInstantiateEnemy0(gameobject enemy0)
    {
        float x = Random.Range(-2.57f, 2.57f);
        GameObject.Instantiate(enemy0, new Vector3(x,transform.position.y,0),Quaternion.identity);
        
    }

 

3D游戏下的代码

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class TrollSpawn : MonoBehaviour {
 5     public GameObject trollSpawn;
 6     public int maxCount = 10;
 7     public float spawnTimer = 10f;
 8     private float resetSpawnTimer;
 9     private int trollCount=0;
10     private bool isMaxCount = false;
11     // Use this for initialization
12     void Start()
13     {
14         resetSpawnTimer = spawnTimer;
15 
16 
17     }
18 
19     // Update is called once per frame
20     void Update()
21     {
22         spawnTimer -= Time.deltaTime;
23         print(spawnTimer);
24        
25         if (!isMaxCount)
26         {
27             if (spawnTimer<=0)
28             {
29                 spawnTimer = resetSpawnTimer;
30                 print("chuxian");
31                 GameObject.Instantiate(trollSpawn, this.transform.position, Quaternion.identity);
32                 trollCount++;
33 
34                 print(trollCount);
35 
36                  
37             }
38             
39           
40         }
41         if (trollCount>=maxCount)
42         {
43             isMaxCount = true;
44         }
45         
46     }
47 }

 

  

posted @ 2016-03-31 21:42  礼桀  阅读(1909)  评论(0编辑  收藏  举报