Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Ignoring Collisions Problem

Discussion in 'Scripting' started by blueballsmcgee, Dec 15, 2007.

  1. blueballsmcgee

    blueballsmcgee

    Joined:
    Oct 16, 2007
    Posts:
    9
    I am using the Physics.IgnoreCollision to selectively choose whether or not two objects collide. Now the issue I am running into is that the first moment the two objects interface they interact and then after that they don't. It seems to be speed dependent because it isn't noticable if they collide slowly but the faster they collide the great the effect is. I guess my question is how can I fix this?


    This is the code I am using:

    Code (csharp):
    1.  
    2. function OnCollisionEnter (other : Collision)
    3. {
    4.     if(other.gameObject.tag == "WNonColl")
    5.     {
    6.         Physics.IgnoreCollision(other.collider, collider);
    7.     }
    8. }
    9.  
    10. function OnCollisionStay (other : Collision)
    11. {
    12.     if(other.gameObject.tag == "WNonColl")
    13.     {
    14.         Physics.IgnoreCollision(other.collider, collider);
    15.     }
    16. }
    17.  
    This is just basically checking to see if the tag of the object it is colliding with is "WNonColl." If it is, it ignores the collision otherwise standard physics apply.

    I have a test web player scene attached that will hopefully show you what I mean about this momentary interation. Any thoughts would be greatly appriciated. Thanks in advance!
     

    Attached Files:

  2. blueballsmcgee

    blueballsmcgee

    Joined:
    Oct 16, 2007
    Posts:
    9
    One thing I forgot to mention something that is very important. I am using Unity 1.6.2, sorry about that. Thanks again.
     
  3. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
    Ideally you should be using Physics.IgnoreCollision() before two objects collide. It sets a permanent flag to prevent future collisions (until you call the function again and pass in false).

    You could also add the relative velocity of the collision back onto the object, but really you should ignore collision before anything happens.
     
  4. blueballsmcgee

    blueballsmcgee

    Joined:
    Oct 16, 2007
    Posts:
    9
    Hey thanks Matthew for the response. For some reason that didn't occur to me though thinking about it that is the obvious answer, I just needed that jump start to get things rolling properly. Using that idea and this:

    http://forum.unity3d.com/viewtopic.php?t=7626

    I came to my final solution:

    Code (csharp):
    1.  
    2. var nonCollisions : GameObject[];
    3.  
    4. function Start()
    5. {    
    6.     nonCollisions = GameObject.FindGameObjectsWithTag ("WNonColl");
    7.    
    8.     for (var go : GameObject in nonCollisions)
    9.     {
    10.         Physics.IgnoreCollision(go.collider, collider);
    11.     }
    12. }
    13.  
    Simple and does the trick perfectly. :D