Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Ignoring collisions between a prefab and a child collider (cannot get it to work)

Discussion in 'Scripting' started by big_stoater, Feb 23, 2020.

  1. big_stoater

    big_stoater

    Joined:
    Jun 25, 2014
    Posts:
    13
    Hi all,

    I'm having an issue with ignoring collisions on two objects with Physics.IgnoreCollision.

    I have a player ship that can pickup bombs and then drop them again.

    When I drop the bomb I want the child collider (bottom of ship) to temporarily ignore the bomb collider (which is attached under the ship after picking it up).

    In Void Start() I loop through the Player ship colliders and find the child I want and assign this a reference:

    Code (CSharp):
    1. colliders = GetComponentsInChildren<Collider>();
    2.         foreach (Collider col in colliders)
    3.         {
    4.             if (col.name == "BottomCollider")
    5.             {
    6.                 shipBottomCollider = col;
    7.             }
    8.         }
    Once the player has the bomb picked up, the B key can be pressed to drop the bomb, and it's here I want to ignore the collisions between the bomb and the ship. When the B key is pressed the bomb drops momentarily the attaches itself to the player ship again:

    Code (CSharp):
    1. private void DropBomb()
    2.     {
    3.         if(Input.GetKeyDown(KeyCode.B))
    4.         {
    5.             if (hasBomb)
    6.             {
    7.                 bombDummy.SetActive(false);
    8.                 GameObject bombDrop = Instantiate(bombPrefab, bombDummy.transform.position, bombDummy.transform.rotation);        
    9.                 uiManager.RemoveBombs(1);
    10.                 Physics.IgnoreCollision(bombDrop.GetComponent<Collider>(), shipBottomCollider);      
    11.                 hasBomb = false;
    12.             }
    13.             else
    14.             {
    15.                 //play empty bomb slot
    16.             }
    17.            
    18.         }
    19.     }
    20.  
    Any pointers greatly appreciated!
     
  2. big_stoater

    big_stoater

    Joined:
    Jun 25, 2014
    Posts:
    13
    Got it sorted, so for anyone else who has this issue, I changed ‘GameObject bombDrop’ to ‘Transform bombDrop’ and added ‘as Transform’ at the end of the instantiate line.
     
  3. ATLGAN

    ATLGAN

    Joined:
    Oct 15, 2017
    Posts:
    10
    If you don't want the bombs to collision later, you can adjust it in the `ProjectSettings/Physics/LayerCollisionMatrix` settings.
     
  4. big_stoater

    big_stoater

    Joined:
    Jun 25, 2014
    Posts:
    13
    Thanks for the tip, I actually only want to ignore the collisions temporarily so I've added an Invoke with IgnoreCollison set to False after one second. This way the ship can pick the bomb up again after its been dropped.

    Cheers
     
    ATLGAN likes this.