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

Simple Grid

Discussion in 'Scripting' started by Retrospark, Jul 24, 2014.

  1. Retrospark

    Retrospark

    Joined:
    Sep 28, 2012
    Posts:
    30
    I am making an android game that will need a grid. When you tap on the screen, it'll select the square in grid you tapped on. How do I go about doing this? I've searched everywhere for tutorials, but nothing shows what I am looking for. Only a couple seemed to be related, but were coded in C#. I don't even know where to begin with this script?
     
  2. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    For the grid you just need sprites, or objects with colliders.

    Then you read the input tap or touch and cast a ray into the scene via the camera to hit the square.

    See the GetTouch examples the third one is close to what you want http://docs.unity3d.com/ScriptReference/Input.GetTouch.html

    Do a bit more digging in the docs and you will have the basic input routine you need.

    The grid you can set up in the IDE or with a nested loop.

    Hint if you name the grid elements then that could help if you need to work out where the activated grid square is.
     
  3. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    Is the grid in 2D or 3D ?

    To locate the squares:
    If it's 2D seen from top then you simply divide the input touch coordinates by the size of your squares in pixels.
    If it's 3D consider using Camera.ScreenPointToRay and Physics.Raycast.

    Also try to follow some tutorials if this doesn't help !

    Good luck :)
     
  4. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    You can go through my tutorial, and don't worry! Squares and way easier than stupid freaking hexagons. In my version of hell there will be gigantic hexagons waiting for me.
     
  5. Retrospark

    Retrospark

    Joined:
    Sep 28, 2012
    Posts:
    30
    If you look up Harvest Moon Magical Melody, then you will see how the grid system works. Its a 2D grid system, but a 3D game. Thanks for the help! I'll get started right away!
     
  6. Retrospark

    Retrospark

    Joined:
    Sep 28, 2012
    Posts:
    30
    I couldnt find your tutorial on squares?
     
  7. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    There isn't one. But the vast majority is easily transferable. You should be able to make a map of anything if you can grasp the concepts provided in the hexagon tutorial. Should even be easier.