Search Unity

Can you Invert a Sphere or Box Collider??

Discussion in 'Editor & General Support' started by tchpowdog, Jan 9, 2012.

  1. tchpowdog

    tchpowdog

    Joined:
    Nov 25, 2011
    Posts:
    255
    Was wondering if there's a way to invert a Sphere or box collider... in other words, if you have something inside the collider, setting the collider where nothing can get out... Or is there a better and more traditional way to go about doing this??
     
  2. MichaelSchenck

    MichaelSchenck

    Joined:
    Sep 23, 2011
    Posts:
    49
    For the box perhaps make a compound collider where there are six walls one for each face. This can be done by making child gameobjects and putting box colliders on them that are the correct size.
     
  3. tchpowdog

    tchpowdog

    Joined:
    Nov 25, 2011
    Posts:
    255
    I know I can do that I was just wondering if there's a way, bc I'd like to do this with a sphere
     
  4. OrbitusII

    OrbitusII

    Joined:
    Jul 4, 2011
    Posts:
    175
    For the sphere, I would recommend getting a good sphere mesh and changing the scale factor (in the import settings, accessible from the project view of the mesh) to a negative value. This should invert the object, and thus create an inverted mesh collider.

    OrbitusII
     
    Last edited: Jan 15, 2012
  5. andorov

    andorov

    Joined:
    Feb 10, 2011
    Posts:
    1,061
    For the sphere, use an "ico" or "subdivision" sphere, not a "uv" or "polar" sphere.
     
  6. roger_lew

    roger_lew

    Joined:
    Apr 21, 2015
    Posts:
    13
    I realize this is a 3+ year old thread, but I just discovered that you can create an inverted collider by first creating a right-side out Mesh without a collider and then with a Monobehaviour invert the tris and add a MeshCollider.

    Here's a little Editor extension to build an inverted collider. The normals of your MeshRenderer will also be inverted so you might need to use a two-sided shader or duplicate the original mesh depending on what you are after.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Linq;
    4. using System.Collections;
    5.  
    6. public class AddInvertedMeshCollider : MonoBehaviour
    7. {
    8.   public bool removeExistingColliders = true;
    9.  
    10.   public void CreateInvertedMeshCollider()
    11.   {
    12.     if (removeExistingColliders)
    13.       RemoveExistingColliders();
    14.  
    15.   InvertMesh();
    16.  
    17.   gameObject.AddComponent<MeshCollider>();
    18.   }
    19.  
    20.   private void RemoveExistingColliders()
    21.   {
    22.     Collider[] colliders = GetComponents<Collider>();
    23.     for (int i = 0; i < colliders.Length; i++)
    24.       DestroyImmediate(colliders[i]);
    25.   }
    26.  
    27.   private void InvertMesh()
    28.   {
    29.     Mesh mesh = GetComponent<MeshFilter>().mesh;
    30.     mesh.triangles = mesh.triangles.Reverse().ToArray();
    31.   }
    32. }
    33.  
    This goes in an Editor folder.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.Collections;
    5.  
    6. [CustomEditor(typeof(AddInvertedMeshCollider))]
    7. public class AddInvertedMeshColliderEditor :Editor
    8. {
    9.   public override void OnInspectorGUI()
    10.   {
    11.     DrawDefaultInspector();
    12.     AddInvertedMeshCollider script = (AddInvertedMeshCollider)target;
    13.     if (GUILayout.Button("Create Inverted Mesh Collider"))
    14.       script.CreateInvertedMeshCollider();
    15.    }
    16. }
    17.  
    You can remove the AddInvertedMeshCollider component after your mesh has been constructed.

    Cheers!
     
    Zenys, jakenmaest, xaldin-76 and 21 others like this.
  7. JOKaija

    JOKaija

    Joined:
    Feb 8, 2015
    Posts:
    161
    Yep. It was an old thread, but I found this more than usefull for my use. Thanks!
     
  8. Walbert-Schmitz

    Walbert-Schmitz

    Joined:
    Nov 17, 2014
    Posts:
    11
    Sorry for the necromancy of the threat.
    I wanted to use a sphere collider from unity, because AFAIK they are optimized since it's not really a mesh but it calculates based on the radius.
    I need the normals inverted so it can trigger when I shot Raycasts from inside the sphere. I would rather use an inverted "unity - spherecollider" than doing one of my own, since in my head it is optimized. Mine will have lots of polygons, since it it is spherical...
     
  9. FabDynamic

    FabDynamic

    Joined:
    Jul 31, 2017
    Posts:
    36
    Hey Roger_Lew! I made a video using your code and it worked perfectly! Cheers to you roger_lew!
     
  10. dorukeker

    dorukeker

    Joined:
    Dec 6, 2016
    Posts:
    37
    Just came across this thread. So here is my solution for the sphere:
    - Create a sphere mesh with inverted normals
    - Apply the mesh collider on it

    Since the normals of the sphere is inverted the collider will be inverted as well.
    Hope this helps someone.
    Cheers,
    D.
     
  11. Fragmental

    Fragmental

    Joined:
    Jun 30, 2013
    Posts:
    61
    dvalles likes this.
  12. DarkGate

    DarkGate

    Joined:
    Jan 26, 2016
    Posts:
    33
    There is an easy way to do it if you use ProBuilder(which is a part of unity now). It might be computationally a bit more expensive.

    1. Install ProBuilder via package manager
    2. Open the ProBuilder window and click on ProBuilderize
    3. Select all faces and click on Flip Normals
    4. Remove existing collider and add Mesh Collider

    Voila! Hope that helps.
     
  13. MellowMammoth

    MellowMammoth

    Joined:
    Aug 14, 2019
    Posts:
    1
    This is still going strong in 2019, thanks for your contribution Roger_Lew!
     
    Issun likes this.
  14. Anax7

    Anax7

    Joined:
    Nov 23, 2012
    Posts:
    13
    Hey guys, welcome to 2020!

    I'm trying to do this on a game with 2D colliders and using a plane as a bounding box but it does not work. Mesh collider does not seem to interact with my polygon 2D colliders, anyone has a any suggestions on how I can fix this?
     
  15. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,332
    Mesh Colliders are 3D only. You'll have to convert the script to do a similar thing, but with 2D colliders instead.
     
  16. Anax7

    Anax7

    Joined:
    Nov 23, 2012
    Posts:
    13
    Thanks Baste, it turns out I was a little bit un-educated. There is the Edge Collider 2D ideal for this job.
     
  17. JDeyby

    JDeyby

    Joined:
    Sep 14, 2017
    Posts:
    5
    Hello, You just need to model your object in Blender and invert your normals, then import to Unity and the collider will be inverted too.

    Luck Annotation 2020-04-29 155732.png Annotation 2020-04-29 160003.png Annotation 2020-04-29 155747.png
     
  18. berk_can

    berk_can

    Joined:
    Feb 8, 2016
    Posts:
    15
    WOW amazing thank you so much it is so cool Unity should make these built in
     
  19. CHEMISTRA

    CHEMISTRA

    Joined:
    Apr 3, 2020
    Posts:
    1
    Ok I know this thread are 8 years old already,
    But in Unity 2019.3.7f1, the Probuilder has a Set Collider tool. You can flip the normals and use Set Collider
    This removes the mesh renderer, so you may want to duplicate it, one for the visuals, and one for the collider...