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 /
  • Help Room /
avatar image
0
Question by matteosspam · Sep 06, 2020 at 06:14 PM · rigidbody2dcollider2d

Detecting Adjacent Provinces using Collision

Hello. First time posting so apologies if I'm in the wrong place. So I have a world map with a lot of provinces. Each province is an individual Gameobject with Polygon Collider 2D and Rigidbody 2D (static). Have set all the colliders so they overlap with neighboring provinces like so: alt text

Am storing data in each province Gameobject. Currently am trying to store a Gameobject list of all adjacent provinces in each province (adjacency determined by collision). Code works for storing all other information so far, but the collisions between the provinces aren't being detected. This might be because the provinces are instantiated already overlapping/colliding.

alt text

Anyone have any idea how I can get this to work?

adsfae.png (243.8 kB)
asdfasf.png (19.2 kB)
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
0
Best Answer

Answer by streeetwalker · Sep 06, 2020 at 07:50 PM

@matteosspam, yes, in my experience that is correct. If items are instantiated with overlapping colliders, OnCollisonEnter does not fire. However, OnCollisionStay should.

But why not just create a public array or list of adjacent provinces and author references to them in the inspector of each province? Or, probably even better yet, put references to all your provinces into a array, and reference the indices of each adjacent province in another array that each respective province holds.

It seems you could avoid the hassle using one of those methods, or something similar.

Comment
Add comment · Show 10 · 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 matteosspam · Sep 07, 2020 at 01:45 PM 0
Share

Hi, thanks for your response. Could you give me an example of how OnCollisionStay2D could be used like this? I've been trying to get it to work and haven't been able to figure out how to use it to add to a Gameobject's list.

As for your suggestion, I might be misunderstanding what you're recommending, but there are a couple hundred provinces, so I was looking for a way to $$anonymous$$imize the amount of work I'd have to do by hand, hence the CSVReader and such. Just placing them all by hand was already a pain. At this point though I might just have to suck it up and do it.

avatar image streeetwalker matteosspam · Sep 07, 2020 at 02:25 PM 0
Share

@matteosspam, sorry I didn't catch that you were reading province data from a CSV file. I'm not familiar with the code for CSVReader - is this the one from this source? https://bravenewmethod.com/2014/09/13/lightweight-csv-reader-for-unity/

Then potentially your work is easier, because you can author the adjacency lists into the CSV file, and then populate each province's adjacency list from that. I think in the long run this is going to be easier than trying to do what you are doing with colliders.

Are you creating the CSV in some texteditor or Excel or Word and each line in the CSV represents a province?

If so, let each line number represent the index of the province. For example, the data for province 0 is the 1st line in the CSV and so on (or if you have a header line with the dictionary keys as strings, then the first province is the 2nd line). Because, you are reading the data for each province into a list of dictionaries, you can access that data using privinceData[index].

Therefore, you need one field in each line of the CSV to encode an array of the indexes adjacent provinces. You can encode that as a string using some delimiter that is not going to be parsed by CSVReader. You will have to parse it yourself when you want to populate the array that represents the adjacent provinces for any given province.

Or you could modify CSV reader to create the array. Either way, you can also store that in the dictionary, because it looks like your dictionary stores its data as generic objects...

All of this is not difficult to do, but to explain it gets complex because there are several parts to it. Before I try to go further, can you provide a sample of one province's data that you have in your CSV, along with the field names/ids that become the dictionary keys for a given province's data?

avatar image matteosspam streeetwalker · Sep 07, 2020 at 02:40 PM 0
Share

Yes, I am using that exact CSVReader. It's currently reading in the values from the CSV and converting them into int, bool, or strings, depending on what they are.

Here's a few lines from the CSV. alt text

Here's an example of what a province's data looks like in the inspector.

alt text

adj.png (20.0 kB)
cindfe.png (32.1 kB)
Show more comments
Show more comments
avatar image streeetwalker · Sep 07, 2020 at 04:54 PM 0
Share

@matteosspam - the sample CSV lines I wrote didn't format properly, so I edited it to put a line break after my sample header row.

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

213 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

Related Questions

A box collider 2D (Is Trigger marked) stops my player from moving which has rigidbody 2D 1 Answer

Adding rotate-to-mouse code causes issues with Relative Joint 1 Answer

how avoided this among my player and the wall? 0 Answers

All colliders not longer working 0 Answers

2D Colliders don't actually touch, causing problems 0 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