title: c# unity 3d coordinates map sync
body: <p>I'm working on a procedurally generated map/game. I have 3D chunks with 20x20x20 points in a 3D array. I'm using marching cubes algorithm to generate the chunks. I try to manipulate the density with adding and subtracting the values but in the same time trying to sync the neighbour chunks edge together the with chunk im modifying.<a href="https://i.stack.imgur.com/Y1OBD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Y1OBD.png" alt="adding density to a chunk&#39;s points"></a></p>
  
  <p>Here is the code i tough it will work, but its not working:</p>
  
  <pre><code>`for (int i = 0; i &lt; 9; i++)
      {
          Chunk nChunk = neighbours[i].GetComponent&lt;Chunk&gt;();
          if (i == 4) continue; //skip self
          for (int z = 0; z &lt; 20; z += 19)
          {
              for (int x = 0; x &lt; 20; x += 19)
              {
                  for (int zz = 0; zz &lt; 20; zz += 19)
                  {
                      for (int xx = 0; xx &lt; 20; xx += 19)
                      {
                          for (int y = 0; y &lt; 20; y++)
                          {
                              if (Points[xx, y, zz].wPosition == nChunk.Points[x, y, z].wPosition)
                                  nChunk.Points[x, y, z].density = Points[xx, y, zz].density;
                          }
                      }
                  }
              }
          }
      }`
  </code></pre>
  
  <p>I want to check the edges and not all Points to save performance. the y coords the height.</p>
  
  <p>Here is how i call this:</p>
  
  <pre><code>        //mouse click
      if (Input.GetMouseButtonDown(0))
      {
          Ray raym = Camera.main.ScreenPointToRay(Input.mousePosition);
          RaycastHit hitm;
          if (Physics.Raycast(raym, out hitm))
          {
              if (hitm.transform.GetComponent&lt;Chunk&gt;())
              {
                  Chunk cScript = hitm.transform.GetComponent&lt;Chunk&gt;();
                  for (int z = 0; z &lt; 20; z++)
                  {
                      for (int y = 1; y &lt; 19; y++)
                      {
                          for (int x = 0; x &lt; 20; x++)
                          {
                              if (PointInsideSphere(cScript.Points[x, y, z].wPosition, hitm.point, 1f))
                                  cScript.Points[x, y, z].density -= 0.1f;
                          }
                      }
                  }
                  cScript.SyncNeighbours();
              }
          }
      }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/pDPEk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pDPEk.png" alt="enter image description here"></a></p>
  
  <p>In this way its working good but i want to check only the edges.</p>
  
  <pre><code>        for (int i = 0; i &lt; 9; i++)
      {
          Chunk nChunk = neighbours[i].GetComponent&lt;Chunk&gt;();
  
          if (i == 4) continue; //skip self
          for (int z = 0; z &lt; 20; z ++)
          {
              for (int x = 0; x &lt; 20; x ++)
              {
                  for (int zz = 0; zz &lt; 20; zz ++)
                  {
                      for (int xx = 0; xx &lt; 20; xx ++)
                      {
                          for (int y = 0; y &lt; 20; y++)
                          {
                              if (Points[xx, y, zz].wPosition.x == nChunk.Points[x, y, z].wPosition.x &amp;&amp; Points[xx, y, zz].wPosition.y == nChunk.Points[x, y, z].wPosition.y &amp;&amp; Points[xx, y, zz].wPosition.z == nChunk.Points[x, y, z].wPosition.z)
                                  nChunk.Points[x, y, z].density = Points[xx, y, zz].density;
                          }
                      }
                  }
              }
          }
      }
  
      for (int j = 0; j &lt; 9; j++)
      {
          neighbours[j].GetComponent&lt;Chunk&gt;().UpdateChunk();
      }
  </code></pre>