Search Unity

Getting all applied collisions on an object

Discussion in 'Scripting' started by samafshar, Sep 9, 2011.

  1. samafshar

    samafshar

    Joined:
    Sep 9, 2011
    Posts:
    15
    Hello friends.
    I need to access to all collisions applied to a game object outside of OnCollisionStay function.
    I've created a collision array and stored all collisions in it in OnCollisionStay function.
    But i think it's not a good solution because removing them in next run is hard and maybe make lots of garbage.
    Is there any other solutions to do this?
     
  2. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    Of course I don't know the details, but if you want to store them, add in OnCollisionEnter and remove in OnCollisionExit. No need for OnCollisionStay.
     
  3. samafshar

    samafshar

    Joined:
    Sep 9, 2011
    Posts:
    15
    Thanks Jasper. But it doesn't work.
    When an object make a collision to my own object, it adds a collision to list.
    But in debug mode when i move my own object touching that one, no new collision with new contact is added to the list.
    Same old contact stays in the list.
    When my object stop touching and retouch again, a new contact (collision) is added and old one doesn't be removed.
    Here is my code:

    Code (csharp):
    1.  
    2.     List<Collision> collisions = new List<Collision>();
    3.  
    4.     void OnCollisionEnter(Collision collision)
    5.     {
    6.         if (!collisions.Contains(collision))
    7.             collisions.Add(collision);
    8.     }
    9.  
    10.     void OnCollisionExit(Collision collision)
    11.     {
    12.         if (collisions.Contains(collision))
    13.             collisions.Remove(collision);
    14.  
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         foreach (Collision collision in collisions)
    20.         {
    21.             foreach (ContactPoint contact in collision.contacts)
    22.             {
    23.                 Debug.DrawRay(contact.point, contact.normal, Color.blue);
    24.             }
    25.         }
    26.     }
    27.  
    I also removed "if (collisions.Contains(collision))" lines but it again doesn't work.
    If you please test this code, you do me a favor.
    Thanks.
     
  4. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    I assumed you wanted to keep track of each gameobject that's touching (collisions are unique per event). But it seems you want to keep a record of all contact points over time (which are a lot). Is that correct?
     
  5. samafshar

    samafshar

    Joined:
    Sep 9, 2011
    Posts:
    15
    I just want to store all contacts to my object in an array and use it in current game update or next. The array must be refreshed in every frame. I want to use this array in other methods. There won't be too much existing game objects in one frame. Max 50.
     
  6. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    If you want to keep track of the latest collisions, you'll have to empty the list each physics update and then fill it again on OnColissionEnter and OnCollisionStay. Or perhaps accumulate every collision's data, draw them all in Update, and then clear the list. Alternatively, raise a flag in Update so that only the next physics update collects collisions instead of them all. Which approach is acceptable depends on your exact needs.
     
  7. samafshar

    samafshar

    Joined:
    Sep 9, 2011
    Posts:
    15
    Ok. Good ideas. I'll try these. Thank you very much Jasper for helping.