# Generating a Flat Mesh from Spline/Bezier Curve?

I’ve been scratching my head over this problem for the past several days… I finally got something close to what I was hoping for.

Basically I’m attempting to create a new Mesh, the vertices will make a 2D road that follows a Bezier curve. I finally figured out that Meshes use Local Space, after doing the conversations I was able to get the curve to end in the right place. But now I’m stuck trying to figure out how to get both edges of the road to follow a curve. I basically need an extrude point along the sideways Vector of the current curve position.

This is my code for each iteration of a mesh part in the curve:

``````for (var i2 = 0; i2 < curveResolution; i2++) {

var newI2 : float = i2;
var catT : float = newI2 / (curveResolution - 1);
// Get the point on our curve using the 4 points generated above
var p : Vector3 = PointOnPath(catT, points[0], points[1], points[2], points[3]);
var p2 : Vector3 = p;

//What do I do with p2 to give the road thickness?
//and p starts in the center of the transform...
//how do I shift p over while still following the curve?

if (catVerts.Count >= 4) {
// We have completed a new quad, create 2 triangles
var start : int = catVerts.Count - 4;
}

}
``````

If that was too confusing basically what I need is a way to get the green lines here:

Obviously I have the black line from the PointOnPath function, but now I need the translations of it that will let create thickness for the road mesh.

Thanks! Or if you have your own example of Mesh Generation on a spline, that would be really helpful as well!

Recently I made something similar. How I got it to work is to not only get the point, but also get the forward at any given point along the path. (I used this tutorial, and there they included how to get the forward). Once you have the forward at that position, you also need an up in order to create a quaternion using Quaternion.LookRotation, since you want 2d, you can simply use Vector3.up I suppose.

Now that you have the rotation, you can calculate the point left and right to it using this simple calculation:

``````Vector3 left = rotation * Vector3.left;
Vector3 right = rotation * Vector3.right;
``````

Now note that left and right here are just the offset to the PointAtPath, so you need to add them to that to get the correct position. You can ofcourse multiply Vector3.left and right by any value to get wider/smaller roads.