Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by mustang4484 · Nov 22, 2019 at 09:48 AM · gameobjectmeshmesh vertices

Get vertex coordinates from gameobject mesh

Hi guys i would like to get all vertex cordinates of a mesh gameobject . With my script i get always the same coordinates for 24 times (the mesh is a simple box).

Any suggestion ?

Best regards.

 void Start ()
 {
     GameObject gameObject = GameObject.Find("head");
     Mesh mesh = gameObject.GetComponent<MeshFilter>().mesh;
     Vector3[] vertices = mesh.vertices;

     for (var i = 0; i < vertices.Length; i++)
     {
         direction = transform.TransformPoint(vertices[i]);
         Debug.Log(direction);
     }
 }
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
0

Answer by vlee116 · Jul 18, 2021 at 09:52 PM

     GameObject myObject = GameObject.Find("head");
     Mesh mesh = myObject.GetComponent<MeshFilter>().mesh;
     Vector3[] vertices = mesh.vertices;

     for (var i = 0; i < vertices.Length; i++)
     {
         Debug.Log(vertices[i]);
         var direction = transform.TransformPoint(vertices[i]);
         Debug.Log(direction);
     }

You're pretty close @mustang4484, your first problem could be that by using "gameObject" as your object name you could be creating some namespace issues, but I haven't actually tested your code so I'm not sure. Either way, it's good practice to just use a different name for your GameObject. Secondly, you're not properly defining the 'direction' variable. You need to either specify a data type or just use 'var'. Funnily enough I'm doing the exact same thing you are in my current project! I stumbled across your question while doing research

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 andrew-lukasik · Jul 17, 2021 at 08:53 PM

Never create variables of type GameObject with name gameObject. The same goes for Transforms names transform. You will thank yourself later.

Also there is a bug here - your not suppose to transform those vertices by transform (which is relative to script location and not head transform itself).


This vector is no direction:

 direction = transform.TransformPoint(vertices[i]);

but world position of a given vertex:

 Vector3 vertexWorldPosition = transform.TransformPoint( vertices[i] );

 void Start ()
 {
     GameObject headGo = GameObject.Find("head");
     Transform headTransform = headGo.transform;
     Mesh headMesh = headGo.GetComponent<MeshFilter>().sharedMesh;
     Vector3[] vertices = mesh.vertices;

     var text = new System.Text.StringBuilder();
     int numVertices = vertices.Length;
     for( int i=0 ; i<numVertices ; i++ )
     {
         Vector3 vertexWorldPosition = headTransform.TransformPoint( vertices[i] );
         text.AppendLine($", ({vertexWorldPosition.x:0.00},{vertexWorldPosition.y:0.00},{vertexWorldPosition.z:0.00})");
     }
     Debug.Log($"vertices: ( {text} )");
 }
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

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

How to link up vertices to form the triangles in a Fibonacci Sphere? 0 Answers

Can you turn a prefab/gameobject into a mesh? 0 Answers

Assign an image as a texture to a mesh 1 Answer

How important is the order of Vertices in the mesh array that the triangles point to? 0 Answers

Split cylinder into multiple gameobjects 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