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 /
  • Help Room /
avatar image
0
Question by riotjayne · Dec 22, 2016 at 07:24 AM · c#gridtilesareacalculation

How to calculate which tiles to affect in tile based game?

Hi all, sorry if this is fairly simple, but I've been working on it for a week or so now and can't seem to find a solution based on my limited programming skills.

So in the game, a bunch of tiles are instantiated in grid formation, labeled with their x and y location, added to an array, and then player can move from tile to tile one at a time.

Once you reach an edge tile, the game should calculate which overall section/collection of tiles from the first edge tile you left to the final edge tile you landed on, has the smaller number of tiles (or other similar queries). It should then change these tiles to whatever you'd like (for example, if all the tiles represent dirt, the new smaller section will change to grass tiles). So it isn't just the ones you've collided with, but each tile you've collided with closest edge neighbor and all the tiles in between.

It's a little hard to explain so I made an example gif here of the process, I hope it makes sense.

My problem is I'm struggling to find a way for the game to understand which tiles to affect. I've tried recording each tile you land on, then having all those tiles calculate which edge is closer, collect those tiles, and affect the tiles based on that. But the problem is if you walk in an irregular path, or occasionally close to one edge, it will affect tiles outside of the collective area, instead of keeping it all inside.

An example of this is here with a simple path, and with a more complex irregular path, the blue shows how my current attempts calculates it. The final image in green is the intended behavior.

I'm kind of losing my mind and I'm sure there's a simple way to do this that I just can't get to. I'd love any / all suggestions on how to possible accomplish this? Thank you all so much in advance for giving this question a look. I'm using C#.

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
1
Best Answer

Answer by PizzaPie · Dec 22, 2016 at 12:24 PM

Use a an array int[,] map to flag the walked tiles with 1 and 0 the rest. After you finish the walk by reaching an edge you can run a flood algorithm to figure your regions, save em on an array and compare the Counts. After that you can select the one you want and do whatever to it. For flood algorthm check the Procedural Cave Generator Tutorial (GetRegion()). The hard part on that is how you gonna know from where to start the algorithm run an ,kinda, efficient way would be to start with the (0,0) tile get the region it produce. Then get the next edge tile check if it is contained in the region you already have, if not run the algorithm with that tile as starting point, now you have a second region, note that region would be different type from your first one (if first one is 0 as unwalked this one will be type 1, your path). At that point you have the one unwalked region and the walked one. Continue with the next edge tile. Check again if it is contained by the other two regions if not run again the algorithm. As soon you have 3 regions you are done. Compare the 2 unwalked ones figure which one you want to change. For a small map you shouldn't have any proble with performance, although for a large map it will be a bit slow and require more memory. Hope that helps. (Note: There are probably more efficient ways to solve this.)

Comment
Add comment · Show 2 · 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 riotjayne · Dec 23, 2016 at 01:46 AM 0
Share

Thanks so much for the reply PizzaPie, it's definitely given me some new avenues to explore in how to solve this problem. The tutorial you referred to seems to be dealing with exactly what I need, so now I just need to figure out a way to really parse the info presented and apply it to my project, as he seems to be using more advanced techniques all the way through (for example, I'm, I realize stupidly, holding all tiles as GameObjects in a list from 1 - 16000 in consecutive order ins$$anonymous$$d of as X, Y co-ordinates, which would help a great deal).

So for clarification, the steps seem to be:

Create an int array of tile references, and shift them 1 by 1 as the player walks through to create a map during calculation.

Once the player completes a path, run a flood algorithm on the int array to get total tile counts (should return 3 separate batches of tiles). I suppose you could start this at the players last known position, since you'd have a walked tile and then either side of it would be a different region.

Once you have tilecounts of all three regions, grab that range of tiles from initial storage (in my case a list with 16,000 gameobjects) with whatever specs you'd like (smallest) and have them each change tiletype (grass tile) based on that information.

It seems like that would lead to the intended behavior, but I may need to rewrite my initial design. Nevertheless, this seems to be a great way to deal with the problem presented so thank you. If you have any other tips I'd love to hear them, since this is pretty new to me.

avatar image PizzaPie riotjayne · Dec 23, 2016 at 06:54 PM 0
Share

Happy to help. Yeah you got it right. Small tip try to use a single mesh ins$$anonymous$$d of 16k gameObjects would be better for perforance reasons but a bit harder to implement, there are several tutorials for tile map texture generators look around.

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

287 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 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 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 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

Area filling like Paper.io using fllod fill algorithm 0 Answers

Instantiate and object around the perimeter of an area inside of an area. (C#) 1 Answer

How To Get Tile Cell Position 0 Answers

How to Place currently selected tiles in the editor 0 Answers

can i create a grid, using the screen size? 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