Unity实现方圆X范围随机生成怪物

Unity实现方圆X范围随机生成怪物_第1张图片

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CreatMonster : MonoBehaviour
{
    // S这个脚本间隔一点时间生成怪物
    /*1.程序逻辑
     * 1. 设计一个计时器
     * 2.间隔一段时间3s执行一下     * 
     */
    float SaveTime = 0f;
   public GameObject OneMonsterPre;
    public Transform MonsterCenterPOS;
    float CloneX;
    float CloneZ;

    // Update is called once per frame
    void Update()
    {
        SaveTime += Time.deltaTime;
        if (SaveTime>=3)
        {
            Debug.Log("时间超过3秒");
            SaveTime = 0;
            CloneMonster();
        }
    }//endupdate

    void CloneMonster()
    {
        Debug.Log("生成随机数");
        for (int i = 0; i < 10; i++)
        {
            CloneX = Random.Range(1f, 100f) + MonsterCenterPOS.position.x;
            CloneZ = Random.Range(1f, 300f) + MonsterCenterPOS.position.z;

            Vector3 TempPos = new Vector3(CloneX, MonsterCenterPOS.position.y, CloneZ);
            Debug.Log("随机坐标" + TempPos);
          GameObject TempOBJ=  GameObject.Instantiate(OneMonsterPre, TempPos, Quaternion.Euler(new Vector3(Random.Range(10, 80), 0, Random.Range(10, 80))));
            Destroy(TempOBJ,3f);
        }  
    }
}//endclass

 

你可能感兴趣的:(自学网站开发,unity,游戏引擎)