Unity3d 物体沿着正七边形轨迹移动

不对之处,敬请谅解。

1.圆内接正七边形半径

public static float r = 10;

2.存储七个顶点的值
Vector3[] ar = new Vector3[7];

3.圆心角
static float circleAngle = 360 / 7*Mathf.PI/180;

static float ca3 =2 * circleAngle-90*Mathf.PI/180;//第三个顶点的夹角

图示:

Unity3d  物体沿着正七边形轨迹移动

4.计算顶点坐标

static float a2y =Mathf.Cos(circleAngle) * r;
static float a2x = Mathf.Sin(circleAngle) * r;

static float a3x = Mathf.Cos(ca3) * r;
static float a3y = Mathf.Sin(ca3) * r;

static float a4x = Mathf.Sin(circleAngle / 2) * r;
static float a4y = Mathf.Cos(circleAngle / 2) * r;

5.对顶点赋值

Vector3 a1 = new Vector3(0, r, 0);
Vector3 a2 = new Vector3(a2x, a2y, 0);
Vector3 a3 = new Vector3(a3x, -a3y, 0);
Vector3 a4 = new Vector3(a4x, -a4y, 0);
Vector3 a5 = new Vector3(-a4x, -a4y, 0);
Vector3 a6 = new Vector3(-a3x, -a3y, 0);
Vector3 a7 = new Vector3(-a2x, a2y, 0);

7.初始化顶点
void Start()
{

ar[0] = a1;
ar[1] = a2;
ar[2] = a3;
ar[3] = a4;
ar[4] = a5;
ar[5] = a6;
ar[6] = a7;
}
8.更新物体位置
void Update()
{

if (i<7)//当循环一圈后置为零
{
this.transform.Translate(ar[i].x, ar[i].y, 0);
}
else
{
i = 0;
}

}

上一篇:教你一招:win 7 或win 10右键菜单 添加获取管理员权限


下一篇:execlp函数使用