4
\$\begingroup\$

I'm a beginner. I need to place a grid on my map in Unity and would like to access it to place a simple object on mouse click. How can I achieve this ?

I am unable to understand the grid functionality, should I have to write code for a 2D array or is there something in Unity that I can access, and what is basically the grid like something when I click my background image ?

\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$

This could be achieved in many ways. Unity has a pretty advanced positioning system as it is.

For just regular positioning look here https://docs.unity3d.com/Manual/PositioningGameObjects.html

But without prior knowledge of whether you are looking to do this in code, it's hard to give a good answer. Try asking more specific, precise questions that you feel others could benefit from that can't be found out with a little bit of research

EDIT: ADDITION This shows how to place a tower, the towers positions are checked against in an arraylist is C#. Hopefully this should give you an idea of how to start.

public GameObject tower;
public GameObject tower2;
private ArrayList towerList = new ArrayList();
void Start()
{
    Vector3 towerPosition = new Vector3(1.0F, .75F, 1.0F);
    foreach (GameObject towerObject in towerList)
    {
        if (towerObject.transform.position == towerPosition)
        {
            // there is already a tower in that position
            return;
        }
    }

    tower = (GameObject)Instantiate(tower, new Vector3(1.0F, .75F, 1.0F), this.transform.rotation);
    towerList.Add(tower);

    // for proof of concept you we're going to try to place the tower
    // in the same position as the other one
    Vector3 towerPosition2 = new Vector3(1.0F, .75F, 1.0F);
    foreach (GameObject towerObject in towerList)
    {
        if (towerObject.transform.position == towerPosition2)
        {
            // there is already a tower in that position
            return;
        }
    }
    tower2 = (GameObject)Instantiate(tower2, new Vector3(1.0F, .75F, 1.0F), this.transform.rotation);
    towerList.Add(tower2);
}
\$\endgroup\$
2
  • \$\begingroup\$ ok thnx for the reply. As I am going to place towers on my plane so what my understanding up to now is to make 2D Array first on my plane and access it to place towers. right ? for e.g place tower on myArray[0,0] OR I should access plane position without making 2D array ? for eg. place tower on transform.position(x,y,z). The game I am going to make is like fieldrunners.com/media \$\endgroup\$
    – Syed
    Sep 29, 2011 at 8:08
  • 1
    \$\begingroup\$ Updated the answer to reflect some additional information. As you can see we put the towers into an arraylist in c#. When we go to place a tower you check against all elements in the arraylist and make sure that were not placing our new tower in the same position as a preexisting one \$\endgroup\$ Sep 29, 2011 at 14:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .