Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
10
Question by TomNCatz · Sep 29, 2011 at 03:58 AM · collidermeshdynamic

Dynamic mesh collider

I'm generating a mesh at runtime to create terrain. Generating the mesh is running fine, mapping UVs is good, and the bounding box is working properly. However, there is a slight issue with there not being any kind of collisions.

It has a mesh collider attached to it, and the generated mesh is getting passed to the collider, however it is ignoring this mesh. I have also found that if i set the collider to Convex in the unity editor then change it back the collider works as it should. I have tried changing the collider to convex in the code, but this doesn't seem to do the same thing as clicking the check box. This says to me that there is some function call that takes place when you check the box in the editor. Since I can't very well click this check box in the finished game, does anyone know about this? I've seen other people dynamically generating mesh colliders with no trouble, but none of them have mentioned this issue.

Comment
Add comment · Show 1
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
avatar image frogsbo · Mar 20, 2014 at 04:41 PM 0
Share

Creating a mesh of 1024 vertices takes 0,02 seconds in Unity, but making the mesh collider takes 0.3 seconds at least...

So you can only make 1/10th the colliders that you can make the mesh... mesh colliders are precalculated to be objects with mass that can bounce many ways and have many materials, unlike the terrain.

i would recommend to make a custom collider code that bounces things if they go underneath, i am about to post a question about custom physics as i cant find the answer.

4 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by Kos-Dvornik · Jun 23, 2012 at 06:47 PM

You should apply sharedMesh in MeshCollider AFTER any operation with Object mesh gonna be DONE. So simply put assign code at the BOTTOM. For example:

WRONG:

Mesh m = (gs[i].GetComponent(typeof(MeshFilter)) as MeshFilter).mesh;

MeshCollider theMeshCollider = gs[i].GetComponent(typeof(MeshCollider)) as MeshCollider;
theMeshCollider.sharedMesh = m;

m.triangles = someTriangles;

CORRECT:

Mesh m = (gs[i].GetComponent(typeof(MeshFilter)) as MeshFilter).mesh;

m.triangles = someTriangles;

MeshCollider theMeshCollider = gs[i].GetComponent(typeof(MeshCollider)) as MeshCollider;
theMeshCollider.sharedMesh = m;

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
avatar image
3

Answer by Ming · Nov 04, 2011 at 02:57 PM

Ah, I figured out the answer.

When looking into this, I noticed that the bounds on my new meshCollider was zero area. I tried to set the bounds on the meshCollider, but it didn't work. I investigated further.

So, it seems that if you have this:

 MeshCollider myMC = GetComponent<MeshCollider>();
 myMC.sharedMesh = new Mesh();
 myMC.sharedMesh.vertices = newverts;
 myMC.sharedMesh.triangles = newtris;
 myMC.sharedMesh.RecalculateBounds();

Then RecalculateBounds won't actually do anything. Setting the bounds directly also won't change the bounds on myMC even if it changes it on myMC.sharedMesh.

To set the bounds on myMC properly, you must assign myMC.sharedMesh after the mesh has been created an populated with vertices.

So the new code looks like:

 MeshCollider myMC = GetComponent<MeshCollider>();
 Mesh newMesh = new Mesh();
 newMesh  = new Mesh();
 newMesh.vertices  = newverts;
 newMesh.triangles = newtris;
 newMesh.RecalculateBounds();
 myMC.sharedMesh = newMesh;
Comment
Add comment · Show 2 · 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
avatar image intrepidis · Nov 13, 2014 at 09:29 PM 0
Share

There's no need to do "new $$anonymous$$esh()" twice.

avatar image IzzySoft · Dec 27, 2014 at 06:57 PM 0
Share
 $$anonymous$$esh my$$anonymous$$esh = GetComponent< $$anonymous$$esh >();
 .......
 my$$anonymous$$esh.RecalculateBounds();

 $$anonymous$$eshCollider my$$anonymous$$C = GetComponent< $$anonymous$$eshCollider >();
 my$$anonymous$$C.shared$$anonymous$$esh = null;
 my$$anonymous$$C.shared$$anonymous$$esh = my$$anonymous$$esh;

This should make sure the $$anonymous$$eshCollider updates to match the $$anonymous$$esh Bounds

avatar image
1

Answer by Ming · Nov 04, 2011 at 02:20 PM

I have the same issue.

I used sharedMesh to create the new mesh, so that's not an issue.

I noticed that clicking any of the following on and then off enables the new mesh collision to work (IsTrigger, Smooth Sphere Collisions, Convex). Apparently, there is some sort of initialization done by the Editor with this clicks that allows it to work.

Comment
Add comment · Show 1 · 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
avatar image Ranger-Ori · Nov 18, 2013 at 10:15 PM 0
Share

Hi, I know your answer was a long time ago, anyway, I still encounter this problem and your solution is the same thing that happened to me, any other way to solve it? cause I can't use the editor during game time...

avatar image
0

Answer by cupsster · Nov 03, 2011 at 07:17 PM

 // Assigns an arbitrary mesh collider to the current transform
 
 var meshToCollide : Mesh;
 
 if(!meshToCollide) {
     Debug.LogError("Assign a mesh in the inspector");
     return;
 }
 transform.gameObject.AddComponent(MeshCollider);
 transform.GetComponent(MeshCollider).sharedMesh = meshToCollide;


I think you must access shared mesh instead.

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Tile-based approach in Unity 1 Answer

Having trouble getting a smooth collision with dynamic mesh collider 0 Answers

Manipulating vertices through colliders 1 Answer

Raycast crashing build on collision with mesh collider 3 Answers

Mesh or Box Collider 1 Answer


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