Search Unity

cancel a collision

Discussion in 'Scripting' started by benblo, Aug 22, 2007.

  1. benblo

    benblo

    Joined:
    Aug 14, 2007
    Posts:
    476
    Hello there!
    Been using Unity for about a month, now starting to toy around with the PhysX engine... ouch.

    So I'm writing some kind 3D pong game; the ball is a rigidbody, the walls are simple cubes with colliders, so are the goals, only they're invisible (mesh renderer disabled).
    I receive the OnCollision event from the "goal" walls, so I can say "the ball's out", easy peacy.
    Problem is, the ball stops on the goals, like it's hitting empty space, and I'd like it to actually cross the invisible goals.

    So I'm wondering if they're such a thing as telling the physx engine to just ignore a collision, while still telling me it happened so I can keep score...?
     
  2. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    You'd probably use triggers instead of normal colliders. Click the appropriate checkbox in the collider component, then change your scripts so they listen for OnTriggerEnter and friends instead of OnCollisionEnter.

    In case you're not familiar with the idea, a trigger is a collider switched into a different mode which makes it non-solid and more like a sensor that detects anything that's inside it. You get messages when objects enter, leave or remain within the collider's volume.
     
  3. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Set the "Is Trigger" property in the inspector and then use the OnTriggerEnter/OnTriggerExit events instead of OnCollision.
     
  4. benblo

    benblo

    Joined:
    Aug 14, 2007
    Posts:
    476
    Perfect, that did it!
    Wow I'm impressed by the Unity community, the response time is great, thanks guys!

    I'm still quite confused on the use of OnCollision/OnTrigger though...
    I have another problem, with a "one-sided wall". I'd like this wall to produce a normal physic rebound from one side, and just let the ball pass from the other side.
    I could use a trigger and ignore it on one side, but then I'd have to manually compute the rebound on the other side. And anyway a trigger gives me a lot less info than a collision, which gives me contact points, normal and stuff. How would I even know which side of the wall I hit with a trigger?

    So... back to the original question, how about a conditional collision :roll: ?
     
  5. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    I would use Physics.IgnoreCollision to turn collisions between the ball and the barrier on or off depending on which side of the barrier the ball was on. Simply put a trigger collider on either side of the barrier. Have one of them turn collisions between the ball and the barrier on, and the other turn them off.
     
  6. benblo

    benblo

    Joined:
    Aug 14, 2007
    Posts:
    476
    Seems exactly what I was looking for, thanks a bunch! I'll have a look tomorrow.
     
  7. benblo

    benblo

    Joined:
    Aug 14, 2007
    Posts:
    476
    Sweet!
    The 2 colliders thing does the trick, whether I use Physics.IgnoreCollision or just enable/disable isTrigger on the wall collider.
    Thanks!