Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Mesh collider not working

Discussion in 'Scripting' started by CoopReed, Dec 29, 2015.

  1. CoopReed

    CoopReed

    Joined:
    Nov 17, 2015
    Posts:
    27
    Hey Guys I have been following the Procedural Cave generation tutorial and have just finished it. I added my player and now When I play I move my character around and the collider do not work? It does not even recoginise as a trigger, I want it to be a trigger and when I press to make it a trigger it only happens ingame, is there anyway to make it a trigger ingame? I will put my source code below.

    Thankyou!

    Mesh Generator Script
    Code (CSharp):
    1.  void CreateWallMesh() {
    2.  
    3.         CalculateMeshOutlines ();
    4.  
    5.         List<Vector3> wallVertices = new List<Vector3> ();
    6.         List<int> wallTriangles = new List<int> ();
    7.         Mesh wallMesh = new Mesh ();
    8.         float wallHeight = 5;
    9.  
    10.         foreach (List<int> outline in outlines) {
    11.             for (int i = 0; i < outline.Count -1; i ++) {
    12.                 int startIndex = wallVertices.Count;
    13.                 wallVertices.Add(vertices[outline[i]]); // left
    14.                 wallVertices.Add(vertices[outline[i+1]]); // right
    15.                 wallVertices.Add(vertices[outline[i]] - Vector3.up * wallHeight); // bottom left
    16.                 wallVertices.Add(vertices[outline[i+1]] - Vector3.up * wallHeight); // bottom right
    17.  
    18.                 wallTriangles.Add(startIndex + 0);
    19.                 wallTriangles.Add(startIndex + 2);
    20.                 wallTriangles.Add(startIndex + 3);
    21.  
    22.                 wallTriangles.Add(startIndex + 3);
    23.                 wallTriangles.Add(startIndex + 1);
    24.                 wallTriangles.Add(startIndex + 0);
    25.             }
    26.         }
    27.         wallMesh.vertices = wallVertices.ToArray ();
    28.         wallMesh.triangles = wallTriangles.ToArray ();
    29.         walls.mesh = wallMesh;
    30.  
    31.         MeshCollider wallCollider = walls.gameObject.AddComponent<MeshCollider> ();
    32.         wallCollider.sharedMesh = wallMesh;
    33.     }
    34.  
    35. This is the Collider Script
    36.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class MeshGenerator : MonoBehaviour {
    6.  
    7.     public SquareGrid squareGrid;
    8.     public MeshFilter walls;
    9.     public MeshFilter cave;
    10.  
    11.     public bool is2D;
    12.  
    13.     List<Vector3> vertices;
    14.     List<int> triangles;
    15.  
    16.     Dictionary<int,List<Triangle>> triangleDictionary = new Dictionary<int, List<Triangle>> ();
    17.     List<List<int>> outlines = new List<List<int>> ();
    18.     HashSet<int> checkedVertices = new HashSet<int>();
    19.  
    20.     public void GenerateMesh(int[,] map, float squareSize) {
    21.  
    22.         triangleDictionary.Clear ();
    23.         outlines.Clear ();
    24.         checkedVertices.Clear ();
    25.  
    26.         squareGrid = new SquareGrid(map, squareSize);
    27.  
    28.         vertices = new List<Vector3>();
    29.         triangles = new List<int>();
    30.  
    31.         for (int x = 0; x < squareGrid.squares.GetLength(0); x ++) {
    32.             for (int y = 0; y < squareGrid.squares.GetLength(1); y ++) {
    33.                 TriangulateSquare(squareGrid.squares[x,y]);
    34.             }
    35.         }
    36.  
    37.         Mesh mesh = new Mesh();
    38.         cave.mesh = mesh;
    39.  
    40.         mesh.vertices = vertices.ToArray();
    41.         mesh.triangles = triangles.ToArray();
    42.         mesh.RecalculateNormals();
    43.  
    44.         int tileAmount = 10;
    45.         Vector2[] uvs = new Vector2[vertices.Count];
    46.         for (int i =0; i < vertices.Count; i ++) {
    47.             float percentX = Mathf.InverseLerp(-map.GetLength(0)/2*squareSize,map.GetLength(0)/2*squareSize,vertices[i].x) * tileAmount;
    48.             float percentY = Mathf.InverseLerp(-map.GetLength(0)/2*squareSize,map.GetLength(0)/2*squareSize,vertices[i].z) * tileAmount;
    49.             uvs[i] = new Vector2(percentX,percentY);
    50.         }
    51.         mesh.uv = uvs;
    52.  
    53.  
    54.         if (is2D) {
    55.             Generate2DColliders();
    56.         } else {
    57.             CreateWallMesh ();
    58.         }
    59.     }
    60.  
    61.     void CreateWallMesh() {
    62.  
    63.         CalculateMeshOutlines ();
    64.  
    65.         List<Vector3> wallVertices = new List<Vector3> ();
    66.         List<int> wallTriangles = new List<int> ();
    67.         Mesh wallMesh = new Mesh ();
    68.         float wallHeight = 5;
    69.  
    70.         foreach (List<int> outline in outlines) {
    71.             for (int i = 0; i < outline.Count -1; i ++) {
    72.                 int startIndex = wallVertices.Count;
    73.                 wallVertices.Add(vertices[outline[i]]); // left
    74.                 wallVertices.Add(vertices[outline[i+1]]); // right
    75.                 wallVertices.Add(vertices[outline[i]] - Vector3.up * wallHeight); // bottom left
    76.                 wallVertices.Add(vertices[outline[i+1]] - Vector3.up * wallHeight); // bottom right
    77.  
    78.                 wallTriangles.Add(startIndex + 0);
    79.                 wallTriangles.Add(startIndex + 2);
    80.                 wallTriangles.Add(startIndex + 3);
    81.  
    82.                 wallTriangles.Add(startIndex + 3);
    83.                 wallTriangles.Add(startIndex + 1);
    84.                 wallTriangles.Add(startIndex + 0);
    85.             }
    86.         }
    87.         wallMesh.vertices = wallVertices.ToArray ();
    88.         wallMesh.triangles = wallTriangles.ToArray ();
    89.         walls.mesh = wallMesh;
    90.  
    91.         MeshCollider wallCollider = walls.gameObject.AddComponent<MeshCollider> ();
    92.         wallCollider.sharedMesh = wallMesh;
    93.     }
    94.  
    95.     void Generate2DColliders() {
    96.  
    97.         EdgeCollider2D[] currentColliders = gameObject.GetComponents<EdgeCollider2D> ();
    98.         for (int i = 0; i < currentColliders.Length; i++) {
    99.             Destroy(currentColliders[i]);
    100.         }
    101.  
    102.         CalculateMeshOutlines ();
    103.  
    104.         foreach (List<int> outline in outlines) {
    105.             EdgeCollider2D edgeCollider = gameObject.AddComponent<EdgeCollider2D>();
    106.             Vector2[] edgePoints = new Vector2[outline.Count];
    107.  
    108.             for (int i =0; i < outline.Count; i ++) {
    109.                 edgePoints[i] = new Vector2(vertices[outline[i]].x,vertices[outline[i]].z);
    110.             }
    111.             edgeCollider.points = edgePoints;
    112.         }
    113.  
    114.     }
    115.  
    116.     void TriangulateSquare(Square square) {
    117.         switch (square.configuration) {
    118.         case 0:
    119.             break;
    120.  
    121.         // 1 points:
    122.         case 1:
    123.             MeshFromPoints(square.centreLeft, square.centreBottom, square.bottomLeft);
    124.             break;
    125.         case 2:
    126.             MeshFromPoints(square.bottomRight, square.centreBottom, square.centreRight);
    127.             break;
    128.         case 4:
    129.             MeshFromPoints(square.topRight, square.centreRight, square.centreTop);
    130.             break;
    131.         case 8:
    132.             MeshFromPoints(square.topLeft, square.centreTop, square.centreLeft);
    133.             break;
    134.  
    135.         // 2 points:
    136.         case 3:
    137.             MeshFromPoints(square.centreRight, square.bottomRight, square.bottomLeft, square.centreLeft);
    138.             break;
    139.         case 6:
    140.             MeshFromPoints(square.centreTop, square.topRight, square.bottomRight, square.centreBottom);
    141.             break;
    142.         case 9:
    143.             MeshFromPoints(square.topLeft, square.centreTop, square.centreBottom, square.bottomLeft);
    144.             break;
    145.         case 12:
    146.             MeshFromPoints(square.topLeft, square.topRight, square.centreRight, square.centreLeft);
    147.             break;
    148.         case 5:
    149.             MeshFromPoints(square.centreTop, square.topRight, square.centreRight, square.centreBottom, square.bottomLeft, square.centreLeft);
    150.             break;
    151.         case 10:
    152.             MeshFromPoints(square.topLeft, square.centreTop, square.centreRight, square.bottomRight, square.centreBottom, square.centreLeft);
    153.             break;
    154.  
    155.         // 3 point:
    156.         case 7:
    157.             MeshFromPoints(square.centreTop, square.topRight, square.bottomRight, square.bottomLeft, square.centreLeft);
    158.             break;
    159.         case 11:
    160.             MeshFromPoints(square.topLeft, square.centreTop, square.centreRight, square.bottomRight, square.bottomLeft);
    161.             break;
    162.         case 13:
    163.             MeshFromPoints(square.topLeft, square.topRight, square.centreRight, square.centreBottom, square.bottomLeft);
    164.             break;
    165.         case 14:
    166.             MeshFromPoints(square.topLeft, square.topRight, square.bottomRight, square.centreBottom, square.centreLeft);
    167.             break;
    168.  
    169.         // 4 point:
    170.         case 15:
    171.             MeshFromPoints(square.topLeft, square.topRight, square.bottomRight, square.bottomLeft);
    172.             checkedVertices.Add(square.topLeft.vertexIndex);
    173.             checkedVertices.Add(square.topRight.vertexIndex);
    174.             checkedVertices.Add(square.bottomRight.vertexIndex);
    175.             checkedVertices.Add(square.bottomLeft.vertexIndex);
    176.             break;
    177.         }
    178.  
    179.     }
    180.  
    181.     void MeshFromPoints(params Node[] points) {
    182.         AssignVertices(points);
    183.  
    184.         if (points.Length >= 3)
    185.             CreateTriangle(points[0], points[1], points[2]);
    186.         if (points.Length >= 4)
    187.             CreateTriangle(points[0], points[2], points[3]);
    188.         if (points.Length >= 5)
    189.             CreateTriangle(points[0], points[3], points[4]);
    190.         if (points.Length >= 6)
    191.             CreateTriangle(points[0], points[4], points[5]);
    192.  
    193.     }
    194.  
    195.     void AssignVertices(Node[] points) {
    196.         for (int i = 0; i < points.Length; i ++) {
    197.             if (points[i].vertexIndex == -1) {
    198.                 points[i].vertexIndex = vertices.Count;
    199.                 vertices.Add(points[i].position);
    200.             }
    201.         }
    202.     }
    203.  
    204.     void CreateTriangle(Node a, Node b, Node c) {
    205.         triangles.Add(a.vertexIndex);
    206.         triangles.Add(b.vertexIndex);
    207.         triangles.Add(c.vertexIndex);
    208.  
    209.         Triangle triangle = new Triangle (a.vertexIndex, b.vertexIndex, c.vertexIndex);
    210.         AddTriangleToDictionary (triangle.vertexIndexA, triangle);
    211.         AddTriangleToDictionary (triangle.vertexIndexB, triangle);
    212.         AddTriangleToDictionary (triangle.vertexIndexC, triangle);
    213.     }
    214.  
    215.     void AddTriangleToDictionary(int vertexIndexKey, Triangle triangle) {
    216.         if (triangleDictionary.ContainsKey (vertexIndexKey)) {
    217.             triangleDictionary [vertexIndexKey].Add (triangle);
    218.         } else {
    219.             List<Triangle> triangleList = new List<Triangle>();
    220.             triangleList.Add(triangle);
    221.             triangleDictionary.Add(vertexIndexKey, triangleList);
    222.         }
    223.     }
    224.  
    225.     void CalculateMeshOutlines() {
    226.  
    227.         for (int vertexIndex = 0; vertexIndex < vertices.Count; vertexIndex ++) {
    228.             if (!checkedVertices.Contains(vertexIndex)) {
    229.                 int newOutlineVertex = GetConnectedOutlineVertex(vertexIndex);
    230.                 if (newOutlineVertex != -1) {
    231.                     checkedVertices.Add(vertexIndex);
    232.  
    233.                     List<int> newOutline = new List<int>();
    234.                     newOutline.Add(vertexIndex);
    235.                     outlines.Add(newOutline);
    236.                     FollowOutline(newOutlineVertex, outlines.Count-1);
    237.                     outlines[outlines.Count-1].Add(vertexIndex);
    238.                 }
    239.             }
    240.         }
    241.     }
    242.  
    243.     void FollowOutline(int vertexIndex, int outlineIndex) {
    244.         outlines [outlineIndex].Add (vertexIndex);
    245.         checkedVertices.Add (vertexIndex);
    246.         int nextVertexIndex = GetConnectedOutlineVertex (vertexIndex);
    247.  
    248.         if (nextVertexIndex != -1) {
    249.             FollowOutline(nextVertexIndex, outlineIndex);
    250.         }
    251.     }
    252.  
    253.     int GetConnectedOutlineVertex(int vertexIndex) {
    254.         List<Triangle> trianglesContainingVertex = triangleDictionary [vertexIndex];
    255.  
    256.         for (int i = 0; i < trianglesContainingVertex.Count; i ++) {
    257.             Triangle triangle = trianglesContainingVertex[i];
    258.  
    259.             for (int j = 0; j < 3; j ++) {
    260.                 int vertexB = triangle[j];
    261.                 if (vertexB != vertexIndex && !checkedVertices.Contains(vertexB)) {
    262.                     if (IsOutlineEdge(vertexIndex, vertexB)) {
    263.                         return vertexB;
    264.                     }
    265.                 }
    266.             }
    267.         }
    268.  
    269.         return -1;
    270.     }
    271.  
    272.     bool IsOutlineEdge(int vertexA, int vertexB) {
    273.         List<Triangle> trianglesContainingVertexA = triangleDictionary [vertexA];
    274.         int sharedTriangleCount = 0;
    275.  
    276.         for (int i = 0; i < trianglesContainingVertexA.Count; i ++) {
    277.             if (trianglesContainingVertexA[i].Contains(vertexB)) {
    278.                 sharedTriangleCount ++;
    279.                 if (sharedTriangleCount > 1) {
    280.                     break;
    281.                 }
    282.             }
    283.         }
    284.         return sharedTriangleCount == 1;
    285.     }
    286.  
    287.     struct Triangle {
    288.         public int vertexIndexA;
    289.         public int vertexIndexB;
    290.         public int vertexIndexC;
    291.         int[] vertices;
    292.  
    293.         public Triangle (int a, int b, int c) {
    294.             vertexIndexA = a;
    295.             vertexIndexB = b;
    296.             vertexIndexC = c;
    297.  
    298.             vertices = new int[3];
    299.             vertices[0] = a;
    300.             vertices[1] = b;
    301.             vertices[2] = c;
    302.         }
    303.  
    304.         public int this[int i] {
    305.             get {
    306.                 return vertices[i];
    307.             }
    308.         }
    309.  
    310.  
    311.         public bool Contains(int vertexIndex) {
    312.             return vertexIndex == vertexIndexA || vertexIndex == vertexIndexB || vertexIndex == vertexIndexC;
    313.         }
    314.     }
    315.  
    316.     public class SquareGrid {
    317.         public Square[,] squares;
    318.  
    319.         public SquareGrid(int[,] map, float squareSize) {
    320.             int nodeCountX = map.GetLength(0);
    321.             int nodeCountY = map.GetLength(1);
    322.             float mapWidth = nodeCountX * squareSize;
    323.             float mapHeight = nodeCountY * squareSize;
    324.  
    325.             ControlNode[,] controlNodes = new ControlNode[nodeCountX,nodeCountY];
    326.  
    327.             for (int x = 0; x < nodeCountX; x ++) {
    328.                 for (int y = 0; y < nodeCountY; y ++) {
    329.                     Vector3 pos = new Vector3(-mapWidth/2 + x * squareSize + squareSize/2, 0, -mapHeight/2 + y * squareSize + squareSize/2);
    330.                     controlNodes[x,y] = new ControlNode(pos,map[x,y] == 1, squareSize);
    331.                 }
    332.             }
    333.  
    334.             squares = new Square[nodeCountX -1,nodeCountY -1];
    335.             for (int x = 0; x < nodeCountX-1; x ++) {
    336.                 for (int y = 0; y < nodeCountY-1; y ++) {
    337.                     squares[x,y] = new Square(controlNodes[x,y+1], controlNodes[x+1,y+1], controlNodes[x+1,y], controlNodes[x,y]);
    338.                 }
    339.             }
    340.  
    341.         }
    342.     }
    343.  
    344.     public class Square {
    345.  
    346.         public ControlNode topLeft, topRight, bottomRight, bottomLeft;
    347.         public Node centreTop, centreRight, centreBottom, centreLeft;
    348.         public int configuration;
    349.  
    350.         public Square (ControlNode _topLeft, ControlNode _topRight, ControlNode _bottomRight, ControlNode _bottomLeft) {
    351.             topLeft = _topLeft;
    352.             topRight = _topRight;
    353.             bottomRight = _bottomRight;
    354.             bottomLeft = _bottomLeft;
    355.  
    356.             centreTop = topLeft.right;
    357.             centreRight = bottomRight.above;
    358.             centreBottom = bottomLeft.right;
    359.             centreLeft = bottomLeft.above;
    360.  
    361.             if (topLeft.active)
    362.                 configuration += 8;
    363.             if (topRight.active)
    364.                 configuration += 4;
    365.             if (bottomRight.active)
    366.                 configuration += 2;
    367.             if (bottomLeft.active)
    368.                 configuration += 1;
    369.         }
    370.  
    371.     }
    372.  
    373.     public class Node {
    374.         public Vector3 position;
    375.         public int vertexIndex = -1;
    376.  
    377.         public Node(Vector3 _pos) {
    378.             position = _pos;
    379.         }
    380.     }
    381.  
    382.     public class ControlNode : Node {
    383.  
    384.         public bool active;
    385.         public Node above, right;
    386.  
    387.         public ControlNode(Vector3 _pos, bool _active, float squareSize) : base(_pos) {
    388.             active = _active;
    389.             above = new Node(position + Vector3.forward * squareSize/2f);
    390.             right = new Node(position + Vector3.right * squareSize/2f);
    391.         }
    392.  
    393.     }
    394. }
    395.  

    The Second script is just a collision part of the first script
     
    Last edited: Dec 29, 2015
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. wallCollider.isTrigger = true;
    3.  
     
  3. CoopReed

    CoopReed

    Joined:
    Nov 17, 2015
    Posts:
    27
    Thanks mate
     
  4. CoopReed

    CoopReed

    Joined:
    Nov 17, 2015
    Posts:
    27
    My mesh is still not colliding with the player object at all......