Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
4
Question by AntLewis · Apr 29, 2013 at 03:20 PM · vertexmesh collider

Changing mesh collider at run time

Hi there, I'm having some issues with my mesh collider. During run time I'm changing the vertices and I want to update the mesh collider to match the visual changes of the mesh.

Visibly the mesh is changing, but the collision volume is remaining as it's original shape - I'm using mesh.RecalculateBounds() to update the collider after I assign the updated vertices back to the mesh (I believe that's the correct approach) but this doesn't seem to work.

Any suggestions? Thanks! Ant

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

7 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by HannesR · Apr 29, 2013 at 03:25 PM

You have to set the collider mesh to null and then assign the updated mesh.

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 HannesR · Apr 29, 2013 at 04:08 PM 1
Share
 the$$anonymous$$eshCollider.mesh = null;
 the$$anonymous$$eshCollider.mesh = theUpdated$$anonymous$$esh;
avatar image AntLewis · Apr 29, 2013 at 04:13 PM 0
Share

Great, thanks for the help!

avatar image
3

Answer by keithkeith · Nov 25, 2014 at 12:00 AM

This doesn't seem to work anymore. Destroying and recreating the meshcollider does.

 DestroyImmediate(this.GetComponent<MeshCollider>());
 var collider = this.AddComponent<MeshCollider>();
 collider.sharedMesh = myMesh;

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 YoyoMario · Jun 22, 2016 at 08:34 PM 1
Share

Hello, I am having issue with this 2 years later, and none of these options work anymore, after my mesh changes, i want to update the mesh collider like this, and it doesnt work in unity5 anymore i guess... pls halp

 mesh.vertices = vertices;
 mesh.RecalculateBounds();
 mesh.RecalculateNormals();
 
 DestroyImmediate(this.GetComponent<$$anonymous$$eshCollider>());
 $$anonymous$$eshCollider collider = gameObject.AddComponent<$$anonymous$$eshCollider>();
 collider.shared$$anonymous$$esh = mesh;
avatar image
2

Answer by lil_sichen · Dec 14, 2017 at 08:16 AM

I have tried all solutions out in Unity 2017.2.0f3 but none worked. Here is my observation and solution: If you assign a "used" mesh like this:

 var newMesh = ProceduralGen.GetNewMesh();
 DestroyImmediate(this.GetComponent<MeshCollider>());
 var collider = gameObject.AddComponent<MeshCollider>();
 collider.sharedMesh = newMesh;
 

it won't work for some reason. However, in my case, when I assigned the procedurally generated Mesh straight to the collider's sharedMesh, it worked:

 var collider = gameObject.GetComponent<MeshCollider>();
 collider.sharedMesh =  ProceduralGen.GetNewMesh();

Also, in this case you don't have to worry about the order in which you assign the meshFilter and collider's meshes. Plus, there is no need to remove and add the collider! This is definitely not the best solution for those of you who have to deal with heavy, complicated procedural generation, but in my case it worked fine. Hope this helps!

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
0

Answer by vladibo · Jul 05, 2017 at 02:28 AM

             var smr = _renderer;
             var mc = _collider;
             var colliderMesh = new Mesh();
             smr.BakeMesh(colliderMesh);
             mc.sharedMesh = null;
             mc.sharedMesh = colliderMesh;
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
0

Answer by Cursedth · Oct 12, 2017 at 06:16 AM

Most practical solution here suggested by defaxer: http://answers.unity3d.com/questions/193630/modify-mesh-collider-when-modifying-vertices.html

Workd for me. Just get the

 myGamObject.GetComponent<AnyCollider>().gameObject.SetActive(false then true again);

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
  • 1
  • 2
  • ›

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

23 People are following this question.

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

Related Questions

Trouble recalculating 3D mesh's triangles after deleting verts 2 Answers

Vertex count 10 times higher in Unity 5 Answers

adding texture to vertex color darkens color 0 Answers

Vertex count on a procedurally generated map and some related doubts 2 Answers

Manipulating a vertex on a plane 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