unity 圍繞物體生成多層圓

private Vector3 centerPos;    //你圍繞那個點 就用誰的角度
    private float radius = 3;     //物理離 centerPos的距離
    private float angle = 0;      //偏移角度  
     
    void Start()
    {
        CreateMosquitoCoil();
    }
 
    public void CreateCubeAngle30()
    {
        centerPos = transform.position;
         //20度生成一個圓
        for (angle = 0; angle < 360; angle += 20)
        {
            //先解決你物體的位置的問題
            // x = 原點x + 半徑 * 鄰邊除以斜邊的比例,   鄰邊除以斜邊的比例 = cos(弧度) , 弧度 = 角度 *3.14f / 180f;   
            float x = centerPos.x + radius * Mathf.Cos(angle * 3.14f / 180f);
            float y = centerPos.y + radius * Mathf.Sin(angle * 3.14f / 180f);
            // 生成一個圓
            GameObject obj1 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            //設置物體的位置Vector3三個參數分別代表x,y,z的座標數  
            obj1.transform.position = new Vector3(x, centerPos.z,y);
        }
    }
 
    // 生成螺旋   //原理 += 半徑
    public void CreateMosquitoCoil()
    {
        centerPos = transform.position;
        // 每隔30度就生成一個小方塊  
        for (int i = 0; i < 120; angle += 18, radius += 0.2f, i++)
        {
            // 根據原點,角度,半徑獲取物體的位置.  x = 原點x + 半徑 * 鄰邊除以斜邊的比例  
            float x = centerPos.x + radius * Mathf.Cos(angle * 3.14f / 180f);
            float y = centerPos.y + radius * Mathf.Sin(angle * 3.14f / 180f);  
            //我們將obj1初始化爲一個Cube立方體,當然我們也可以初始化爲其他的形狀  
            GameObject obj1 = GameObject.CreatePrimitive(PrimitiveType.Cube);
            //設置物體的位置Vector3三個參數分別代表x,y,z的座標數  
            obj1.transform.position = new Vector3(x, centerPos.z,y);
        }
    }

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章