Drawing a plane facing upwards
I'm trying to make a basic selection "preview" box for a city building game. The best way to describe it, is the way you click and drag zones in SimCity. over a grid.
my problem is that with my current script, due to the way the triangles are created, the mesh will draw upside down when click+dragged to the upper left or lower right. I can use a bunch of nested if statements to draw 4 seperate meshes, each for the 4 corners... but something tells me there is an easier way...
here is the code:
//create preview plane mesh
Mesh CreatePreviewMesh(Vector3 start, Vector3 end){
Mesh m = new Mesh();
m.name = "PreviewMesh";
m.vertices = new Vector3[]{
new Vector3(start.x, 0.01f, start.z),
new Vector3(start.x, 0.01f, end.z),
new Vector3(end.x, 0.01f, end.z),
new Vector3(end.x, 0.01f, start.z)
};
Vector3 point1 = m.vertices[0];
Vector3 point2 = m.vertices[1];
Vector3 point3 = m.vertices[2];
Vector3 point4 = m.vertices[3];
Debug.Log(point3.ToString("F2"));
m.uv = new Vector2[]{
new Vector2 (0, 0),
new Vector2 (0, 1),
new Vector2(1, 1),
new Vector2 (1, 0)
};
m.triangles = new int[]{0, 1, 2, 0, 2, 3};
m.RecalculateNormals();
return m;
}
and here is the code with aligns it to the grid and draws the quad:
Vector3 start = new Vector3(Mathf.RoundToInt(dragStartPosition.x),Mathf.RoundToInt(dragStartPosition.y),Mathf.RoundToInt(dragStartPosition.z));
Vector3 end = new Vector3(Mathf.RoundToInt(cursorWorldPosition.x),Mathf.RoundToInt(cursorWorldPosition.y),Mathf.RoundToInt(cursorWorldPosition.z));
previewMesh = CreatePreviewMesh(start,end);
Graphics.DrawMesh(previewMesh, Vector3.zero, Quaternion.identity, previewMat, 0, null, 0, null, false, false);
Any help is appreciated!
Your answer
Follow this Question
Related Questions
Why flipping triangles, not normals 1 Answer
Some triangles are black on a mesh generated by script 1 Answer
Normals and triangles 0 Answers
Marching Cubes normals Issue 0 Answers
How to rotate gameobjects along animated mesh surface right? 0 Answers