Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 breakfeast · Aug 02, 2015 at 06:22 PM · collisionmeshmeshcollidergeneration

Generated mesh collider on a generated mesh becomes a big box instead of taking mesh's shape

Hi there,

I'm creating a mesh in code, currently just a triangle, which represents my characters' field of view, to capture what enemy units are in view when they take aim. The mesh is generated in code (the details of which I know little about but seem to have gotten working out of cobbled-together samples), and that seems to be working fine. You can see the nice green triangle it's making in this screenshot:

alt text

However, the mesh collider I'm then creating and applying to this object always takes the shape of a box bigger than the triangle mesh it's supposed to be taking its shape from. The way I'm applying the mesh to the collider looks to me pretty much the same as all the code-created mesh collider examples I've found, but I haven't yet come across anyone else complaining of this particular problem. I have no idea where it's getting this box shape from, but it's clearly drawing from the mesh in some way, since it always seems to scale to be just this much larger than the triangle. I'd greatly appreciate any help figuring out what's going on - thanks! The relevant code:

   GameObject CreateAimMesh(float aimArc, float range)
         {
             
             GameObject plane = new GameObject("AimingMesh");
             
             //////MESH
             MeshFilter meshFilter = plane.AddComponent<MeshFilter>();
             
             Mesh m = new Mesh();
             m.name = "aiming mesh";
             m.vertices = new Vector3[] {
                 
                 new Vector3(0, 0.1f, 0),
                 new Vector3(-(Mathf.Tan(aimArc/2) * range), 0.1f, range),
                 new Vector3((Mathf.Tan(aimArc/2) * range), 0.1f, range),
                 
             };
             
                    //note: this bit is pure copy-paste guesswork; hopefully isn't relevant though?
             m.uv = new Vector2[] {
                 new Vector2 (0, 0),
                 new Vector2 (0, 1),
                 new Vector2(1, 0),
             };
             
             m.triangles = new int[] { 0, 1, 2 };
             m.RecalculateNormals();
             m.RecalculateBounds();
             
             meshFilter.mesh = m;
             
             //////DEBUG VISUAL (untouched from the sample I copied from)
             MeshRenderer renderer = plane.AddComponent<MeshRenderer>();
             renderer.material.shader = Shader.Find ("Particles/Additive");
             Texture2D tex = new Texture2D(1, 1);
             tex.SetPixel(0, 0, Color.green);
             tex.Apply();
             renderer.material.mainTexture = tex;
             renderer.material.color = Color.green;
     
             //////COLLISION DETECTION
             MeshCollider coll = plane.AddComponent<MeshCollider>();
             coll.sharedMesh = m;
             coll.convex = true;
             coll.isTrigger = true;
     
     
             AimMesh aimScript = plane.AddComponent<AimMesh>(); //script that keeps track of targets inside the mesh on entry/exit
     
             plane.transform.position = bulletSpawn.transform.position;
             plane.transform.parent = bulletSpawn.transform;
             plane.transform.localEulerAngles = new Vector3(0, 90, 0);
             aimScript.InitAimScript(this);
     
             return plane;
         }


screen-shot-2015-08-02-at-105925-am.png (270.5 kB)
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 mtdrume · Aug 02, 2015 at 07:54 PM 0
Share

Could you add the collider before you turn it into a triangle? and then they both alter together.

1 Reply

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

Answer by zach-r-d · Aug 02, 2015 at 07:16 PM

This probably happens because a single triangle is not a convex mesh by the definition used by MeshCollider, so the mesh collider just uses the bounds of the mesh as its collision mesh. A quick fix would be to create a fourth vertex slightly below the triangle, and create three triangles between the original edges and the new point to make a very flat but technically convex tetrahedron.

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 breakfeast · Aug 02, 2015 at 08:42 PM 1
Share

Hi Zach,

This worked like a charm. All the other mesh collider Q's I was co$$anonymous$$g across had to do with convexity, so I shoulda guessed... thanks for the quick answer!

avatar image zach-r-d · Aug 02, 2015 at 09:22 PM 0
Share

Sure thing! Glad it worked out.

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

[SOLVED] Why is this procedurally generated mesh not colliding? 2 Answers

MeshCollider Failure at high velocities 0 Answers

Correct position of overlapping meshes 0 Answers

Mesh Collider on a 2D mesh creating a 3D box? 0 Answers

Mesh Collision system seems to be buggy. 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