x


Detect vertex hit and its neighbors by RaycastHit??

Is it possible to detect the vertex hit and its neighbors by RaycastHit?

more ▼

asked Nov 02 '11 at 08:06 AM

ina gravatar image

ina
3.4k 500 554 616

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Loius is almost right. It's the triangle index. By multiplying it by 3 you get the index of the first vertex index. It should be like this:

// Should work in C# and UnityScript
// ...
var MC = hit.collider as MeshCollider;
if (MC != null)
{
    var mesh = MC.sharedMesh;
    var index = hit.triangleIndex * 3;

    var hit1 = mesh.vertices[mesh.triangles[index    ]];
    var hit2 = mesh.vertices[mesh.triangles[index + 1]];
    var hit3 = mesh.vertices[mesh.triangles[index + 2]];
}

Those are the 3 vertices that surround the hit point.

Keep in mind that hit.triangleIndex only works with a MeshCollider. No other collider will give you that information since they are pure mathematical colliders which are not based on triangles.

more ▼

answered Nov 28 '12 at 11:22 PM

Bunny83 gravatar image

Bunny83
46.9k 12 50 210

(comments are locked)
10|3000 characters needed characters left

RaycastHit has a property called triangleIndex that tells you what the index of the triangle that was hit. I can't be much more concrete about this since I still couldn't even "hit" my procedural mesh, but that's other story. When I have some success, if you are still doubting about this I will try to be more helpful.

more ▼

answered Nov 28 '12 at 09:30 PM

MrVerdoux gravatar image

MrVerdoux
146 1 3

That would be an index into the mesh.vertices array:

hit1 = mesh.vertices[triangleIndex*3];
hit2 = mesh.vertices[triangleIndex*3+1];
hit3 = mesh.vertices[triangleIndex*3+2];
Nov 28 '12 at 10:15 PM Loius
(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

By RSS:

Answers

Answers and Comments

Topics:

x1581
x307
x166
x141
x141

asked: Nov 02 '11 at 08:06 AM

Seen: 1076 times

Last Updated: Nov 28 '12 at 11:23 PM