• 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 Rabwin · Dec 04, 2011 at 01:22 PM · bugverticesmeshfilter

mesh.vertices position not correct

I wanted to use vertices on meshes to create specific and accurate visual effects but am unable as I got stuck. It seems that I can't get the correct or exact position of a mesh's vertices. Am I not using this function correctly?

This is the code (C#) I am using:

 public Transform target;
 private Mesh mesh;
 private Vector3[] vertices = new Vector3[0];
 
 void Update()
 {
     mesh = target.GetComponent<MeshFilter>().mesh;
     print(target.GetComponent<MeshFilter>().mesh.vertices[0]);
     Debug.DrawLine(transform.position, mesh.vertices[0]);
 }

I have my target (A cube) sitting at 0,0,5. It's first vertice I am attempting to debug should be sitting at 0.5,-0.5,5.5.

From the print (it says 0.5, -0.5, 0.5) and the drawline debug goes there too. It seems it is at the origin point 0,0,0 rather than where the object is actually at.

I have also tried this, which is the same script but on the cube object pointing from it's vertice to the camera, and the same result.

 void Update()
 {
     print(GetComponent<MeshFilter>().mesh.vertices[0]);
     Debug.DrawLine(GetComponent<MeshFilter>().mesh.vertices[0], Camera.mainCamera.transform.position);
 }

Any help is much appreciated, whether I have to use another solution or fix this one, I don't mind.

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
2
Best Answer

Answer by Cameron_SM · Dec 04, 2011 at 02:19 PM

Vertex position are in local space, as in the vertex positions of the mesh do not update or change as you move an object around in the world, only the object's main transform is updated. If the game engine updated the vertex position of every single mesh every frame we'd never be able to have millions of polygons moving around in real time.

In order to get the world space position of a vertex instead of the local/object space position, simply add the object's transform.position to the vertex position.

 Mesh mesh = target.GetComponent<MeshFilter>().mesh;
 Debug.DrawLine(Camera.mainCamera.transform.position, transform.position + mesh.vertex[0]);
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 Rabwin · Dec 08, 2011 at 03:44 PM 0
Share

I understand now, thanks for the help.

$$anonymous$$y problem now is that I don't know the vertex location if the object is rotated.

I'm pretty bad at maths and don't know how to get the new position.

avatar image golay · Jun 26, 2020 at 06:05 PM 0
Share

It seems that the vertices are not scaled. I have a cube with scale (3, 0.1, 3) and it has the same set of vertices that a unit cube positioned at origin would have.

avatar image
1

Answer by Graham-Dunnett · Dec 04, 2011 at 01:28 PM

Presumably your cube has a transform attached to it which you are using to translate the cube in the Z direction. You are getting the raw vertex positions, and will want to transform them by the same matrix attached to the cube. Use Transform.TransformPoint:

http://unity3d.com/support/documentation/ScriptReference/Transform.TransformPoint.html

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 Rabwin · Dec 04, 2011 at 01:46 PM 0
Share

I'm not sure I understand how to use this. I've placed this function in both the Awake and Update functions before attempting to draw a line or print the vertex locations and it still acts like transform position was 0,0,0.

transform.TransformPoint(new Vertex3(2,0,0)); is what I used for example.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Number of mesh vertices increase when imported into unity 1 Answer

Extruding faces of a mold. Need to properly assign vertices to triangles 0 Answers

Mesh Vertices Welding 0 Answers

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

Assigning SharedMesh is creating an unreferenced instance 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