Search Unity

Getting Weird Results when ray-casting vertices to lay over terrain

Discussion in 'Scripting' started by Filtiarn_, Sep 1, 2014.

  1. Filtiarn_

    Filtiarn_

    Joined:
    Jan 24, 2013
    Posts:
    173
    Hi I'm trying to make a grid for my tbs game that draws over terrain. Look at Capture for a pic of what I'm talking about. I'm getting some weird results. I attached an image called results showing what is going on.

    I create a cell by creating a quad.
    Code (CSharp):
    1. cells[x, z].cell = GameObject.CreatePrimitive(PrimitiveType.Quad);
    Then I position the cell right above my path node (Path nodes are laid out in a square grid formation)
    Code (CSharp):
    1.   cells[x, z].cell.transform.position = new Vector3(GridGraph.pathNodes[x, z].position.x, GridGraph.pathNodes[x, z].position.y + 1, GridGraph.pathNodes[x, z].position.z);
    Then I rotate x to 90 so it lays over the terrain instead of vertically over it.
    Code (CSharp):
    1. cells[x, z].cell.transform.eulerAngles = new Vector3(90,0,0);
    After all cells are created I raycast all the vertices of each cell the get the height of the map environment. You'll notice that i'm setting vertices.z instead of y because I rotated the cell.
    Code (CSharp):
    1. for (int x = 0; x < width; x++)
    2.         {
    3.             for (int z = 0; z < depth; z++)
    4.             {
    5.                 Mesh mesh = ((MeshFilter)cells[x, z].cell.GetComponent(typeof(MeshFilter))).mesh as Mesh;
    6.                 Vector3[] vertices = mesh.vertices;
    7.                 for (int i = 0; i < vertices.Length; i++)
    8.                 {
    9.                     RaycastHit hit;
    10.                     if (Physics.Raycast(new Vector3(vertices[i].x, vertices[i].y, vertices[i].z), Vector3.down, out hit,1000))
    11.                     {
    12.                         //vertices[i].x = hit.point.x;
    13.                         vertices[i].z = hit.point.y;
    14.                         //vertices[i].z = hit.point.z;
    15.                     }
    16.                     //else
    17.                     //{
    18.                     //  vertices[i].z -= 5;    
    19.                     //}
    20.                 }
    21.                 mesh.vertices = vertices;
    22.                 mesh.RecalculateBounds();
    23.                
    24.             }
    25.         }
    I put a debug.log and i am looping through 4 vertices for each cell but only two of them hits the map terrain. Each cell is a child of the parent grid object.

    Thanks for your time.
     

    Attached Files:

  2. Filtiarn_

    Filtiarn_

    Joined:
    Jan 24, 2013
    Posts:
    173
    So I got it working pretty much but it only works on terrain. Under the Raycast I am setting vertices.y to the terrain sample height and that works. I tried setting it to hit.point.y and I get some weird results. Here is code so far. I dont want to just sample terrain because I going to have different objects such as stairs and bridges so I would need to raycast.


    Code (CSharp):
    1.  for (int x = 0; x < width; ++x)
    2.         {
    3.             for (int z = 0; z < depth; ++z)
    4.             {
    5.                 cells[x, z] = new Cell();
    6.                 cells[x, z].cell = GameObject.CreatePrimitive(PrimitiveType.Plane);
    7.                 cells[x, z].cell.transform.position = new Vector3(GridGraph.pathNodes[x, z].position.x, 100, GridGraph.pathNodes[x, z].position.z);
    8.                 cells[x, z].cell.transform.localScale = new Vector3(GridGraph.nodeSize * 0.1f, 1, GridGraph.nodeSize * 0.1f);
    9.                 cells[x, z].cell.name = "Cell";
    10.                 cells[x, z].cell.transform.parent = transform;
    11.                 cells[x, z].cell.renderer.material.shader = Shader.Find("Transparent/Diffuse");
    12.             }
    13.         }
    14.  
    15. for (int x = 0; x < width; x++)
    16.         {
    17.             for (int z = 0; z < depth; z++)
    18.             {
    19.                 Mesh mesh = ((MeshFilter)cells[x, z].cell.GetComponent(typeof(MeshFilter))).mesh as Mesh;
    20.                 Vector3[] vertices = mesh.vertices;
    21.                 Vector3 position = new Vector3(cells[x, z].cell.transform.position.x + (cells[x, z].cell.transform.localScale.x * 10 / 2), cells[x, z].cell.transform.position.y, cells[x, z].cell.transform.position.z + (cells[x, z].cell.transform.localScale.z * 10 / 2));
    22.  
    23.                 float xStep = cells[x, z].cell.transform.localScale.x;
    24.                 float zStep = cells[x, z].cell.transform.localScale.z;
    25.                 int squaresize = 10 + 1;
    26.                 for (int n = 0; n < squaresize; n++)
    27.                 {
    28.                     for (int i = 0; i < squaresize; i++)
    29.                     {
    30.                         RaycastHit hit;
    31.                         if (Physics.Raycast(position, -Vector3.up, out hit, 1000.0F))
    32.                         {
    33.                             vertices[(n * squaresize) + i].y = Terrain.activeTerrain.SampleHeight(position);
    34.                             position.x -= xStep;
    35.                         }
    36.                     }
    37.                     position.x += (((float)squaresize) * xStep);
    38.                     position.z -= zStep;
    39.                 }
    40.  
    41.                 mesh.vertices = vertices;
    42.                 mesh.RecalculateBounds();
    43.             }
    44.         }
     

    Attached Files: