Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Pearbook · Mar 13, 2015 at 11:10 PM · arraymousemouse position2d array

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlwaysSunny · Mar 13, 2015 at 11:04 PM 0
Share

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,

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges