• 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 Neuno · Sep 19, 2017 at 10:52 PM · scripting problemmesh3dhexagonhex

Can't generate 3D meshes via script

So, i created a really simple script (suck at programming) to generate a 3D mesh of an hexagon, and when i run it, it looks like the mesh gets flattened out, so instead of getting a 3D mesh, i get a 2D mesh of the hexagon. The fun part is that my script actually works, it generate a 3D mesh (At leats, that's what my mesh filter say; And my vertices are in the right position, i've checked hundreds of times), but for some reason it gets flattened on the y plane (Vertically). It looks like i can't extend my mesh's triagles further from the vector z of my trasform position. My project is set to 2D, but i tried to make a new 3D project to see if that was the problem. It wasn't. What did i get wrong? Here's some images, probably you'll understand better.

Here's what i get:

alt text

Here's what the mesh should look like(i've got 20 triangles and 12 vertices, the right number: Maybe the normals are a bit messed up):

alt text

Here's the script:

     private void generateMesh(Material standard)
     {
 
         MeshRenderer hexRend = gameObject.AddComponent<MeshRenderer>();
         MeshFilter hexFilt = gameObject.AddComponent<MeshFilter>();
         Mesh hexMesh = hexFilt.mesh;
 
         MeshCollider hexCol = gameObject.AddComponent<MeshCollider>();
         hexCol.convex = true;
 
         Rigidbody meshRig = gameObject.AddComponent<Rigidbody>();
         meshRig.isKinematic = true;
 
 
 
         hexRend.material = standardMaterial;
 
         hexMesh.Clear(); hexMesh.ClearBlendShapes();
 
 
 
         float scale = transform.localScale.x;
         float xxx = scale + (scale / 1.4f);
 
         Vector3[] corners = new Vector3[12];
 
         // lato alto dell'esagono
 
         corners[1] = new Vector3(scale, 0);
         corners[2] = new Vector3(scale + (scale / 2), xxx / 2);
         corners[3] = new Vector3(scale, xxx);
         corners[4] = new Vector3(0, xxx);
         corners[5] = new Vector3(-(scale / 2), xxx / 2);
         
         for (int x = 6; x < corners.Length; x++)
         {
             corners[x] = corners[x - 6];
 
             corners[x] += new Vector3(0, 0, scale);
         }
 
 
         hexMesh.vertices = corners;
 
         hexMesh.triangles = new int[]
         {
             0, 4, 5,
             0, 3, 4,
             0, 1, 3,
             1, 2, 3,
 
 
             0, 6, 1,
             1, 6, 7,
 
             1, 2, 7,
             2, 7, 8,
 
             2, 3, 8,
             3, 8, 9,
 
             3, 9, 10,
             3, 4, 10,
 
             4, 10, 11,
             4, 5, 11,
 
             0, 5, 6,
             5, 6, 11,
 
 
             6, 10, 11,
             6, 9, 10,
             6, 7, 9,
             7, 8, 9
         };
 
         hexMesh.RecalculateNormals();
     }

I've tried to re-write the script completely so the hexagon get generated on the z plane, instead of the y plane, but it get flattened out on the z plane (So horizontally, instead of vertically; My hexagon got generated, but 2D on the z plane): I've tried to create a new unity project, to see if maybe i messed something up: I've tried to rotate the vertices, to they face the camera: I've tried to attach the script to a primitive 3D object, to see if maybe was the trasform of a new empty object (just because the project is set to 2D) to flatten the mesh: But nothing worked. I don't know what to do anymore. Please... Help. Thanks in advance.

btw i'm not an english native.

scriptttt.png (159.9 kB)
scriptttt2.png (122.1 kB)
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
Best Answer

Answer by FortisVenaliter · Sep 19, 2017 at 11:13 PM

The z-scale on your transform is zero.

Comment
Add comment · 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 Bunny83 · Sep 19, 2017 at 11:26 PM 1
Share

Well spotted in the screenshot :)

There's another thing strange. Why would one use the x scale to calculate the vertex positions? That would mean when you scale your object you actually get the square of the scale. For example if you have a scale of "2" you use a local position of "2" which is then scaled by the same factor of "2" so the vertex ends up at the worldspace position of 4 in relation fo the object origin.

However if the scale would be 4 you place the local vertex at 4 which is then scaled by 4 so you end up at 16. If you plan to use the object scale to define the size, just define your localspace vertices with constant values, just like the default cube mesh which uses +- 0.5 on all axis.

avatar image Neuno Bunny83 · Sep 20, 2017 at 02:25 PM 0
Share

I use the x scale because i use another script to generate a grid, wich create a finite number of new game object and attach this script (that generate the mesh) to them. Just because i didn't wanted to create another public integer variable. It's a silly solution, i know. I am a noob at programming, i could have done so many things better, but i'm trying to learn alone without tutorials, so that's why. Didn't notice what i was doing, thank you, i appreciated the comment.

avatar image Neuno · Sep 20, 2017 at 01:58 PM 0
Share

Oh my god... I swear, i've spent 2 days trying to figure this out, and the problem was just that little value... How stupid. Thank you, i think it would have take other two days to find out by myself.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

132 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 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 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 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 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 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

Player model physics doesn't seem to work properly. Gravity issues maybe? 0 Answers

Md5mesh uvs and vertices bugging 0 Answers

How to make sure certain tiles dont spawn behind eachother 1 Answer

UDP app signature? 1 Answer

How to snap a Prefab to the normal of an irregular mesh 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges