Search Unity

Question about 2D alternative of mesh collider?

Discussion in '2D' started by Luchunpen_0, Sep 16, 2014.

  1. Luchunpen_0

    Luchunpen_0

    Joined:
    Aug 7, 2014
    Posts:
    58
    Hi community, sorry for my bad English ^).
    I have some trouble with collider generating.

    When I generated 3D mesh, I use MeshCollider for collision detection. That the MeshCollider takes the shape of visual mesh use this code like this:

    meshCollider.sharedMesh = null;
    meshCollider.sharedMesh = visualMesh;
    and can take mesh like this (it is one Mesh and MeshCollider):



    But MeshCollider not working with 2D colliders. I tried to use the PolygonCollider and there was problem - PolygonCollider joins all points and it turns out the wrong form.

    How to create 2D Mesh Collider as in the picture ?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Use a PolygonCollider2D. It can have multiple separate paths; see the docs (specifically SetPath).

    --Eric
     
  3. Shadeless

    Shadeless

    Joined:
    Jul 22, 2013
    Posts:
    136
    Are there any code examples of how to generate a PolygonCollider2D programmatically?
     
  4. Luchunpen_0

    Luchunpen_0

    Joined:
    Aug 7, 2014
    Posts:
    58
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Use .pathCount to set the number of paths, and then call SetPath as many times as needed with the appropriate index value and Vector2[] array each time.

    --Eric
     
  6. Luchunpen_0

    Luchunpen_0

    Joined:
    Aug 7, 2014
    Posts:
    58
    Thank's Eric, I'll try it.
     
  7. Luchunpen_0

    Luchunpen_0

    Joined:
    Aug 7, 2014
    Posts:
    58
    New Question:
    I have Vector2[] array with all points of mesh shape. How can I find the correct index what I need and create new path?

    Official documentation is very poor, Is there a more detailed documentation with code examples, how use this, or other source ?
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That's not what you want; you need multiple Vector2[] arrays. Each Vector2[] array should be a separate shape. For the image you posted at the top of this thread, for example, you would need three Vector2[] arrays.

    SetPath is dead simple...it just takes an index, and a Vector2[] array. The Vector2[] array is a list of consecutive points in the collider, and that's all there is to it. If you have just one path, then the index would be 0.

    Code (javascript):
    1. var points = [Vector2(0, .5), Vector2(.5, 0), Vector2(0, 0)]; // A triangle
    2. myCollider.SetPath (0, points);
    --Eric
     
  9. Luchunpen_0

    Luchunpen_0

    Joined:
    Aug 7, 2014
    Posts:
    58
    That's not what you want; you need multiple Vector2[] arrays. Each Vector2[] array should be a separate shape. For the image you posted at the top of this thread, for example, you would need three Vector2[] arrays.

    Ok. For my example (sorry, I cant find bbcode for formating the script code):
    I created simple class instead multiple Arrays with Vector2 array.

    using UnityEngine;
    using System.Collections;
    using System;

    [Serializable]
    public class MeshPoints {

    [SerializeField]
    public Vector2[] points;
    }

    In Mesh class I created List of this class type:
    public List<MeshPoints> points = new List<MeshPoints> (); //my points Arrays

    In each MeshPoint add 4 points (that will be part of Polygon Collider).
    Then I touch all my MeshPoints list:

    for (int p = 0; p < points.Count; p++) {
    meshCollider.SetPath(0, points[p].points);
    }

    and take this (I create 2 quad): First quad - no collider, last quad - has collider. If I created more quad - take same, the last quad has collider, others do not.