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??
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.
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
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
For the sphere, use an “ico” or “subdivision” sphere, not a “uv” or “polar” sphere.
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.
using UnityEngine;
using System.Linq;
using System.Collections;
public class AddInvertedMeshCollider : MonoBehaviour
{
public bool removeExistingColliders = true;
public void CreateInvertedMeshCollider()
{
if (removeExistingColliders)
RemoveExistingColliders();
InvertMesh();
gameObject.AddComponent<MeshCollider>();
}
private void RemoveExistingColliders()
{
Collider[] colliders = GetComponents<Collider>();
for (int i = 0; i < colliders.Length; i++)
DestroyImmediate(colliders[i]);
}
private void InvertMesh()
{
Mesh mesh = GetComponent<MeshFilter>().mesh;
mesh.triangles = mesh.triangles.Reverse().ToArray();
}
}
This goes in an Editor folder.
using UnityEngine;
using UnityEditor;
using System.Collections;
[CustomEditor(typeof(AddInvertedMeshCollider))]
public class AddInvertedMeshColliderEditor :Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
AddInvertedMeshCollider script = (AddInvertedMeshCollider)target;
if (GUILayout.Button("Create Inverted Mesh Collider"))
script.CreateInvertedMeshCollider();
}
}
You can remove the AddInvertedMeshCollider component after your mesh has been constructed.
Cheers!
Yep. It was an old thread, but I found this more than usefull for my use. Thanks!
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…
Hey Roger_Lew! I made a video using your code and it worked perfectly! Cheers to you roger_lew!
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.
I used the script by @roger_lew and it works great. Thanks! However, in 2018.1 I get this error message “Instantiating mesh due to calling MeshFilter.mesh during edit mode. This will leak meshes. Please use MeshFilter.sharedMesh instead.” I don’t know if this is a serious error or something i can ignore. I may have found a solution here Alter Meshes in Editor by Script without warning - Questions & Answers - Unity Discussions , but I haven’t bothered testing it.
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.
- Install ProBuilder via package manager
- Open the ProBuilder window and click on ProBuilderize
- Select all faces and click on Flip Normals
- Remove existing collider and add Mesh Collider
Voila! Hope that helps.
This is still going strong in 2019, thanks for your contribution Roger_Lew!
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?
Mesh Colliders are 3D only. You’ll have to convert the script to do a similar thing, but with 2D colliders instead.
Thanks Baste, it turns out I was a little bit un-educated. There is the Edge Collider 2D ideal for this job.
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
WOW amazing thank you so much it is so cool Unity should make these built in
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…