- Home /
Mouse Position to 2D array
I'm currently working on something where you are able to place buildings and roads. The level in which you are able to build is made using a 2D array like this:
int[,] map = new int[10,10] {
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
void BuildMap()
{
for(int x = 0; x < MapWidth; ++x)
{
for(int y = 0; y < MapHeight; ++y)
{
GameObject Tile = (GameObject)Instantiate(Resources.Load("Tiles/Tile" + map[x,y]), transform.position, Quaternion.identity);
Tile.transform.position = new Vector2((x * TileSize - (MapWidth / 2) * TileSize) + TileSize / 2, -(y * TileSize - ((MapHeight / 2) * TileSize) - TileSize / 2));
Tile.transform.parent = transform;
}
}
}
This will generate a level made of tiles where 0 is grass and 1 rock. But I don't want to give every tile a collider to check if the player is capable of placing a building using an overlappoint. What I want to do is when you want to place a building it checks the 2D array if you're allowed to place something and replaces that number in the array with an new number for the building. So my question is: How do I convert the mouse position to the right position in the 2D array, check it and replace it when allowed?
Generally tile-based games do not spawn an individual tile object for each tile. In most cases this is grossly inefficient. You should definitely do some research on tile-based games before you get too deeply invested to change this approach.
Converting a world position into "tile space" is generally quite simple, especially when your cursor always lies on the floor. The procedure is this:
Get cursor position on the "floor" by projecting a ray against the floor plane.
Round this position to the nearest integer value.
These x,y integers correspond to the array index of the tile.
If your tiles are not 1x1 units, or the map origin is not 0,0, the formula must account for this. There's a ton of info about this stuff floating around, just waiting to be found. Good luck,
Answer by Pearbook · Mar 14, 2015 at 04:52 PM
I found the solution to my problem. Nothing visually has been done yet but this is what the code looks like right now:
public class Grid : MonoBehaviour
{
public float snapValue = 1;
public int MapWidth;
public int MapHeight;
public int[,] map;
private Vector2 mousePos;
void Start ()
{
map = new int[MapWidth, MapHeight];
}
void Update()
{
if(Input.GetMouseButtonDown(0))
{
GetGridPoint();
}
}
void GetGridPoint ()
{
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
float snapInverse = 1/snapValue;
float x, y;
x = Mathf.Round(mousePos.x * snapInverse) / snapInverse;
y = Mathf.Round(mousePos.y * snapInverse) / snapInverse;
mousePos = new Vector2(x, y);
if(map[(int)mousePos.x, (int)mousePos.y] == 1)
{
print ("Can't place an object here.");
}
else
{
print ("Place Object.");
PlaceObject(1);
}
}
void PlaceObject(int id)
{
map[(int)mousePos.x, (int)mousePos.y] = id;
}
}
Your answer
Follow this Question
Related Questions
Normalized Coordinates are Exponentially inccorect 2 Answers
Having a first person player look at the mouse on the y axis 1 Answer
I need advice on how to make a right click system similar to runescapes? 1 Answer
Determining groups in a 2D array by checking neighbours (and their neighbours etc) 2 Answers
Setting a property in a 2D array giving me an Object not set to Instance error. 1 Answer