Unity 随机位置 随机时间生成敌人


1.随机位置 随机时间生成敌人

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using UnityEngine;
/// <summary>
/// 设计随机事件
/// </summary>
public class CreatWolfs : MonoBehaviour {
    float CreatTime = 5f; //设计创造狼的时间
     GameObject Wolfs; //申请一个狼的模块

    void Update () {
        CreatTime -= Time.deltaTime;    //开始倒计时
        if (CreatTime<=0)    //如果倒计时为0 的时候
        {
            CreatTime = Random.Range(3, 10);     //随机3到9秒内
            GameObject obj = (GameObject)Resources.Load("Prefabs/WolfNormal");    //加载预制体到内存
            Wolfs = Instantiate<GameObject>(obj);    //实例化敌人
            Wolfs.transform.position = new Vector3(Random.Range(408f, 77f),21f,Random.Range(87f,397f));    //随机生成狼的位置
        }

    }
}

2.自定义数组 随机生成

1
2
3
4
5
6
7
8
9
public void ChioceBtnEvent()
    {
        string[] NameArray = new string[] {"001婷婷", "002贺堂", "003   莹","004润生","005满想","006   鑫",
            "007丽叶","008梦怡","009燕杰","010小华","011维豪","012朋",
            "013梦凡","014银利","015玉娣","016剑锋","017   龙","018   宁",
           };
        int a = Random.Range(0,18);
        tips.text = "结果是:"+"\n"+NameArray[a].ToString();
        }