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 /
avatar image
0
Question by AntLewis · Apr 29, 2013 at 10:10 AM · meshvertex

Drawing from origin to each vertex on an object

Hi there, I'm doing a raycast to determine which object the mouse is over. Once this is done, I want check the vertex closest to that point within the object.

To start off with, just to ensure I understand what I'm doing, once an object is clicked, I'm looping through all the vertices and drawing a line between hte objects transform and the vertex as below:

     void GetandDraw ()
     {
         mesh = GetComponent<MeshFilter> ().mesh;
         vertices = mesh.vertices;
         DrawVertices ();
 
     }
     
     void DrawVertices ()
     {
         foreach (Vector3 vertex in vertices) {
             // A plane is stored in x and z, so we need to make the z the y
             Vector3 newVertex = new Vector3 (vertex.x, vertex.z, vertex.y);
             
             Debug.DrawLine (transform.position, transform.position + newVertex, Color.red, 10.0f);    
         }
  
     }
     



I dont understand the result I'm getting (the little bundle of read lines at the position of hte plane) - the 'shape' seems to be correct (the bundle of lines seem to be stretching towards each vertex), but come considerably short of reaching them. It looks as if I need to scale the vertex positions to get the result I'm after, but I don't understand why?

BTW I should add the solitary red line is the raycast.

alt text

grab.jpg (105.0 kB)
Comment
Add comment · Show 1
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 AntLewis · Apr 29, 2013 at 10:37 AM 0
Share

So this now makes it work: Vector3 newVertex = new Vector3 (vertex.x transform.localScale.x, vertex.z transform.localScale.z, vertex.y);

I don't understand why I need to scale this up - surely a vertex is an absolute position?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Vonni · Apr 29, 2013 at 10:49 AM

Vertex positions are stored locally, relative to the object. You need to convert them to World Space.

 foreach (Vector3 vertex in vertices) {
     Vector3 vertexWorldPos = vertObject.TransformPoint(vertex);
     Debug.DrawLine (transform.position, vertexWorldPos, Color.red, 10.0f);  
 }

I didnt read your entire post :) But im pretty sure this is what you want!

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 AntLewis · Apr 29, 2013 at 11:28 AM 0
Share

works a treat, thanks

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

13 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

Related Questions

Mesh breaks apart when using vertex displacement with shadergraph. 3 Answers

Reding from Mesh.colors doesn't display vertex colors of fbx 0 Answers

Create a Wireframe Shader with Shader Forge 1 Answer

Vertex snapping of any mesh at runtime 2 Answers

Boat physics 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