• 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 smokygreen · Oct 26, 2014 at 01:39 PM · meshpositionverticesworld coordinatestransform.transformpoint

Change vertex's world position

Hi, I'm trying to create bumps on my mesh. To do so, I have to tell every vertex of my mesh to change its "y" value. It worked fine, when I had one mesh. The problem started when I created chunks. So now I have few meshes and I need to get their world space coordinates. I read about it and used transform.TransformPoint(), but it doesn't work and I have no idea why. I spent hours on figuring out why it doesn't work and still I have no clue. This is what I got so far:

 Vector3 test = transform.FindChild("2").GetComponent<MeshFilter>().mesh.vertices[1];
         test += transform.TransformPoint(test);
         test.y += 5;
         transform.FindChild("2").GetComponent<MeshFilter>().mesh.vertices[1] = test;
         print(transform.FindChild("2").GetComponent<MeshFilter>().mesh.vertices[1]);


Child called "2" is my second chunk. I have its vertices, but when I modify its "y" nothing happens.

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 Bunny83 · Oct 26, 2014 at 01:54 PM

There are several problems in your code:

  • the vertices array is a property. By using the index operator directly on it, the property will return a copy of the array which you index to get the actual vertex in local space. That's not a problem since reading would work that way. However you can't write it back that way since you, again, work on a copy of the array. To actually submit the changes you have to assign the whole changed array back to the property.

  • Your local "test" variable receives the vertex position in local space. You then use transform.TransformPoint to transform it into world space. But now you add the local space and the worldspace position together which makes absolutely no sense.

  • When you try assign the vertex back to the mesh you have to bring the position back into local space or the position will be a total mess.

  • Repeatedly using FindChild / GetComponent and reading the vertices property is a pure performance killer.

So do something like this:

 // C#
 Mesh mesh;
 
 void Start()
 {
     mesh = transform.FindChild("2").GetComponent<MeshFilter>().mesh;
 }
 
 // wherever your code is
 {
     Vector3[] vertices = mesh.vertices;
     Vector3 V = transform.TransformPoint(vertices[1]);
     V.y += 5;
     vertices[1] = transform.InverseTransformPoint(V);
     mesh.vertices[1] = vertices;





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 smokygreen · Oct 26, 2014 at 02:18 PM 0
Share

Thank you very much! I totally forgot I have to change it back to local. It works great :)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to find the closest 4 verts/corners ( along with their orientation ) of a rectangular object in relationship with another object? 1 Answer

Editing vertices 1 Answer

get terrain vertices? 2 Answers

Why vertex positions appear (0.0, 0.0, 0.0) ? 1 Answer

Getting the world position of mesh vertices? 2 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