Search Unity

Grid on terrain help

Discussion in 'Scripting' started by FusionSausage, Oct 6, 2013.

  1. FusionSausage

    FusionSausage

    Joined:
    Sep 8, 2013
    Posts:
    3
    Hi! I have a terrain created with Unitys terrain tools, and now I want to have a grid on the terrain. The grid will be functional. It will show where to place buildings, where AI's are going to go and other fun stuff. Now, I was wondering. How would I do this? I'm making a kind of RTS game, although not exaktly that. How do I create a grid on my terrain and if I place my mouse over a "rectangle" in the grid it should highlight. I really need help with how to achieve this, because I haven't found a good tutorial on this.

    Thanks!
     
  2. uy

    uy

    Joined:
    Jun 18, 2013
    Posts:
    55
    Well first you're going to need a ray and a raycasthit

    I'm going to do this in c# but it's easily converted into javascript

    start by declaring the ray and the hit. The ray is the actual thing that will touch the object where the cursor is, and the raycasthit is what will take the shape of that hit object/point
    Code (csharp):
    1.  
    2.  
    3. Ray ray;
    4. RaycastHit hit;
    5.  
    6.  
    in the update function, you need to update the position of the ray in correspondence to the cursor.
    we then need to actually check where the hit is, or if an object was actually hit.
    You can put this in an if checking if there is a mousebuttondown or mousebutton or whatever you want

    Code (csharp):
    1.  
    2.  
    3. ray = Camera.Main.ScreenPointToRay(Input.mousePosition);
    4.  
    5.  
    6. //the ray is the ray, you then set the position of the hit with the out, and the 5000 is however many meters you want the ray to be valid for. you can change the value
    7. if (Physics.Raycast(ray, out hit, 5000)) {
    8.  
    9.      //You can attach this script to a plane w/ a single grid texture
    10.      //you want to set the pos of the "grid" to where the cursor hit. but this is where it gets to the actual grid making
    11.      //Lets say you want a grid that is 5 meters wide/tall. you need to use Mathf.Round to do this.
    12.      //This piece of code rounds the hit position to the closest 5 meters on the x and z (the y is 0.05 so the plane is visible, but is still visibly on the ground.
    13.      //Mathf.Round works by going -> Mathf.Round(valueThatNeedsToBeRounded / RoundDelta)*RoundDelta); where round delta is however much you want to round by.
    14.      transform.position = new Vector3(Mathf.Round(hit.point.x/5)*5, 0.05, Mathf.Round(hit.point.z/5)*5);
    15.  
    16. }
    17.  
    18.  
    19.  
    20.  
    21.  

    what might be easier is instead of actually putting in 5 each time, you make an int that you assign to whatever you want, and then place that variable in the place of the 5s in the above code


    This is untested code, so there might be errors, and it might not be EXACTLY what you are looking for, but this is the basic idea.

    If you want a visible grid everywhere you could do two for statements and instantiate each piece of the grid every however long/tall/wide it is
     
  3. bluemana

    bluemana

    Joined:
    Apr 29, 2015
    Posts:
    5