Finding the location of a moving Vertex with Raycasting

I have a jellyfish like object that has a slowly changing shape. The vertices are being moved a little bit with some perlin noise by this script:

var scale = 0.1;
var speed = 0.5;
var recalculateNormals = false;

private var baseVertices : Vector3[];
private var noise : Perlin;

function Start ()
{
	noise = new Perlin ();	

}

function Update () {
	var mesh : Mesh = GetComponent(MeshFilter).mesh;
	
	if (baseVertices == null)
		baseVertices = mesh.vertices;
		
	var vertices = new Vector3[baseVertices.Length];
	
	var timex = Time.time * speed + 0.1365143;
	var timey = Time.time * speed + 1.21688;
	var timez = Time.time * speed + 2.5564;
	for (var i=0;i<vertices.Length;i++)
	{
		var vertex = baseVertices[i];
				
		vertex.x += noise.Noise(timex + vertex.x, timex + vertex.y, timex + vertex.z) * scale;
		vertex.y += noise.Noise(timey + vertex.x, timey + vertex.y, timey + vertex.z) * scale;
		vertex.z += noise.Noise(timez + vertex.x, timez + vertex.y, timez + vertex.z) * scale;
		vertices[i] = vertex;

	}
	
	mesh.vertices = vertices;
	
	if (recalculateNormals)	
		mesh.RecalculateNormals();
	mesh.RecalculateBounds();

}

I need to click on a spot on the surface of this jellyfish and have a sphere stick to the side of this gooey, changing shape. I have attached a mesh collider to the jellyfish and use this script to place the sphere:

// Attach this script to a camera and it will
// draw a debug line triangle triangle
// at the triangle where you place the mouse.

var sphere:Transform;

function Update () {
// Only if we hit something, do we continue
var hit : RaycastHit;
if (!Physics.Raycast (camera.ScreenPointToRay(Input.mousePosition), hit))
return;

// Just in case, also make sure the collider also has a renderer
// material and texture
var meshCollider = hit.collider as MeshCollider;
if (meshCollider == null || meshCollider.sharedMesh == null)
return;

var mesh : Mesh = meshCollider.sharedMesh;
var vertices = mesh.vertices;
var triangles = mesh.triangles;

// Extract local space vertices that were hit
var p0 = vertices[triangles[hit.triangleIndex * 3 + 0]];
var p1 = vertices[triangles[hit.triangleIndex * 3 + 1]];
var p2 = vertices[triangles[hit.triangleIndex * 3 + 2]];

// Transform local space vertices to world space
var hitTransform : Transform = hit.collider.transform;
p0 = hitTransform.TransformPoint(p0);
p1 = hitTransform.TransformPoint(p1);
p2 = hitTransform.TransformPoint(p2);

// Display with Debug.DrawLine
Debug.DrawLine(p0, p1);
Debug.DrawLine(p1, p2);
Debug.DrawLine(p2, p0);
sphere.position = p0;
}

My problem is that the mesh collider does not shift its shape with the visible mesh of the jellyfish, so the sphere stays in one place, stuck to the unmoving mesh collider.

How do I get the sphere to move with the jellyfish? How do I use a triangleIndex or a vertex number and get the location of the moving jellyfish vertex?

Any help is appreciated.

Thanks!

This is because you are placing it to the Vertex on the Collision Mesh, and not the Visual mesh.

Now, you are in luck, because the indices are identical.
So retrieve the Indices from the CollisionMesh, and then use those same Indices to retrieve the Vertex out of the MeshFilter Mesh.

However after that you will likely need to maintain it’s update to constantly move to that correct Vertex.

i.e. Mesh = hit.gameObject.GetComponent().mesh;

While still using the hit.TriangleIndices.

Ntero gets today’s Rock Star scripting help award!

Yay![/b]
:smile:
The actual line of code in .js in context is:

** **var mesh : Mesh = meshCollider.GetComponent(MeshFilter).mesh;** **