Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm stumped as to why (sometimes) when I apply a Mesh Collider to a procedurally create mesh, it is sometimes completely correct, but other times completely inaccurate.

In Detail:

I'm creating a procedural mesh (effectively a trapezoid shape), where like a camera, I create a 'near clip plane', then figure out where the 'far clip plane' points should be based on desired vertical angles, desired horizontal angles, and desired depth.

The mesh itself is always correct (and debugging the building portion of the mesh shows all the correct points and angles. The problem is that sometimes, the mesh collider can be somewhat inaccurate or even totally inaccurate once it gets applied.

It seems the issues can occur with when certain changes to the input parameters are changed, like greatly increasing the depth for a given setting, or making the near clip plane smaller for a given setting. So either changing the angles, near clip plane size, or depth can make this happen.

I'm pretty sure I always end up with a convex shape, so I'm not sure why this is happening and have no clue on how to fix it. I am more than happy to attach any code that you think might be helpful to figuring this out and hopefully the attached pictures will illustrate what I'm talking about. Any ideas as to what is happening are greatly appreciated, thank you for reading.

I've also made a quick example that you should be able to plug in and see. If you change the Z distance from 500 to 1000, you will see the type of failure I'm experiencing.

using UnityEngine;

using System.Collections; using System.Linq;

public class zTest_CubeMaker : MonoBehaviour {

public Material mat;

void Start()
{
    MeshFilter filter = gameObject.AddComponent< MeshFilter >();
    Mesh mesh = filter.mesh;
    mesh.Clear();

    #region Vertices
    //bottom
    Vector3 p0 = new Vector3(-0.5f, 0f, 0f); //bottom left close
    Vector3 p1 = new Vector3(0.5f, 0f, 0f); //bottom right close
    Vector3 p2 = new Vector3(20f, -25f, 500f); //bottom right far
    Vector3 p3 = new Vector3(-20f, -25f, 500f); //bottom right far

    //top
    Vector3 p4 = new Vector3(-0.5f, 1f, 0f); //top left close
    Vector3 p5 = new Vector3(0.5f, 1f, 0f); //top right close
    Vector3 p6 = new Vector3(20f, 200f, 500f); //top right far
    Vector3 p7 = new Vector3(-20f, 200f, 500f); //top left far

    Vector3[] vertices = new Vector3[]
    {
        // Bottom
        p0, p1, p2, p3,

        // Left
        p7, p4, p0, p3,

        // Front
        p4, p5, p1, p0,

        // Back
        p6, p7, p3, p2,

        // Right
        p5, p6, p2, p1,

        // Top
        p7, p6, p5, p4
    };
    #endregion

    #region Normales
    Vector3 up  = Vector3.up;
    Vector3 down    = Vector3.down;
    Vector3 front   = Vector3.forward;
    Vector3 back    = Vector3.back;
    Vector3 left    = Vector3.left;
    Vector3 right   = Vector3.right;

    Vector3[] normales = new Vector3[]
    {
        // Bottom
        down, down, down, down,

        // Left
        left, left, left, left,

        // Front
        front, front, front, front,

        // Back
        back, back, back, back,

        // Right
        right, right, right, right,

        // Top
        up, up, up, up
    };
    #endregion  

    #region UVs
    Vector2 _00 = new Vector2( 0f, 0f );
    Vector2 _10 = new Vector2( 1f, 0f );
    Vector2 _01 = new Vector2( 0f, 1f );
    Vector2 _11 = new Vector2( 1f, 1f );

    Vector2[] uvs = new Vector2[]
    {
        // Bottom
        _11, _01, _00, _10,

        // Left
        _11, _01, _00, _10,

        // Front
        _11, _01, _00, _10,

        // Back
        _11, _01, _00, _10,

        // Right
        _11, _01, _00, _10,

        // Top
        _11, _01, _00, _10,
    };
    #endregion

    #region Triangles
    int[] triangles = new int[]
    {
        // Bottom
        3, 1, 0,
        3, 2, 1,            

        // Left
        3 + 4 * 1, 1 + 4 * 1, 0 + 4 * 1,
        3 + 4 * 1, 2 + 4 * 1, 1 + 4 * 1,

        // Front
        3 + 4 * 2, 1 + 4 * 2, 0 + 4 * 2,
        3 + 4 * 2, 2 + 4 * 2, 1 + 4 * 2,

        // Back
        3 + 4 * 3, 1 + 4 * 3, 0 + 4 * 3,
        3 + 4 * 3, 2 + 4 * 3, 1 + 4 * 3,

        // Right
        3 + 4 * 4, 1 + 4 * 4, 0 + 4 * 4,
        3 + 4 * 4, 2 + 4 * 4, 1 + 4 * 4,

        // Top
        3 + 4 * 5, 1 + 4 * 5, 0 + 4 * 5,
        3 + 4 * 5, 2 + 4 * 5, 1 + 4 * 5,

    };
    #endregion

    mesh.vertices = vertices;
    mesh.normals = normales;
    mesh.uv = uvs;
    mesh.triangles = triangles;
    mesh.triangles = mesh.triangles.Reverse().ToArray();

    mesh.RecalculateBounds();
    mesh.Optimize();

    MeshRenderer mRender = gameObject.AddComponent<MeshRenderer>();
    mRender.material = mat;

    MeshCollider mCollide = gameObject.AddComponent<MeshCollider>();
    mCollide.convex = true;
}

}

enter image description here

enter image description here

enter image description here

enter image description here

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.