I am trying to find the distance between a raycast hit point and vertices on a mesh. When I click the mouse button my character sends out a raycast which hits an object, once this happens I put the vertices in the object into a list. Now I want to find the distance between each vertex and the hit.point of the raycast.
Code:
using UnityEngine;
using System.Collections;
public class DistanceTest : MonoBehaviour {
RaycastHit hit;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 fwd = transform.TransformDirection(Vector3.forward);
if(Input.GetMouseButtonDown(0)){
if (Physics.Raycast(transform.position, fwd, out hit, 100.0F)) {
distance();
}
}
}
void distance(){
MeshFilter mf = hit.collider.GetComponent<MeshFilter>();
Mesh mesh = mf.mesh;
Vector3[] vertices = mesh.vertices;
print(vertices.Length);
int i = 0;
while(i < vertices.Length){
print(Vector3.Distance(transform.InverseTransformPoint(vertices*), transform.InverseTransformPoint(hit.point)));*
-
i++;*
-
}*
- }*
}