# Grid selection

**URL:** <https://discussions.unity.com/t/grid-selection/401949>\
**Category:** Scripting\
**Tags:** Scripting\
**Created:** [November 18, 2009, 8:14pm UTC](https://discussions.unity.com/t/grid-selection/401949 "2009-11-18T20:14:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon\_96320892](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@anon\_96320892](https://discussions.unity.com/u/anon_96320892)\
**Post date:** [November 18, 2009, 8:14pm UTC](https://discussions.unity.com/t/grid-selection/401949/1 "2009-11-18T20:14:31Z")

</div>

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?

 ![226365--8160--$screen_shot_2009_11_18_at_120818_pm_924.png](https://europe1.discourse-cdn.com/unity/original/4X/b/0/f/b0fb1df046d115ff16f43cf54e3ed11e7c744063.png)

---

<div class="post-metadata">

**Author:** ![anon\_2074824](https://avatars.discourse-cdn.com/v4/letter/a/d9b06d/32.png) [@anon\_2074824](https://discussions.unity.com/u/anon_2074824)\
**Post date:** [November 18, 2009, 9:47pm UTC](https://discussions.unity.com/t/grid-selection/401949/2 "2009-11-18T21:47:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anon\_96320892](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@anon\_96320892](https://discussions.unity.com/u/anon_96320892)\
**Post date:** [November 18, 2009, 9:57pm UTC](https://discussions.unity.com/t/grid-selection/401949/3 "2009-11-18T21:57:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ivkoni](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/ivkoni/32/320785_2.png) [@ivkoni](https://discussions.unity.com/u/ivkoni)\
**Post date:** [November 18, 2009, 11:27pm UTC](https://discussions.unity.com/t/grid-selection/401949/4 "2009-11-18T23:27:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anon\_96320892](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@anon\_96320892](https://discussions.unity.com/u/anon_96320892)\
**Post date:** [November 19, 2009, 7:00pm UTC](https://discussions.unity.com/t/grid-selection/401949/5 "2009-11-19T19:00:03Z")

</div>

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.
