unity3d 随机范围内生成怪物

来源:互联网 发布:阿里云注销 编辑:程序博客网 时间:2024/05/01 12:00
using UnityEngine;using System.Collections;public class BarrackManager : MonoBehaviour {public Transform[] address;public GameObject[] prefabs;// Use this for initializationvoid Start () {InvokeRepeating ("setBarrack",1,3f);}// Update is called once per framevoid Update () {}void setBarrack(){Transform tf = address [Random.Range (0, address.Length)];Bound bound = getBound (tf);Vector3 pos = new Vector3 (bound.getRandomX(),bound.y,bound.getRandomZ());Instantiate (prefabs[0],pos,Quaternion.identity);}Bound getBound(Transform tf){Vector3 center = tf.collider.bounds.center;Vector3 extents = tf.collider.bounds.extents;Vector3 dL = new Vector3 (center.x - extents.x,center.y,center.z - extents.z);Vector3 dR = new Vector3 (center.x + extents.x,center.y,center.z - extents.z);Vector3 sR = new Vector3 (center.x + extents.x,center.y,center.z + extents.z);Vector3 sL = new Vector3 (center.x - extents.x,center.y,center.z + extents.z);Bound bound = new Bound (dL,dR,sR,sL,center.y);return bound;}class Bound{public Vector3 dL;public Vector3 dR;public Vector3 sR;public Vector3 sL;public float y;public Bound(Vector3 dL,Vector3 dR, Vector3 sR, Vector3 sL,float y){this.dL = dL;this.dR = dR;this.sR = sR;this.sL = sL;this.y = y;}public float getRandomX(){float num = Random.Range (dL.x,dR.x);return num;}public float getRandomZ(){float num = Random.Range (dL.z,sL.z);return num;}}}

0 0
原创粉丝点击