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
1
Question by ina · Oct 08, 2013 at 08:25 AM · dictionary

Dictionary with Vector3 as key cannot be child gameobjects

It appears that there are weird rounding errors if there is a dictionary with keys being Vector3 representing positions of gameobjects that are children of another gameobject.

For such a situation, when you need to check if a hit gameobject is in the dictionary (by position), is there another solution other than to de-parent?


Steps:

  1. Create some pooled gameobjects that are children of a parent object.

  2. Loop through parent transform GetChild to get child objects, and assign each child objet's position as key to the dictionary

  3. Try a raycasthit on any of the child gameobject positions, to see if they are in the dictionary. None will show up!

But, the above works as expected if they are not parented.

Comment
Add comment · Show 2
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 ina · Oct 08, 2013 at 08:30 AM 0
Share

Note, assigning parent null, then reassigning old parent does not work

avatar image s_guy · Oct 08, 2013 at 08:43 AM 0
Share

Can you be more explicit about what you're doing and when it fails (or add more details)?

I've had good luck with transform.position of pre-placed editor objects being a struct member, and that struct being a dictionary key.

Please describe the circumstances where it seems that rounding issues may be co$$anonymous$$g into play and what makes it seem that that parenting or not is relevant.

2 Replies

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

Answer by ArkaneX · Oct 08, 2013 at 08:54 AM

I guess the problem might be related to float numbers rounding, e.g. the Vector3 which is a key might be (1,1,1), while the vector you're using to retrieve value might be (1,1,0.99999999999999999). When checking for existence of key (`dic.ContainsKey(key)`), or when retrieving value by key (`dic[key]`), Equals(object) method is used, which checks if x, y, and z are exactly the same.

Depending on the number of children, you can try iterating through dictionary, checking keys for equality using == operator. This operator for Vector3 is overloaded, so if two vectors differs slightly, they are treated as equal.

Comment
Add comment · Show 3 · 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 ina · Oct 08, 2013 at 06:43 PM 0
Share

So there is no way to just reference it by dictionary[value]?

avatar image ArkaneX · Oct 09, 2013 at 07:18 AM 0
Share

I'm afraid not. You can sometimes be able to retrieve the value this way, but not in all cases.

In rare cases, hash codes of slightly different vectors might be the same, but ultimately Equals method will return false.

avatar image mateov951 · May 08, 2020 at 06:32 AM 0
Share

You sir just saved me a lot of time! Thanks for your clear explanation. $$anonymous$$y game is one step closer to being complete thanks to you. I was starting to lose faith after googling more than 2 hours for possible reasons for my bug and found this answer! The workaround was to use Vector3Int for comparing it to the dictionary key with TryGetValue.

avatar image
5

Answer by IgorAherne · Aug 02, 2017 at 02:30 PM

if you are in situation where you need a dictionary with Keys as Vector3

then, you can create a dictionary with a custom IEqualityComparer:

 class Vector3CoordComparer : IEqualityComparer<Vector3> {
     public bool Equals(Vector3 a, Vector3 b) {
        if (Mathf.Abs(a.x - b.x) > 0.001) return false;
        if (Mathf.Abs(a.y - b.y) > 0.001) return false;
        if (Mathf.Abs(a.z - b.z) > 0.001) return false;
 
         return true; //indeed, very close
     }
 
     public int GetHashCode(Vector3 obj) {
         //a cruder than default comparison, allows to compare very close-vector3's into same hash-code.
         return Math.Round(obj.x, 3).GetHashCode() 
              ^ Math.Round(obj.y, 3).GetHashCode() << 2 
              ^ Math.Round(obj.z, 3).GetHashCode() >> 2;
     }

}

 public static Vector3CoordComparer  _customComparer = new Vector3CoordComparer ();
 public Dictionary<Vector3, someValues> coord_to_values = new Dictionary(_customComparer); //notice, the argument


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 Xyotic · Feb 13, 2018 at 06:45 PM 0
Share

This solved my problem and the code works perfectly! Thanks a lot!

avatar image Bizmut420 · Feb 03, 2020 at 07:33 PM 0
Share

Thank you very much

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

20 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

Related Questions

Searching through a text file with Unity? 4 Answers

[JS] String,List dictionary doesn't compile. Any suggestions? 1 Answer

error CS1526: A new expression requires () or [] after type 1 Answer

Better Dictionary? (serializable with more functionality) 1 Answer

Dictionairy Serialization Problem: Keys and values get lost 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