- Home /
Procedural rail generation from prefabs
Hi everyone!
I need help figuring out how to bend rail prefabs that get spawned around a cinemachine smooth path so, the rails are smoothly around the spline without sharp edges and gaps.
The idea is to create and adjust the cinemachine smooth path how you need, then generate the rails around it. Rails mean the metal parts not the wood underneath them.
This is my approach for spawning the rails and adjusting them to the spline:
1) place rail prefab each (railSize x currentNumberOfRailsPlaced) position until you reach the end of the path. In a for loop.
2) For each rail prefab/mesh: for each vertex in the mesh find the closest point on the path (Point = position, not waypoint!).
3) Check the distance between the vertex and the closestPointOnPath if it is longer than the wanted distance (rails width) reposition the vertex.
--> get a normal vector for the closestPointOnPath: get point's tangent, get the normal vector by rotating the tangent either +90 / -90 degrees (depending if you are generating rails on the right or left side of the path).
--> Get new vertex position: closestPointOnPath.pos + (pointNormalVector * wantedRailDistanceFromPath).
4) Assign these new vertex positions to the vertex array of the rail mesh.
5) Recreate triangles for the rail mesh.
Questions:
1. Is there a way to find the closest point on the path for each vertex? (cinemachinePath.FindClosestPoint gives the closest Waypoint - if I am correct - and that is not what we need)
2. Is there an automatic way to tell the mesh to recreate its triangles without having to specify the order of vertices for each triangle? e.g. to basically move the vertices and have the mesh readjust based on this change automatically. The rail prefab will be a 3d model made by artist, not a simple primitive shape.
Below are some screenshots. Also, the reason for having the rails as prefabs is to be able to delete some of them once generated so for example there can be a jump or as if the track was broken in some parts.
(Uploading images gives me errors sorry, here are the links to them)
Rail prefabs placed on path IMAGE
Intended bending of the rail prefabs along with the path IMAGE
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Can I save class instances in a prefab-like way? 1 Answer
Hint joint how to configure 0 Answers
Generate FTL style star map 0 Answers