Before updating to Windows 10 you may need to return your Pro license. More information here

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)
more ▼

asked Apr 29, 2013 at 10:10 AM

avatar image

580 210 195 212

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?

Apr 29, 2013 at 10:37 AM AntLewis
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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!

more ▼

answered Apr 29, 2013 at 10:49 AM

avatar image

1k 7 8 16

works a treat, thanks

Apr 29, 2013 at 11:28 AM AntLewis
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Apr 29, 2013 at 10:10 AM

Seen: 558 times

Last Updated: Apr 29, 2013 at 11:28 AM