Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Christian Rask · Dec 21, 2010 at 09:31 AM · meshproceduralverticesmeshcollidermeshfilter

MeshCollider is not getting updated when adding vertex to mesh

I have created a simple piece of code for adding vertices to a mesh by clicking on it. The vertex is added to the point of the mesh which is clicked. This works very well the first time. But the second time it fails to get the correct triangle from RaycastHit.triangleIndex. Here is the code, including a hack which actually makes it work. (The fact that the hack works also eliminates the possibility of this being a corrupted mesh.)

public void AddVertexScreenPos(Vector2 screenPos) { RaycastHit hit; if (HitScreenPos(Input.mousePosition, out hit)) { if (hit.collider.GetType() == typeof(MeshCollider)) { MeshCollider meshCollider = (MeshCollider)hit.collider; Mesh mesh = meshCollider.GetComponent<MeshFilter>().mesh; List<Vector3> vertices = new List<Vector3>(mesh.vertices); List<int> triangles = new List<int>(mesh.triangles);

vertices.Add(meshCollider.transform.InverseTransfo rmPoint(hit.point));

int[] triangleHit = GetTriangleIndices(ref hit);

triangles.Clear();

triangles.AddRange(RemoveTriangle(hit.triangleInde x, mesh.triangles));

for (int i = 0; i < triangleHit.Length; i++) { triangles.Add(triangleHit[i % triangleHit.Length]); triangles.Add(triangleHit[(i + 1) % triangleHit.Length]); triangles.Add(vertices.Count - 1);

}

mesh.vertices = vertices.ToArray(); mesh.triangles = triangles.ToArray(); mesh.RecalculateNormals(); mesh.RecalculateBounds();

//The following line should update the meshCollider, but it fails. //meshCollider.sharedMesh = mesh;

//Hack. This fixes the problem GameObject go = hit.transform.gameObject; GameObject.DestroyImmediate(meshCollider);

go.AddComponent<MeshCollider>(); //Hack end.

} }

}

Any suggestions?

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
2

Answer by MachineGunCat · Mar 15, 2011 at 10:47 PM

I know this is old but it showed up in Google when looking for a similar problem. User pkamat on the Unity forums solved this very same problem for me :

In place of your hack, try nullifying the mesh before assigning it your new mesh :

meshCollider.sharedMesh = null;
meshCollider.sharedMesh = mesh;

Source of the answer : http://forum.unity3d.com/threads/32467-How-to-update-a-mesh-collider

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 HarvesteR · Apr 05, 2011 at 10:02 PM 0
Share

What he said.

Destroying and recreating a meshcollider component is far heavier than just assigning it the new mesh.

For some reason, meshcolliders don't like replacing meshes when they already have one... it will fail silently, and collisions won't work as expected.

Setting it to null first will let the meshcollider accept the new mesh.

Cheers

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

No one has followed this question yet.

Related Questions

Mirror vertices procedurally 2 Answers

Save previous state of mesh procedurally 1 Answer

Generate a highpoly sphere with mesh collider procedurally 2 Answers

Selecting a single polygon / face at runtime 1 Answer

Hidden triangles and performance. 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