Reid Monahan
Reid Monahan

Reputation: 11

How to invert sphere collider/ spherical mesh in unity 3d

I need to invert a spheres collider in unity and im not smart enough to be able to do it. if any of you help me it would be greatly appreciated.

Upvotes: 0

Views: 2163

Answers (1)

derHugo
derHugo

Reputation: 90872

With the SphereCollider itself this is not possible.

But you could use a MeshCollider and assign a Sphere mesh to it.

Then you can invert all the normals by simply reversing all triangle indices like e.g.

// Linq provides handy shorthand queries and operations for IEnumerable collections
using System.Linq;

...

public MeshCollider meshCollider;

private void Awake ()
{
    if(!meshCollider) meshCollider = GetComponent<MeshCollider>();       

    var mesh = meshCollider.sharedMesh;

    // Reverse the triangles
    mesh.triangles = mesh.triangles.Reverse().ToArray();

    // also invert the normals
    mesh.normals = mesh.normals.Select(n => -n).ToArray();
}

Upvotes: 2

Related Questions