Alto's Adventure Style Procedural Surface Generation Part 2


In this section we will be going through on how to make the depth surface that the ball is bouncing on. If you haven't seen Part 1, please do so and come back.
This is actually another plane which has a script that takes in a reference of the plane that does the up and down motion from the previous part.
Instead of relying on another set of perlin noise data to modify the vertices of the plane we take the first row of vertices from the plane with the 'SurfaceGenerator' component and give the same values to the current plane's vertices.
But we have to make sure that we give the 'z' value of the vertices of the 'SurfaceGenerator' object's mesh to the  'y' value of the depth surface.
I am calling that thing the depth surface because I can't think of a better word.๐Ÿ˜…
Let's call the class 'SurfaceImitator' because it imitates a surface.๐Ÿ‘.
The member variables are : 
public SurfaceGenerator reference;  
public bool generateCollider = true;  
private Mesh mesh;  
private Vector3[] vertices;  
The Start Function:
void Start()  
{  
   mesh = GetComponent<MeshFilter>().mesh;  
   vertices = mesh.vertices;  
}  
The LateUpdate Function: - We will come to why we are using 'LateUpdate' instead of  'Update' later on ( pun intended๐Ÿ˜‡)
void LateUpdate()  
{  
   vertices = mesh.vertices;  
   int counter = 0;  
   for (int i = 0; i < 11; i++)  
   {  
      for (int j = 0; j < 11; j++)  
      {            
        vertices[counter].y = reference.vertices[i].z;  
        counter++;  
      }  
    }  
   mesh.vertices = vertices;  
   mesh.RecalculateBounds();  
   mesh.RecalculateNormals();  
   if (generateCollider)  
   {  
      Destroy(GetComponent<MeshCollider>());  
      MeshCollider colliderComponent = gameObject.AddComponent<MeshCollider>();  
      colliderComponent.sharedMesh = null;  
      colliderComponent.sharedMesh = mesh;  
    }  
}  
Most of this code was already covered in the previous part.
The important part is this :
int counter = 0;  
for (int i = 0; i < 11; i++)  
{  
   for (int j = 0; j < 11; j++)  
   {            
       vertices[counter].y = reference.vertices[i].z;  
       counter++;  
   }  
}  
We are basically going through all the vertices of the mesh and setting each row's y axis value to the reference mesh's vertices ( These are not the mesh component vertices these are 'SurfaceGenerator' public member variable called 'vertices' ).
Coming to the point as to why we have to put this code in 'LateUpdate' is because of the fact that 'SurfaceGenerator' 'vertices' value gets updated on the 'Update' function of the 'SurfaceGenerator' so the values we get out of it will always be 1 frame behind all the time.
If the change in position of some of the vertices from one frame to another are very high we won't be able to match up with the 'SurfaceGenerator' vertices.
So inorder to get the current values of 'vertices' within the same frame we use 'LateUpdate'.
Source code of Part 2 is available HERE.
Unity package available HERE.
For more Unity development tutorials go HERE
Support Bitshift Programmer by leaving a like on Bitshift Programmer Facebook Page and be updated as soon as there is a new blog post.
If you have any questions that you might have about shaders or unity development in general don't be shy and leave a message on my facebook page or down in the comments.