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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by ZAxisTechnology · Oct 19, 2014 at 07:55 PM · meshpositionpositioningmesh manipulationgamobject

Creating new GameObject for Procedural Mesh has wrong Transform.position

Hey all, So Ive read that the anchor point of an object's transform cannot be directly changed, but Im looking for a workaround for a project of mine.
Basically Im creating a new empty game object called "Tile" in a loop. I then assign the mesh vertices and triangles etc and end up with exactly what I wanted, except for the fact that each one of the created Tile objects has its transform.position at the origin. I tried setting the position directly after creating the new GameObject before assigning the mesh vertices and tris but this distorted my result. I have the center points of each tile mesh already stored in a list and I use that point to generate the mesh. Is there any way of telling this new gameobject that its transform's position should be at this center point without translating the mesh away from its desired position?
Heres some of my code and some screen shots.
The Vector3 vertices[i] represents the desired center point of the game objects transform.position.

Result when I DO NOT set the GameObject's transform.position to vertices[i]

Result when I DO set the GameObject's transform.position to vertices[i]

 private void generateSubMeshes(List<Vector3> centers, List<Vector3> vertices){
         //Generate the hex/pent mesh for each vertex in the main mesh by associating it to its surrounding triangle centers
         for(int i = 0; i < vertices.Count; i++){
             GameObject tile = new GameObject ("Tile");
             Mesh submesh = new Mesh ();
             //tile.transform.position = vertices[i];
             List<Vector3> submeshVs = new List<Vector3>();

 //Some stuff happens here to generate the mesh

             tile.AddComponent<MeshFilter> ();
             tile.AddComponent<MeshRenderer>().material.color = Color.white;
             tile.GetComponent<MeshFilter>().mesh = submesh;
             submesh.RecalculateBounds();
             submesh.RecalculateNormals();
             tile.AddComponent<Tile>();


screenshot (6).png (144.6 kB)
screenshot (5).png (177.6 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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Ozy51 · Apr 23, 2015 at 09:08 PM

Hope it's not to late for an answer but if you give each vertice a position it will be assigned as if you are on the origin. And if you apply a transformation to that Tile(in your case) you are transforming that origin, and the vertices have a position according to that as i said before, that will make that separation that you see in the second screenshot. So as a solution you may have to do the math and take from the vertices position what you are adding on the transform. It's actually confusing to explain without code, but hope it helps.

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
0

Answer by GDiSalvo · May 22, 2020 at 12:34 AM

Hi @Ozy51 (and @ZAxisTechnology )! It's waaaay too late for a reply to your answer, but this is the only post I've found with the same problem I have and with an answer that helped me! I was thinking on making a new post for this topic but first I will try to find if you have more information about this issue.

I have an example simpler than the one from the OP. I'm making a procedural grid of quads (so, it's a plane with square tiles, not a sphere of hexagons). I post my code below to create a single tile: I create a quad and assign it to a GO, this is simple and works fine with all the tiles in the right position but all the GOs transform.position are at the zero position. The problem is that I need to add a dynamic destroy of this objects when they are far from the player, and I don't have a tile transform.position to compare!

I've added a vertices' repositioning as you mentioned and now I have the correct positioning and the transform.position correct values, but it's an ugly solution! ?? Maybe you or anyone know about a better option. Thank you!

         public void CreateQuadMesh(GameObject segmentPrefab, Vector3 pointX0Z0, Vector3 pointX1Z0, Vector3 pointX0Z1, Vector3 pointX1Z1)
         {
             Vector3[] vertices = new Vector3[4] { pointX0Z0, pointX1Z0, pointX0Z1, pointX1Z1 };
             mesh = new Mesh();
             mesh.vertices = vertices;
             mesh.triangles = tris;
             mesh.RecalculateNormals();
             mesh.uv = uv;
             tempQuad = Instantiate(segmentPrefab, Vector3.zero, Quaternion.identity);
             tempQuad.GetComponent<MeshFilter>().mesh = mesh;
     
             // Required added steps to keep position in transform:
             // - doing nothing = right position but transform.position zero
             // - assign transform.position only = GO with right transform.position but mesh moves position
             tempQuad.transform.position = pointX0Z0;
             vertices[0] = pointX0Z0 - pointX0Z0;
             vertices[1] = pointX1Z0 - pointX0Z0;
             vertices[2] = pointX0Z1 - pointX0Z0;
             vertices[3] = pointX1Z1 - pointX0Z0;
             mesh.Clear();
             mesh.vertices = vertices;
             mesh.triangles = tris;
             mesh.RecalculateNormals();
             mesh.uv = uv; // this maybe is the only one not required
             //
             tempQuad.GetComponent<MeshCollider>().sharedMesh = mesh;
         }
 
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

Combine Meshes Problem 2 Answers

X and Y positioning(buttons). How do they work and why does building the game change their positions? 0 Answers

How to place an object near N number of objects in order to avoid overlap? 0 Answers

Create a droplet wave effect using Mesh deformation 0 Answers

How to control position of GUITexture when its inside a prefab 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