Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Ignore collisions with all objects tagged in a string array??

Discussion in 'Scripting' started by Paul-Swanson, Nov 10, 2018 at 9:08 PM.

  1. Paul-Swanson

    Paul-Swanson

    Joined:
    Jan 22, 2014
    Posts:
    160
    I give up I cant get this to work.
    I have an alpha shader called Microsplat that lets me paint holes. I wanted to paint a hole for a subway entry hole. Obviously needed to disable the terrain collider for objects in a specific area and ONLY for certain objects this is what I'v tried:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnterHole : MonoBehaviour
    6. {
    7.     [Tooltip("List of Tags that you want to Ignore Collision for")]
    8.     public string[] entityTagList = {"Bullet","Player","Enemy"};
    9.  
    10.     [Tooltip("Terrain you want to ignore Collisions with")]
    11.     public TerrainCollider terrainSlot;
    12.  
    13.     private Collider c;
    14.  
    15.     void OnTriggerEnter(Collider other)
    16.     {
    17.         foreach (string TagToTest in entityTagList)
    18.         {
    19.             if (other.gameObject.tag == TagToTest)
    20.             {
    21.                 Debug.Log(other.gameObject.tag+" has entered");
    22.                 c = GetComponent<Collider>();
    23.                 Physics.IgnoreCollision(c, terrainSlot, true);
    24.             }
    25.         }
    26.     }
    27.  
    28.     void OnTriggerExit(Collider other)
    29.     {
    30.         foreach (string TagToTest in entityTagList)
    31.         {
    32.             if (other.gameObject.tag == TagToTest)
    33.             {
    34.                 Physics.IgnoreCollision(other, terrainSlot, false);
    35.             }
    36.         }
    37.     }
    38.  
    39. }
    Its not ignoring collisions for any thing that enters. It DOES detect the objects tag that enters the collider. So I know that's working at the very least.

    But I cant get it to yank the colliders :( Id appreciate any help.
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    1,984
    The collision with the trigger has already occurred in order for this code to be called in OnTriggerEnter()
    You cannot go back in time and undo the physics event that happened
     
  3. dara_rw

    dara_rw

    Joined:
    Jul 17, 2018
    Posts:
    1
    If I understand correctly, you try to prevent the collision between the hole and the objects. You should try something like:

    Code (CSharp):
    1.     using System.Collections;
    2.     using System.Collections.Generic;
    3.     using UnityEngine;
    4.    
    5.     public class EnterHole : MonoBehaviour
    6.     {
    7.         [Tooltip("List of Tags that you want to Ignore Collision for")]
    8.         public string[] entityTagList = {"Bullet","Player","Enemy"};
    9.    
    10.         [Tooltip("Terrain you want to ignore Collisions with")]
    11.         public TerrainCollider terrainSlot;
    12.    
    13.         private Collider c;
    14.    
    15.         void OnTriggerEnter(Collider other)
    16.         {
    17.             foreach (string TagToTest in entityTagList)
    18.             {
    19.                 if (other.gameObject.tag == TagToTest)
    20.                 {
    21.                     return;
    22.                 }
    23.             //Add here what should happen if the tag is not included in entityTagList
    24.             }
    25.         }
    26.    }
    27.  
     
  4. Paul-Swanson

    Paul-Swanson

    Joined:
    Jan 22, 2014
    Posts:
    160
    That would be true yes, but I have a trigger volume larger than my actual collider. So that logic isnt quite correct.


    @dara_rw
    What exactly is the return; doing?
    The only thing i want to make happen is it the tags listed in my array are on the object they do not get affected by the terrain collider so long as they are in the trigger volume. I didn't post the on trigger ext part simply because if it doesn't work on enter then there no point having both yet. It will be a temporary state.

    //Add here what should happen if the tag is not included in entityTagList
    I'm not sure I follow here? DO i really need to make a else then do nothing given that i tried to make it sense ONLY objects with my set tags?

    In any case this is what I have that sorta works, I haven't tested it on multiple objects yet, but I suspect that's remaining issue.:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnterHole : MonoBehaviour
    6. {
    7.     [Tooltip("List of Tags that you want to Ignore Collision for")]
    8.     public string[] entityTagList = {"Bullet","Player","Enemy"};
    9.  
    10.     [Tooltip("Terrain you want to ignore Collisions with")]
    11.     public Terrain terrain;
    12.     private TerrainCollider terrainSlot;
    13.     private Collider myCollider;
    14.    
    15.  
    16.     void Start()
    17.     {
    18.         terrainSlot = terrain.GetComponent<TerrainCollider>();
    19.     }
    20.  
    21.     void OnTriggerEnter(Collider other)
    22.     {
    23.         foreach (string TagToTest in entityTagList)
    24.         {
    25.             if (other.gameObject.tag == TagToTest)
    26.             {
    27.                 Debug.Log(other.gameObject.tag+" has entered");
    28.                 myCollider = other.GetComponent<Collider>();
    29.                 Physics.IgnoreCollision(myCollider, terrainSlot, true);
    30.             }
    31.         }
    32.     }
    33.  
    34.     void OnTriggerExit(Collider other)
    35.     {
    36.         foreach (string TagToTest in entityTagList)
    37.         {
    38.             if (other.gameObject.tag == TagToTest)
    39.             {
    40.                 Debug.Log(other.gameObject.tag + " has walked back out");
    41.                 myCollider = other.GetComponent<Collider>();
    42.                 Physics.IgnoreCollision(myCollider, terrainSlot, false);
    43.             }
    44.         }
    45.     }
    46. }
     
    Last edited: Nov 11, 2018 at 11:56 PM