Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 NationalRedoubt · Jun 22, 2017 at 09:12 AM · 2d gametilemapproperties

2D TileMap with each tile as a separate object

I'm currently working on a small project that's based on 2D tiles. My main problem now is figuring out how to have each tile have different properties (movement point cost etc.) that would be revealed in a tooltip on mouse-over. I've found some tutorials that deal with this rogue-like concept but they're all based on randomly generated maps which I don't plan to use.

My current solution would be to have each non-cosmetic tile as a separate object with collision box so it can be detected with OnMouseOver and then the scripted properties would be shown on a tooltip. Each time player would move a script would check the properties of the tile and act accordingly.

At my current game resolution/tile size that's ~225 tiles/objects onscreen in a busy scene, but usually less since walls/decorations would be grouped up as meshes. The off-screen tiles would be culled. But as I've read Unity has problems handling many objects at once but I'm not sure how many on-screen tiles would be too much, performance wise.

So I'd like some advice on how to deal with tile based game where most of them aren't just cosmetic and should affect the player in one or more ways (with tooltips OnMouseOver) without killing the performance and not employing randomly generated maps.

Thanks.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by toddisarockstar · Jun 23, 2017 at 08:32 PM

Check out the similar example i gave here to create an array of arrays to represent coordinates:

link text

you could set up your tile/mouseover positions in space to correlate with arrays of info. i would recommend making the first tile in your scene start at postition 0,0 then make the tile/grid positions at even number positions up and right from that so that they are all positive. Then when you get your mouseover position, it can simply be rounded off and directly used to get info from an array based on the two coordinates. this method should is super proficient too. let me know if you have any questions about it!

Comment
Add comment · Show 7 · 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
avatar image NationalRedoubt · Jun 23, 2017 at 08:54 PM 0
Share

Thanks for the recommendation, I'll try it tomorrow.

avatar image NationalRedoubt · Jun 24, 2017 at 03:36 PM 0
Share

Ok so I used your script and it seems to generate the coordinate array. By default their values are 0 and assigning values manually also works. So far so good.

I'm very new to C# so this is all new to me. Anyway this code displays mouse's location in world coordinates (they still have a decimal point for some reason, $$anonymous$$athf.Round properly removes the decimal point in the next code part though).

     void OnGUI()
     {
         Vector2 p = new Vector2();
         Camera c = Camera.main;
 
         p = c.ScreenToWorldPoint(new Vector2($$anonymous$$athf.Round(Input.mousePosition.x), $$anonymous$$athf.Round(Input.mousePosition.y)));
 
         GUILayout.BeginArea(new Rect(40, 400, 250, 120));
         GUILayout.Label("World position: " + p);
         GUILayout.EndArea();
     }

This part stores mouse x and y world coords in separate variables on key press. For some reason the Debug.Log is called twice but at least I get int values, no decimals.

         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0))
         {
             int mouseInfoX = $$anonymous$$athf.RoundToInt(p.x);
             int mouseInfoY = $$anonymous$$athf.RoundToInt(p.y);
 
             Debug.Log("X: " + mouseInfoX + "Y: " + mouseInfoY);
         }

I tried figuring out how to compare the mouse location (in world) with the array (with foreach etc.) but I'm obviously missing something so nothing worked. So now I need advice how to compare the array with mouse coordinates int values.

Next step would be some auto-assignment of array properties. It would be quite laborious to manually edit array each time I make a map. I'm currently using Tiled and Tiled2Unity for tile maps and the map is exported as a .xml file with the tile/layer properties. There should be a way to combine the Tiled2Unity mesh (visual part) with Tiled xml (data part).

avatar image Cherno NationalRedoubt · Jun 24, 2017 at 04:27 PM 1
Share
  1. OnGUI can be called multiple times per frame so it's bad using Input.Get$$anonymous$$ey and similar in it as these will fire twice as you already noticed. Ins$$anonymous$$d, use Events.

  2. Don't use $$anonymous$$athf.Rount to get your cursor tile position. Use $$anonymous$$athf.Floor ins$$anonymous$$d.

    Vector3 cursorPos = YourCamera.ScreenToWorldPoint(Input.mousePosition);

    coords_cursor = new Vector2( $$anonymous$$athf.FloorToInt(cursorPos.x), $$anonymous$$athf.FloorToInt(cursorPos.y) );

There is cvertainly a way to read and convert Tiled's format, after all that's what Tiled2Unity does, but it involves diving deep into the code so if you are a beginner be prepared for quite a task at hand. That's one of the reasons why I commonly write my own level editors.

i

avatar image NationalRedoubt Cherno · Jun 24, 2017 at 05:24 PM 0
Share

Thank you, removed the mouse function from OnGui, will learn more about events.

FloorToInt is indeed better, turns out rounding (up) would result in 1 tile offset, didn't notice that at first.

Yeah, the xml produced by Tiled is quite readable even for me but using it is something else. Of course I'm not the first one to try something like this so I'm slowly gathering tips/methods for my grid/tile based game.

Show more comments

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

72 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Annoying lines in 2D tilemap. 0 Answers

How could I make a fog of war on a tilemap 0 Answers

How to tell if your touching the side of the ground or platform 1 Answer

optimizing game objects - deciding what way to organize my level. 0 Answers

How to store tile data for placing tiles 2 Answers


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