1、u3d16用Cube摆一个圆并让其转动(附赠椭圆做法) 代码清单如下:(圆---父子结构) 下面的代码是采用父子结构的算法比较简单,一般我们不采用! using UnityEngine; using System.Collections; public class CirCleTest : MonoBehaviour { private float r=5;//定义一个半径 private float angle;//定义一个角度变量 private float x0=0.0f;//定义初始的圆心的位置 private float y0=0.0f; pri
2、vate float z0=0.0f; private ArrayList arr;//定义一个数组链表 private float flo;//定义一个变量 private GameObject zhuan; // Use this for initialization void Start () { //初始化我们定义的变量 flo=0; angle=0; float hudu = 0; zhuan = new GameObject(); for(int i=0;i<6;i++){//循环我们创建的Cube确定他的xx yy 的位置
3、 float xx = x0+r*Mathf.Cos(hudu); //float yy = y0+r*Mathf.Sin(hudu);//确定其yy的位置我们做出的圆是树立的 float zz = z0+r*Mathf.Sin(hudu);//确定其zz的位置我们做出的圆是平铺的 GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); //cube.transform.rotation = Quaternion.Euler(0.0f,-angle,0.0f);//改变了cube的角度
4、 //cube.transform.localScale = new Vector3(1.0f,1.0f,1.0f);//改变其缩放比例 //上面的yy 或者是zz改变这里也别忘了改变 cube.transform.position = new Vector3(xx,0.0f,zz); cube.transform.parent = zhuan.transform;//设置cube的父亲是zhuan //arr.Add(cube); angle +=60; //角度一次增加60度 hudu = angle*Mathf.PI/180;//求其弧度
5、 } } // Update is called once per frame void Update () { flo++; zhuan.transform.rotation=Quaternion.Euler(0.0f,flo,0.0f); //让我们创建的这个圆不停地围绕圆心旋转 } } 效果图: 把两行蓝色代码打开就是下面的效果当然for循环应该小于60; angle 每次加6 下面的代码是采用精确的算法应用比较广,一般我们使用下面的方法: 上面的代码进行了详细的解释这下面的代码应该会很明白不用多说什么效果图和上
6、面的一样! 代码清单如下:(圆--算法) using UnityEngine; using System.Collections; public class CirCleTest : MonoBehaviour { private float r=5; private float angle; private float x0=0.0f; private float y0=0.0f; private float z0=0.0f; private ArrayList arr; // Use this for initialization void S
7、tart () { angle=0; float hudu = 0; arr = new ArrayList(); for(int i=0;i<6;i++){ float xx = x0+r*Mathf.Cos(hudu); float zz = z0+r*Mathf.Sin(hudu); GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); cube.transform.position = new Vector3(xx,0.0f,zz); arr.A
8、dd(cube); angle +=60; hudu = angle*Mathf.PI/180; } angle=0; } // Update is called once per frame void Update () { float tmpAngle = angle++; for(int i=0;i<6;i++){ float hudu = tmpAngle*Mathf.PI/180; float xx = x0+r*Mathf.Cos(hudu); float zz = z0+r*Mathf
9、Sin(hudu); GameObject gobj = (GameObject)arr[i]; gobj.transform.position = new Vector3(xx,0.0f,zz); tmpAngle += 60; } } } 下面我们要做的是通过算法做出椭圆的效果,上面的代码进行了详细的解释这下面的代码理解同样是没有问题的 代码清单如下:(椭圆--算法) using UnityEngine; using System.Collections; public class DrawTuoYuan : MonoBehaviour {
10、 private float x0; private float y0; private float z0; private float RR; private float rr; // Use this for initialization void Start () { x0 = 0; y0 = 0; z0 = 0; rr = 3; RR = 5; GameObject cube = GameObject.Find("Cube"); float angle=0; for(int i=0;i<72;i++){
11、 GameObject cube1 = (GameObject)Instantiate(cube); float hudu = (angle*Mathf.PI)/180; float xx = x0 + RR*Mathf.Cos(hudu); //float yy = y0 + rr*Mathf.Sin(hudu); float zz = z0 + rr*Mathf.Sin(hudu); cube1.transform.position = new Vector3(xx,0,zz); cube1.transform.rotation=Quaternion.Euler(0,-angle,0); angle += 5; } } // Update is called once per frame void Update () { } }






