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
0
Question by Dzejkob1218 · Nov 01, 2015 at 10:53 PM · c#meshmesh collider

Mesh collider with a mesh created by code returns incorrect results.

So I need to create a plane in real time based on location of 4 different points as the vertices. Here's my script:

public class MeshTest : MonoBehaviour {

 public GameObject[] Points;
 public Material mat;

 void Start() {
     GameObject plane = new GameObject("Plane");
     MeshFilter meshFilter = (MeshFilter)plane.AddComponent(typeof(MeshFilter));
     MeshRenderer renderer = plane.AddComponent(typeof(MeshRenderer)) as MeshRenderer;
     renderer.material = mat;

     Mesh m = new Mesh();
     m.name = "ScriptedMesh";
     m.vertices = new Vector3[] {
         Points[0].transform.position,
         Points[1].transform.position,
         Points[2].transform.position,
         Points[3].transform.position,
     };
     m.uv = new Vector2[] {
         new Vector2 (0, 0),
         new Vector2 (0, 1),
         new Vector2 (1, 1),
         new Vector2 (1, 0)
     };
     m.triangles = new int[] { 0, 1, 2, 0, 2, 3};
     m.RecalculateNormals();

     meshFilter.mesh = m;
     MeshCollider collider = plane.AddComponent(typeof(MeshCollider)) as MeshCollider;
     collider.convex = true;
 }


}

Creating the mesh works fine, on start I get a correctly rendered plane spanning between the Points game objects, only the mesh collider doesn't work.

When the object is created, it's collider is just a simple 3D box. Changing the vertices positions doesn't do anything, neither does changing the order of commands, it seems like the component just doesn't accept this mesh although in the inspector it looks like it was assigned correctly.

Am I doing something wrong?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by IgorAherne · Nov 01, 2015 at 11:37 PM

I remember having the same problem, during custom terrain generation.

As far as I remember, there isn't a function which would update the mesh collider. However, I use to solve it with either:

re-setting the collider.sharedMesh to the current shared mesh of your MeshFilter,

or disabling / enabling the collider (which forced it to update itself). One of those two things

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
1

Answer by Dzejkob1218 · Nov 02, 2015 at 02:46 PM

Allright, I've got this.

If anyone is having the same problem, here's my solution;

Mesh collider component doesn't work with 2D planes, you can try this on the generic plane

If you want to create planes with colliders (for example for terrain) you need to make a second set of vertices, move them down just a fraction of a unit and create a 3D shape, mesh collider should accept it.

This may be a little harder on performance since there are many more faces, but it works.

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 Arycama · Nov 03, 2015 at 06:57 AM 0
Share

If you go to "GameObject > Create > Plane" (Or a quad) you'll see that it creates a plane with a mesh collider attached, so it is definitely possible to have a mesh collider with a 2D plane. Did you try my answer below?

avatar image
1

Answer by Arycama · Nov 02, 2015 at 09:28 AM

After you add a mesh collider component on line 28, you need to tell the mesh collider what mesh to actually use. Adding the following line after line 28 should do the trick:

 collider.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

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

How to Move two spheres over the surface of irregular shapes meshes towards the closest X and Y boundary of mesh without leaving its trigger till end.,? 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Why is my vertex array empty using GetComponent().mesh.vertices? 3 Answers

Pushing a mesh with mesh 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