Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

How do i get the polygon(face) on the contact point by collider caster?

Discussion in 'Physics for ECS' started by amdroid135, Jun 21, 2021.

  1. amdroid135


    Apr 20, 2021
    1. How do I get the polygon(face) of the contact point (float3 by casting)?
    2. And how can I get the material information of polygon?
    • Unlike... "UnityEngine.Physics.RaycastHit", I found no triangle index for "Unity.Physics.ColliderCastHit". :confused:
    • So... I want to find out polygon, material, color in every way. o_O
    Code (CSharp):
    1.             if (Physics.Raycast(ray, out RaycastHit hit))
    2.             {
    3.                 int[] triangles = mesh.triangles;
    5.                 var vertIndex1 = triangles[hit.triangleIndex * 3 + 0];
    6.                 var vertIndex2 = triangles[hit.triangleIndex * 3 + 1];
    7.                 var vertIndex3 = triangles[hit.triangleIndex * 3 + 2];
    9.                 colorArray[vertIndex1] =;
    10.                 colorArray[vertIndex2] =;
    11.                 colorArray[vertIndex3] =;
    13.                 mesh.colors = colorArray;
    Last edited: Jun 21, 2021
  2. petarmHavok


    Nov 20, 2018
    ColliderCastHit.ColliderKey gives you the information of the hit collider (provide it to Collider.GetLeaf). However, it won't give you a proper mapping to the original triangles, as physics mesh doesn't map 1-1 to graphics mesh (there are simplifications that happen so some triangles may be merged, others may be dismissed and so on). Material is actually global, per collider, there is currently no option to set a per triangle material.

    Sorry I couldn't be of more help. There were other topics in the forum that discussed this in more depth, you might want to take a closer look at them.
    amdroid135 likes this.
  3. MNNoxMortem


    Sep 11, 2016
    @petarmHavok Collider.GetLeaf is internal (now?). What is the recommended way to do this currently?

    Edit: Found the answer. It is a bit cumbersome but essentially follow Query/Scripts/QueryTester.cs

    The only thing I am unsure is, how to get the triangle index back, as this gives me the position in local or world space.

    I just want to highlight, there is a comment that the method should be made "internal". I am sure there are many use cases for triangle information and I hope you still keep some replacement!
    Last edited: Mar 1, 2022