• 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
Question by Aubrey Hesselgren · Oct 21, 2010 at 09:43 AM · collisionmeshprocedural

Collision on generated mesh

According to the documents, mesh colliders are only constructed when the mesh is placed into the scene.

Is it possible to force a collider component to construct itself based on a mesh filter which you yourself have constructed at runtime? i.e. if geometry changes?

I.e. if I were to make a parametric spiral mesh, for example, and wanted to be able to tweak how many degrees it generates through at run time, and then wanted the result to be collidable, how would I go about that?

If a prefab was instantiated into the scene, and the geometry was constructed in the "Start()" function, would that be early enough to let the mesh collider construct itself?

Or should I add a mesh collider to the game object at runtime after the mesh data is set up?

Comment
duck

People who like this

1 Show 0
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
Best Answer

Answer by duck · Oct 21, 2010 at 11:26 AM

All you need to do is add the mesh collider component to the same object after you've finished generating the procedural mesh itself. It's fine if it's all in the Start() method. For example:

void Start() {

 // create new Mesh Filter & Mesh objects
 var meshFilter = AddComponent<MeshFilter>();
 var mesh = new Mesh();

 // set up mesh
 mesh.name = name;
 mesh.vertices = myVerts;
 mesh.triangles = myTris;
 mesh.RecalculateNormals();
 meshFilter.mesh = mesh;
 mesh.RecalculateBounds(); 

 // add collider! 
 AddComponent<MeshCollider>();

}

Comment
Aubrey Hesselgren
TaintedLemon
Dave-Carlile

People who like this

3 Show 3 · 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 Aubrey Hesselgren · Oct 21, 2010 at 11:41 AM 0
Share

Thank you!

There's no need to point the MeshCollider at the actual mesh, after adding it, or...?

avatar image duck ♦♦ · Oct 21, 2010 at 12:17 PM 0
Share

No, I guess unless you specifically tell it to use another mesh, it uses the same mesh as the MeshFilter object.

If you want to make a lower-res proxy mesh for the collision (while using a higher res mesh for the renderer), you'd have to look into that more closely - or just use separate gameObjects for each :)

avatar image Aubrey Hesselgren · Oct 25, 2010 at 09:55 AM 0
Share

Thanks again duck! Really appreciate the help!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

minimize vertex artifacting for procedural mesh manipulations - vertex collision same object? 0 Answers

Procedural Geometry doesn't collide.. 1 Answer

Best approach for collision in shape combination game? 0 Answers

How do I calculate the UV's for a procedurally generated shape? 2 Answers

Rivers on procedurally terrain 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