This is a problem I have encountered while working on my personal side project, a simulation game. Unity mesh allows developers to dynamically create meshes at runtime. Meshes take an array of Vector3s and an array of Integers to create the mesh. Vector3 is a data type that stores the X, Y, and Z positions as floating point numbers.
Creating an array of Vector3s, which is the vertices, and the array of integers; signifying the triangles; allows the developer to make any shape they would like. The array of integers which is grouped into threes, tells the mesh system which three vertices, by index from the array of vertices, make up the triangle. All triangles are rendered as one sided triangles, developers can change which side is rendered by swapping the first and last indexes of the triangle. Example 1,2,3 should be come 3,2,1 to reverse it.
Wall generation will utilize a starting and ending point, passed in as Vector3s. They also have an extrude height and an extrude width. This will allow a lot of customization in the long run. Below is an image showing the wall generation tool with two selected points that are 3 units apart in the X direction.
However when you make the same distance wall in the Z direction it looks more like the image below.
The problem is when the walls are generated in the Z direction their extrude outward from the input points isn’t calculated correctly and makes the wall flat. Below is the code used to add the vertices to the list of vertices.
verts.Add(startPosition + new Vector3(0.0f, 0.0f, -widthOffset)); // 0 verts.Add(startPosition + new Vector3(0.0f, 0.0f, widthOffset)); // 1 verts.Add(startPosition + new Vector3(0.0f, height, -widthOffset)); // 2 verts.Add(startPosition + new Vector3(0.0f, height, widthOffset)); // 3 verts.Add(endPosition + new Vector3(0.0f, 0.0f, -widthOffset)); // 4 verts.Add(endPosition + new Vector3(0.0f, 0.0f, widthOffset)); // 5 verts.Add(endPosition + new Vector3(0.0f, height, -widthOffset));// 6 verts.Add(endPosition + new Vector3(0.0f, height, widthOffset)); // 7
Solution #1: Create the object using the distance between the start position and end position in the X(or Z) direction and then rotate the object towards the end point. This might work if you use the origin as the start position and then set transform.position on the object to move it to the correct position.
Solution #2: Use mathematic formula derived from the formula to calculate the tangent of a bezier curve to calculate the rotation needed at the start and end points.
Solution #3: Use bezier curve that has 4 points and set the two control points to the midpoint of the line. I have already implemented a 4 point bezier wall generation system, wouldn’t be too hard to convert. Four point bezier curve is shown below in wireframe mode.