Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by lm2malicl · Dec 22, 2015 at 08:57 AM · meshmesh colliderprocedural-generation

Procedural Generated mesh collider creates invisible walls after the first iteration

I have the a procedural generated cave code from the unity tutorials. It works great the first time but as soon as I press 'r' (the update key) to generate a new cave the mesh collider creates invisible walls. The only thing I can come up with is that the old mesh collider is not being erased, but I dont know how to do that. Attached bellow is the relevant portion of the code I am using.

 public class MarchingSquares : MonoBehaviour {
 
     public SquareGrid squareGrid;
     List<Vector3> vertices;
     List<int> triangles;
     public MeshFilter walls;
     //public bool is2D; 
     public static float wallHeight = 5;
 
     Dictionary<int,List<Triangle>> triangleDictionary = new Dictionary<int, List<Triangle>>();
     List<List<int>> outlines = new List<List<int>> ();
     HashSet<int> checkedVertices = new HashSet<int>();
 
     public void GenerateMesh(int[,] map, float squareSize){
 
         outlines.Clear ();
         checkedVertices.Clear ();
         triangleDictionary.Clear ();
 
         squareGrid=new SquareGrid(map,squareSize);
 
         vertices = new List<Vector3> ();
         triangles = new List<int> ();
         for (int x =0; x< squareGrid.squares.GetLength(0); x++) {
             for (int y=0; y< squareGrid.squares.GetLength(1); y++) {
                 TrigangluateSqaure(squareGrid.squares[x,y]);
             }
         }
 
         Mesh mesh = new Mesh ();
         GetComponent<MeshFilter>().mesh = mesh;
 
         mesh.vertices = vertices.ToArray ();
         mesh.triangles = triangles.ToArray ();
         mesh.RecalculateNormals();
         CreateWallMesh ();
     }
 
     void CreateWallMesh(){
 
         CalculateMeshOutlines ();
         List<Vector3> wallVertices = new List<Vector3> ();
         List<int> wallTriangles = new List<int> ();
         Mesh wallMesh = new Mesh ();
 
         foreach (List<int> outline in outlines) {
             for (int i=0; i<outline.Count-1;i++){
                 int startIndex = wallVertices.Count;
                 wallVertices.Add(vertices[outline[i]]);//top left
                 wallVertices.Add(vertices[outline[i+1]]);//bottom left
                 wallVertices.Add(vertices[outline[i]]-Vector3.up*wallHeight);//bottom left
                 wallVertices.Add(vertices[outline[i+1]]-Vector3.up*wallHeight);//bottom right
 
                 wallTriangles.Add (startIndex);
                 wallTriangles.Add (startIndex+2);
                 wallTriangles.Add (startIndex+3);
 
                 wallTriangles.Add (startIndex+3);
                 wallTriangles.Add (startIndex+1);
                 wallTriangles.Add (startIndex);
             }
         }
 
         wallMesh.vertices = wallVertices.ToArray ();
         wallMesh.triangles = wallTriangles.ToArray ();
         wallMesh.RecalculateBounds();
         walls.mesh = wallMesh;
 
         MeshCollider wallCollider = walls.gameObject.AddComponent<MeshCollider> ();
         wallCollider.sharedMesh.RecalculateBounds ();
         wallCollider.sharedMesh.RecalculateNormals ();
         //wallCollider.sharedMesh = wallMesh;
     }
 

Any help would be greatly appreciated

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by CorruptComputer · Nov 30, 2016 at 07:37 AM

This is late, however I am sure others still have this issue and I have been able to fix it by removing this line and just getting the component:

 MeshCollider wallCollider = walls.gameObject.AddComponent<MeshCollider> ();


Here is my CreateWallMesh() method:

 void CreateWallMesh()
     {
 
         CalculateMeshOutlines();
 
         List<Vector3> wallVertices = new List<Vector3>();
         List<int> wallTriangles = new List<int>();
         Mesh wallMesh = new Mesh();
         float wallHeight = 5;
         MeshCollider wallCollider = walls.GetComponent<MeshCollider>();
 
         foreach (List<int> outline in outlines)
         {
             for (int i = 0; i < outline.Count - 1; i++)
             {
                 int startIndex = wallVertices.Count;
                 wallVertices.Add(vertices[outline[i]]); // left
                 wallVertices.Add(vertices[outline[i + 1]]); // right
                 wallVertices.Add(vertices[outline[i]] - Vector3.up * wallHeight); // bottom left
                 wallVertices.Add(vertices[outline[i + 1]] - Vector3.up * wallHeight); // bottom right
 
                 wallTriangles.Add(startIndex + 0);
                 wallTriangles.Add(startIndex + 2);
                 wallTriangles.Add(startIndex + 3);
 
                 wallTriangles.Add(startIndex + 3);
                 wallTriangles.Add(startIndex + 1);
                 wallTriangles.Add(startIndex + 0);
             }
         }
         wallMesh.vertices = wallVertices.ToArray();
         wallMesh.triangles = wallTriangles.ToArray();
         walls.mesh = wallMesh;
         wallCollider.sharedMesh = wallMesh;
     }

You just have to make sure that the Walls object has a Mesh Collider before the game launches, rather than having the code generate it.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

moving hole 3 Answers

Pushing a mesh with mesh collider 1 Answer

Changing Meshes but end up in the air when testing 0 Answers

Vertex Colors not smooth enough? 0 Answers

What is wrong with this mesh editing code? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges