unity 在指定范围内随机生成怪物
using UnityEngine; public class test2 : MonoBehaviour { public float RandomPos; public GameObject MonsterPrefab; private void OnMouseDown() { float posx = transform.position.x + Random.Range(-RandomPos + 3, RandomPos - 3);//获取随机X轴坐标 float posz = transform.position.z + Random.Range(-RandomPos + 3, RandomPos - 3);//获取随机Z轴坐标 Vector3 randomNewPos = new Vector3(posx, transform.position.y, posz);//组合成新的三维坐标 Instantiate(MonsterPrefab, randomNewPos, Quaternion.identity);//生成怪物 } void OnDrawGizmosSelected() { //设置范围颜色 Gizmos.color = Color.white; //画出刷怪范围 Gizmos.DrawWireSphere(transform.position, RandomPos); } }
时间若流水,恍惚间逝去