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 ceaston7 · Feb 27, 2019 at 08:55 PM · collisiongameobjectgameobjectscollisionsdictionary

Dictionary with GameObject as key is updating values under other keys

I'm trying to see if an object has collisions on opposite sides, suggesting it is pinned inbetween two other objects. In order to do this, I'm maintaining a dictionary with GameObject keys and Collision values, then going through all combinations of Collisions and getting the angle between their contact normals.

 private Dictionary<GameObject, Collision> collisions;
 private List<GameObject> gameObjects;
 
 void OnCollisionEnter(Collision collision){
 
     if (collisions.ContainsKey(collision.gameObject)){
         collisions[collision.gameObject] = collision;
     }
     else{
         collisions.Add(collision.gameObject, collision);
     }
     gameObjects.Add(collision.gameObject);
 }
 
 void OnCollisionStay(Collision collision){
     float angle = 0;
     if (collisions.ContainsKey(collision.gameObject)){
         collisions[collision.gameObject] = collision;
     }
     else{
         collisions.Add(collision.gameObject, collision);
     }
         //Step through the list of GameObjects to get all combinations of Collisions
         //Only get first contact from Collision for current iteration of code, make robust later
         for (int i = 0; i < gameObjects.Count; i++){
             for (int j = i + 1; j < gameObjects.Count; j++){
                 angle = Vector3.Angle(collisions[gameObjects[i]].GetContact(0).normal, collisions[gameObjects[j]].GetContact(0).normal);
 
                  if (angle > 170 && angle < 190){
                    isPinched = true;
                    return;
                  }
             }
         }
 }

The problem I'm having is that when I check the normals that are being compared in OnCollisionStay, they are all the same. Specifically, it seems like all values in the Dictionary are being overwritten by the most recent Collision. Am I storing a reference to the Collision that is being overwritten each time OnCollisionStay occurs? Am I missing something very obvious? I've been working at this for days without any luck.

Thank you.

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 ceaston7 · Mar 03, 2019 at 12:37 AM

From some testing, it appears that storing the Collision as a value stores a reference to an object that is constantly being overwritten. So instead I used a Dictionary structure and store the contacts array from the collision.

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

186 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

Related Questions

If Check Colliding GameObject Variable 1 Answer

unity2D colliders not working properly,,Unity2D Colliders don't seem to be working HELP PLEASE! 0 Answers

How to move a Game Object from a script not attached to it. 1 Answer

collisions in character not working! 2 Answers

Issue with GameObject has been destroyed but it is not destroyed 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