Unity 利用 LineRenderer 画圆

  • 源码:
    private LineRenderer line;
    private int r = 10;
    private int n = 360;
    
    void Start () {
        line = this.GetComponent ();

        for (int i = 0; i < n; i++) {
            //划线的话2D坐标就行了,这里我们计算x和z坐标轴上的坐标,而y永远是0
            //计算x和z的长度,乘以半径r来得到最终长度
            float x = Mathf.Cos ((360 * (i + 1) / n) * Mathf.Deg2Rad) * r;
            float z = Mathf.Sin ((360 * (i + 1) / n) * Mathf.Deg2Rad) * r;
            //设置坐标画线
            line.SetPosition (i, new Vector3 (x, 0, z));
        }
        
    }

除此之外还有个更简单的方法

private LineRenderer line;
private int n = 360;
private int r = 10;

void Start () {
    line = this.GetComponent ();

    for (i = 0; i < n; i++) {
    	//旋转一度画一个点,是不是很简单呢
        this.transform.Rotate (0, 1, 0);
        line.SetPosition (i, this.transform.forward * r);
    }
    
}

你可能感兴趣的:(Unity)