[Unity]围绕一点球形生成物体,空心球体

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

public class Test_sphere : MonoBehaviour{
    private Vector3 centerPos;    //你围绕那个点 就用谁的角度
    private float radius = 3;     //物理离 centerPos的距离
    private float angle = 0;      //偏移角度  

    void Start()
    {
        CreateSphere();
    }


    public void CreateSphere()
    {
        centerPos = transform.position;
        //20度生成一个圆
        for (int i=0; i<1000 ;i++)
        {
            Vector3 p = Random.insideUnitSphere * radius;
            Vector3 pos = p.normalized * (2 + p.magnitude);

            GameObject obj1 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            //设置物体的位置Vector3三个参数分别代表x,y,z的坐标数  
            obj1.transform.position = pos;
        }
    }
}

运行代码,发现 围绕该 物体 的坐标,生成一个空心球体。

Random.insideUnitSphere

Returns a random point inside a sphere with radius 1 (Read Only).返回一个 围绕一个点 的 单位半径的球体的 随机 坐标。

参考文章:

1.Unity 围绕物体 围成一个圆 unity生成圈圈

2.

相关文章:

1.[Unity]空心圆范围内随机生成物品

2.

猜你喜欢

转载自blog.csdn.net/BuladeMian/article/details/81112135