Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Grid placement

Discussion in 'Scripting' started by luckie12, Feb 24, 2015.

  1. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    Hello guys :)
    I have just one simple question... Ok its not that simple. For some of us...

    I'm creating a farming game, the inventory works, tree growing works, picking up items and placing in inventory works and the tree chopping works.
    But my only problem now is, i want a GRID for my whole terrain, so a player can ONLY within like a radius of 2 units build something, like with grid snapping.

    The question is;

    How can i make it that when someone presses B ( Changes to my build cam) sees a grid on the WHOLE terrain and can place a object on it?(Snapped)

    This is what i have at the moment;

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GridSystem : MonoBehaviour {
    5.  
    6.     public GameObject plane;
    7.     public int width = 10;
    8.     public int height = 10;
    9.  
    10.     private GameObject [,] grid = new GameObject[10,10];
    11.  
    12.     void Awake()
    13.     {
    14.         for (int x = 0; x < width; x ++)
    15.         {
    16.             for(int z = 0; z < height; z ++)
    17.             {
    18.                 GameObject gridPlane = (GameObject)Instantiate (plane);
    19.                 gridPlane.transform.position = new Vector3(gridPlane.transform.position.x +x, gridPlane.transform.position.y, gridPlane.transform.position.z +z);
    20.                 grid[x,z] = gridPlane;
    21.             }
    22.         }
    23.     }
    24.     void OnGUI()
    25.     {
    26.  
    27.     }
    28.     // Use this for initialization
    29.     void Start ()
    30.     {
    31.  
    32.     }
    33.  
    34.     // Update is called once per frame
    35.     void Update ()
    36.     {
    37.  
    38.     }
    39. }
    40.  
    With kind regards,
    It only makes a small grid on a 0.1 0.1 0.1 plane
    Luc
     
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    I would have each tile, have its own script, and pass its renderer bounds to a common manger at start to form a list.

    Then during build phase, run a ray. Check of Ray falls within bounds of any tile in list. If it does, mark it visually somehow.
     
  3. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    Thats one mouth full renman. Have too look what i can do with it..
     
  4. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    Displaying a grid is a bit of a headache, there are a few ways of doing it and in my opinion the best option is to do a simple method that gets the job done.
    If you use unityPro you could use a projector but simpler yet, create a canvas that hovers above the terrain with an image of a grid, then simply make the canvas follow the camera with a script and round the x,z position to the nearest 1 or 2 or whatever you want for your placement, similarly round the position of an object when placing it.

    Scaling of the canvas will be tricky as will parallax error from it being a little above the ground. Experiment and don't be be afraid to throw out the need for a grid in favor of other options!
     
  5. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    haha i will look for it, kinda new scripter on C# :$
     
  6. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    It's really not.
    I guess it's all relative tho.

    Seriously, the mehpthod I suggested is a very workable, flexable, handy and effecient one.


    Just a tip as I am taking you are a relatively jr programmer. Learn to build your projects with manager, and units. Managers over see units, units report to and recieve instructions from the manager.

    So, tiles manager, delegates out to tile units. Tile units may tell a manager it exists at start and to add it to the managers tile list. The unit can provide basic information of itself that the manger stores.

    The manager can then pass info to all or any one of the units etc.


    Hope that helps.
     
  7. jpthek9

    jpthek9

    Joined:
    Nov 28, 2013
    Posts:
    944
    An easy method that does have a bit of overhead is instantiating a quad with your grid graphic for each of the individual cells. When you instantiate then, be sure to assign them to a coordinate on the grid so when they detect a click, you know where on the grid the click was placed.
     
  8. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    To use bounds and a ray tho, your user can move the position of the mouse, touch where ever and all tiles bounds are accounted for. This allows for flexability. Anyhow, it's what I do.
    :p
     
  9. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    Thanks about to look for some Tiles tutorials :),

    Does anybody know why i cant put any of the tutorials in my head?
    I watched alot from TheSlayer and so on, but yes it works, but i cant get it in my head.
    Are there any great conditions just to learn the Unity C#, I am very distractible its kinda annoying to learn a C# Unity that way. Hope someone has a tip for me :)

    -KR
    Luc