Search Unity

Getting Material Struck from Unity.Physics.RaycastHit

Discussion in 'Physics for ECS' started by Stricklandk0163, Aug 26, 2019.

  1. Stricklandk0163

    Stricklandk0163

    Joined:
    Jun 5, 2019
    Posts:
    2
    Hello,

    I'm using CollisionWorld's CastRay method to raycast within a job. I want to be able to figure out which material was struck in each of the resulting RaycastHits.

    I haven't been able to find clear way to do this. The most promising thing I've found is RaycastHit's ColliderKey which seems to link back to the collision mesh. That being said I haven't found a way to leverage the ColliderKey to figure out any details about what submesh was hit or the material of that submesh.

    Does anyone know how to do this? Any help would be greatly appreciated.

    Thanks!
     
  2. digitaliliad

    digitaliliad

    Joined:
    Jul 1, 2018
    Posts:
    64
    I'm going to assume that this material is present on the render mesh of the entity that the raycast hits.

    I can quickly think of two circuitous, main-thread ways to do this: either by matching the shared component data index, or by matching the shared component data itself (doing it by index is probably best):

    Code (CSharp):
    1. // Get the shared component data index from raycast hit
    2. var sharedIndex = EntityManager.GetSharedComponentDataIndex(CollisionWorld.Bodies(RaycastHit.RigidBodyIndex).Entity);
    3.  
    4. // Get the shared component data, itself, from raycast hit
    5. var sharedData = EntityManager.GetSharedComponentData(CollisionWorld.Bodies(RaycastHit.RigidBodyIndex).Entity);
    But, since you want to do this in a job, you'll need to find to find an alternative method for finding the shared component data index.

    Maybe check out Sargon's article on shared component data: https://gametorrahod.com/everything-about-isharedcomponentdata/
     
    Stricklandk0163 likes this.
  3. Stricklandk0163

    Stricklandk0163

    Joined:
    Jun 5, 2019
    Posts:
    2
    Thanks for getting back to me so quickly!

    Yes, you're correct in assuming that the material is present on the render mesh. In addition there are some cases where one of my meshes contains several submeshes with each of these submeshes having a different material type.

    My assumption would be that in the case of the answer above I would only be able to identify the material if there is only one material per mesh since I wouldn't know which part of the rigid body was hit. Is this the case? If so, do you have any recommendations on how to get around this?
     
  4. digitaliliad

    digitaliliad

    Joined:
    Jul 1, 2018
    Posts:
    64
    I think you need to reconsider how you're building your entities to achieve what you want: take a look at the conversion workflow; that should give you an idea how you might build complex confederations of entities such that each individual render mesh has its own entity. I don't know how to distinguish between submeshes on the same entity using unity physics and the hybrid renderer