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);
    }
}

posted @ 2022-12-02 19:31  伊凡晴天  阅读(2116)  评论(0编辑  收藏  举报