Question by 
               h0neyfr0g · Feb 17, 2020 at 02:28 AM · 
                meshmaterialmaterialssharedmesh  
              
 
              Use a Raycast to get the Material from a shared Mesh (ProBuilder Object)
Hello Everyone,
I am trying to use a Raycast to determine the material below the player. However, the object is a ProBuilder object, using a mesh with shared materials. I cannot seem to figure out how to retrieve the correct material... Please help! :)
Thanks so much
void Update() { RaycastHit hit;
     if (Physics.Raycast(transform.position, -Vector3.up, out hit))
     {
         Debug.DrawLine(transform.position, hit.point, Color.cyan);
         Mesh m = GetMesh(gameObject);
         if (m)
         {
             int[] hittedTriangle = new int[]
             {
                         m.triangles[hit.triangleIndex * 3],
                         m.triangles[hit.triangleIndex * 3 + 1],
                         m.triangles[hit.triangleIndex * 3 + 2]
             };
             for (int i = 0; i < m.subMeshCount; i++)
             {
                 int[] subMeshTris = m.GetTriangles(i);
                 for (int j = 0; j < subMeshTris.Length; j += 3)
                 {
                     if (subMeshTris[j] == hittedTriangle[0] &&
                         subMeshTris[j + 1] == hittedTriangle[1] &&
                         subMeshTris[j + 2] == hittedTriangle[2])
                     {
                         Debug.Log(string.Format("triangle index:{0} submesh index:{1} submesh triangle index:{2}", hit.triangleIndex, i, j / 3));
                     }
                 }
             }
         }
         _nameGroundObject = hit.collider.gameObject;
     }        
 }
 static Mesh GetMesh(GameObject go)
 {
     if (go)
     {
         MeshFilter mf = go.GetComponent<MeshFilter>();
         if (mf)
         {
             Mesh m = mf.sharedMesh;
             if (!m) { m = mf.mesh; }
             if (m)
             {
                 return m;
             }
         }
     }
     return (Mesh)null;
 }
               Comment
              
 
               
              Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                