• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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 check if a collision impact is perpendicular with the velocity of the collider? 0 Answers

My marble (Player/Gameobject) goes through the "play board" (gameobject). Why? 1 Answer

Making an object collide but transparent 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges