Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

How to get Collider for Physics.IgnoreCollision?

Discussion in 'Physics' started by bovicafize, Feb 13, 2019.

  1. bovicafize

    bovicafize

    Joined:
    Jan 31, 2019
    Posts:
    2
    Hi, I'm new to Unity but just inherited a Unity project, apologies if this is a dumb question, I couldn't find the specific answer in the forums already.

    I'm trying to solve the pretty common problem of having a bullet ignore collisions with the gun it was shot from. The existing "solution" in this project is to do string compares in OnCollisionEnter on the names of the objects.

    I could solve this by putting the guns and bullets on different collision layers that don't collide, but we also plan on supporting detecting when bullets legitimately hit guns. Collision layers are great, but they aren't a scalable solution to this specific problem. (and the existing string compare solution also fails at this in addition to being inefficient).

    The consensus on forums seems to be to use Physics.IgnoreCollison and pass the colliders of the bullet and the weapon that fired it. However, GetComponent<Collider> always returns null on both the bullet and weapon. I have tried calling GetComponent<Collider> both from where Instantiate is called to create the bullet and also from the Start method on the MonoBehavior attached to the bullet. It's always null.

    Note that OnCollisionEnter *is* getting called with the correct objects, and at that point the Collision parameter has a valid Collider. I just want to know how to get ahold of the Collider before OnCollisionEnter gets called.

    In the Unity editor, I did notice that bullet and gun objects don't have explicit Collider components attached to them. Is this why their colliders don't exist in code? How does OnCollisionEnter get called in the first place, and why is there a valid Collider passed to it? They do have RigidBodies.

    Thanks for any insight on this. Unity is still largely a black box to me.
     
  2. razzraziel

    razzraziel

    Joined:
    Sep 13, 2018
    Posts:
    396
    i dont know what kind of bullet you're talking about but shooting bullets with colliders is bad idea. moving colliders are expensive and also calculations of speedy objects would be wrong. because physics doesnt calculated per frame.

    using raycast is better solution.
     
  3. bovicafize

    bovicafize

    Joined:
    Jan 31, 2019
    Posts:
    2
    For context, raycasts would not quite work because these are slow-motion bullets that ricochet (and in some cases are heavier projectiles that arc). They do need to be objects that move. I apologize if the term "bullet" was confusing here.

    These projectiles don't have colliders, which is the source of the problem (I can't get a collider for the bullet using GetComponent<Collider>). OnCollisionEnter is still getting called, and when it does it provides a valid Collider under the Collision parameter. Can you explain to me how OnCollisionEnter could get called and provide a Collider for an object that doesn't have a Collider to begin with?