Search Unity

Grid selection

Discussion in 'Scripting' started by nooB, Nov 18, 2009.

  1. nooB

    nooB

    Joined:
    Oct 4, 2009
    Posts:
    20
    I have a tower of defense style game where I have to place cards on to the flat terrain.

    The cards (GUI) at the bottom left are to be dragged and dropped on to the terrain.
    How can I go about it?
     

    Attached Files:

  2. KaelisAsur

    KaelisAsur

    Joined:
    Apr 9, 2009
    Posts:
    361
    Insert an empty GameObject into your scene and add a thin box collider that covers your entire grid. Then, when a card is dropped, simply raycast against it and calculate grid coordinates based on the hit point.
     
  3. nooB

    nooB

    Joined:
    Oct 4, 2009
    Posts:
    20
    Thanks KaelisAsur!!!!


    The issue is that the card has to drop into predefined grids as when I drag the card the grid over which the card is hovering should highlight

    How do I draw a invisible grid?
    How do I do the above in this case?

    Thanks for help
     
  4. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    Say your grid is in the xz plane, y = 0. Then if you are moving the card in a plane parallel to the xz plane, say at y = 5. By the position of the card (x and z coords) you know in which grid you are (just check if the the xz position of your card is contained in a rectagle). If you have a lot of rectangles in the grid, you should try to think of a way to speed up this check. For example if your rectangles' center x coords are 0, 1, 2, 3, etc, then just take the x coord of the card and round and you know in which column (or row) in the grid you are, etc, etc.
    To highlight a rectanagle you can position a quad with a transparent texture on top of the rectagle if the terain is even. You can also use line renderer.
    If the terrain is uneven, use projector.
     
  5. nooB

    nooB

    Joined:
    Oct 4, 2009
    Posts:
    20
    Thanks again!!!

    Again, I am just not able to figure out how
    1. to create rectangles by script on the terrain grid
    2. I would be able to drag the cards from GUI onto the rectangles

    Any help would be appreciated. Also, Somebody knows of any reference/ code please lemme knw.
     
unityunity