我提供LineRenderer在2D中绘制Room的形状,并将LineRenderer位置值存储在DrawLine.storeLineRenderers列表中。(List2>)
我计划使用2D Shape创建3D房间,即LineRenderer&#39。
以下代码转换为3d,但不准确
for(int i=1;i<=noOfLines;i++)
{
LineRenderer line =GameObject.Find("line" + i).GetComponent<LineRenderer>();
if(line!=null)
{
Vector3[] ss= DrawLine.storeLineRenderers[i - 1];
print(ss[0]); //Position Element 0 value (Vector3)
print(ss[1]); //Position Element 1 value (Vector3)
//generate 3d box (wall) here
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.name = "wall" + i;
cube.transform.localPosition =new Vector3(ss[0].x,ss[0].y,ss[0].z);
cube.transform.localScale = new Vector3(ss[1].x - ss[0].x, ss[1].y - ss[0].y,ss[1].z - ss[0].z);
}
}
输出
如何解决这个问题?有没有正确创建的公式??
提前感谢...
答案 0 :(得分:0)
试一试。 您需要注意的是,立方体的枢轴位于其中心,这意味着您必须将其大小的一半添加到其位置,以使其在ss [0]处“开始”并一直跨越到ss [1]。
此外,墙需要旋转以查看ss [1]。
const float thickness = .1f; // how thick will the wall be
const float height = 1f; // how high will the wall be
Vector3 dist = ss[1] - ss[0];
float length = dist.magnitude; // make wall long enough to span ss[0] to ss[1]
Vector3 cubeSize = new Vector3(thickness, height, length);
Vector3 cubePos = ss[0] + (cubeSize * 0.5f); // cube pivot is in its center
Quaternion cubeOrientation = Quaternion.LookRotation(dist, Vector3.up); // rotate to look at ss[1]
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.localPosition = cubePos;
cube.transform.localScale = cubeSize;
cube.transform.localRotation = cubeOrientation;