Hi community,
I am trying to create curved streets while runtime and therefore i need to bend planes for my sidewalk (bending the street works already).
My function at first calculates the new length of the plane and changes its position and scale.
This is vital because the closer you are to the center point of the circle you are bending arround, the less length you need for a part with an angle of alpha. (U = 2pir/ a/360 U = a/360 *2 pi * r)
The amount of segments that you have in a plane is 10x10 (by default) and so you need 11 entries for vertices in one dimension.
Then I divide the degree i want to bend by 10 and calculate the coordinates of a rotation arround the origin by x/10 times the amount i want to bend (im working on a unit circle here) and save them for later. I also save the radius sizes for each column of vertices within my mesh.
Then i just multiply the rotated points by the specific radius size and then i need to add the actual radius position to get the position of the new point in the global coordinate system.
After that I just need to get the new mesh vertex coordinates. To get these I have to do this:
Vector3 vertexPositionInMesh = (newGlobalPositionVertex-objectPosition)/scale
Sooo… this is my idea and I hope you could follow it /understand my bad english. And here is my code:
float rotate = transform.parent.GetChild(1).GetComponent().rotate;
Vector3 position = transform.parent.GetChild (1).transform.position;
float meshLengthZ = transform.parent.GetChild (1).GetComponent ().mesh.bounds.size.z;
float radius = meshLengthZ/(-rotate / 360 * 2 * Mathf.PI);
Vector3 r = new Vector3(radius,0,0);
Vector3 positionObject = transform.position;
float offset2Radius = (positionObject - position).magnitude;
Vector3 radius2Object = positionObject - r;
float r2Object = radius2Object.magnitude;
float rotateAmount = rotate;
if (rotateAmount < 0) {
rotateAmount = -1;
}
Mesh meshObject = GetComponent ().mesh;
float meshLengthXObject = meshObject.bounds.size.x;
float meshLengthZObject = meshObject.bounds.size.z;
float zOffset = positionObject.z - (meshLengthZObject / 2);
float newZSize=(rotateAmount / 360) * 2 * Mathf.PI * r2Object;
Vector3 scale = transform.localScale;
float zLenghtOrigin = meshLengthZObject / scale.z;
float newZScale = newZSize / zLenghtOrigin;
scale.z = newZScale;
transform.localScale = scale;
float positionz= (scale.zzLenghtOrigin) / 2;
positionObject.z = positionz+zOffset;
transform.position = positionObject;
Vector3[ ] vertices = meshObject.vertices;
Vector3[ ] normalizedMeshPoints = new Vector3[11];
float[ ] radiusSizes = new float[11];
float partialDegree=rotate/10;
int parseVerticesStart = 0,counter = 1,parseNormalizedMeshPointsStart = 10;
if (rotate < 0) {
counter=-1;
parseVerticesStart=120;
parseNormalizedMeshPointsStart=0;
}
for(int h = 0;h<11;h++){
float turn = hpartialDegree;
Vector3 help = Quaternion.Euler(0, -turn, 0) * Vector3.right;
normalizedMeshPoints=help;
//Calculate Raiudslength with help of the vertices (scale.xVector3.right = length of 1 plane segment/ h-5 is a counter that parse the plane from left(-5) to right(5)))
radiusSizes=radius2Object.magnitude-5scale.x+ ((h-5)scale.xVector3.right).magnitude;
Debug.Log(radiusSizes);
}
int v = parseVerticesStart;
float newX,newZ;
Vector3 currentPosition, newPosition, vertexPosition;
for(int i = 0; i<11;i++){
currentPosition=normalizedMeshPoints[parseNormalizedMeshPointsStart-icounter];
for(int j=0;j<11;j++){
newPosition=currentPosition*radiusSizes[j]*counter;
vertexPosition=(newPosition-positionObject+r);
newX=vertexPosition.x/scale.x;
newZ=vertexPosition.z/scale.z;
vertexPosition.x=newX;
vertexPosition.z=newZ;
Debug.Log ("vertexPosition (newPos-posObj+r): "+vertexPosition);
vertices[v]=vertexPosition;
v+=counter;
}
}
//
// }
meshObject.vertices = vertices;
meshObject.RecalculateBounds ();
}
I’ve attached 2 images to this post where you can see the result of bending a street 45 degrees to the right. You can clearly see that the width of the white planes has changed and the radius and/or the vertices has been calculated wrong because the rendered mesh has an unnormal offset to its mesh collider. (compare bendstreet2.png with bendstreet3.png. The street in the middle has been bend correctly but not the planes on the side of it).
Can you please tell me where I did my mistake. I cant figure it out by myself ![]()


