Hi everyone,

I’ve written a script that will allow a user to click on a spot of the surface of a mesh (collider), and then follow the corresponding vertex as it is being animated.

I’ve based the code on this earlier post:

I’m imaging this as I construct a new gameObject and sticking this to the midpoint of the vertex.

Now, I can follow the position of the midpoint of the vertex via TransformPoint, however I am struggling to let the GameObject also follow its rotation. I’ve tried TransformDirection in a similar manner to TransformPoint, but I just can’t make it work.

I would imagine the GameObject could represent a bounding box on the surface of the vertex and follow its overall rotation.

Attached is a picture to show that while the position can be tracked as the vertex is moving, the rotation is not (the gameobject is just a primitive cube).

Any suggestions?

Here’s the code:

```
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Make : MonoBehaviour {
List<GameObject> objs = new List<GameObject>();
List<RaycastHit> hits = new List<RaycastHit>();
void Update () {
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit))
{
MeshCollider meshCollider = hit.collider as MeshCollider;
// Make new primitive and stick to hit location
GameObject obj = GameObject.Instantiate(Resources.Load("track", typeof(GameObject)), hit.point, Quaternion.identity) as GameObject;
obj.name = "track "+i;
i++;
obj.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
obj.transform.parent = transform;
objs.Add (obj);
hits.Add (hit);
}
}
}
void LateUpdate ()
{
for (int i = 0; i < objs.Count; i++) // Loop through List to update trackers
{
// Init position and rotation vars
Vector3 p0;
Vector3 p1;
Vector3 p2;
Vector3 rp0;
Vector3 rp1;
Vector3 rp2;
// Init other vars
Mesh mesh;
Vector3[] vertices;
int[] triangles;
GameObject obj = objs[i];
RaycastHit hit = hits[i];
mesh = new Mesh();
// Get mesh from skinned mesh renderer (animated mesh)
this.gameObject.GetComponent<SkinnedMeshRenderer>().BakeMesh(mesh);
vertices = mesh.vertices;
triangles = mesh.triangles;
// Extract local space vertices that were hit
p0 = vertices[triangles[hit.triangleIndex * 3 + 0]];
p1 = vertices[triangles[hit.triangleIndex * 3 + 1]];
p2 = vertices[triangles[hit.triangleIndex * 3 + 2]];
// Transform local space vertices to world space
Transform hitTransform = hit.collider.transform;
p0 = hitTransform.TransformPoint(p0);
p1 = hitTransform.TransformPoint(p1);
p2 = hitTransform.TransformPoint(p2);
rp0 = hitTransform.TransformDirection(p0);
rp1 = hitTransform.TransformDirection(p1);
rp2 = hitTransform.TransformDirection(p2);
// Update position of tracker based on position of vertex
obj.transform.position = (p0+p1+p2)/3;
// Update rotation of tracker based on position of vertex - THIS IS NOT WORKING
obj.transform.rotation = Quaternion.LookRotation((rp0 + rp1 + rp2) / 3, Vector3.up); ;
}
}
}
```