Search Unity

Question How to access material properties of a Collider?

Discussion in 'Physics for ECS' started by CookieStealer2, Nov 11, 2020.

  1. CookieStealer2

    CookieStealer2

    Joined:
    Jun 25, 2018
    Posts:
    119
    Hi,

    I want to access the friction of a rigidbody that I got through a raycast.
    This is the code I came up with. I get a pointer to the collider and can then access the friction by casting the pointer to a sphere collider pointer. This works fine if for example the hitBody is a quad collider (plane) but not for a mesh collider. I'm guessing this is because the mesh collider does not have a material property. How should I modify the code to be able to access the friction of any collider?

    Thanks!

    Code (CSharp):
    1. RigidBody hitBody = collisionWorld.Bodies[hit.RigidBodyIndex];
    2.  
    3. unsafe
    4. {
    5.     var ptr = (SphereCollider*)hitBody.Collider.GetUnsafePtr();
    6.     Debug.Log(ptr->Material.Friction);
    7. }
     
  2. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    You can check that a Collider has the CollisionType of Convex then cast to a ConvexColliderHeader e.g.

    Code (CSharp):
    1.        
    2.         var collider = collisionWorld.Bodies[hit.RigidBodyIndex].Collider;
    3.         if (collider.Value.CollisionType == CollisionType.Convex)
    4.         {
    5.             unsafe
    6.             {
    7.                 var header = (ConvexColliderHeader*)collider.GetUnsafePtr();
    8.                 var material = header->Material;
    9.                 material.Friction = newFriction;
    10.                 header->Material = material;
    11.             }
    12.         }
    13.  
    We are looking into make Materials available for composite colliders, as well making this sort of Material tweaking much easier without the need for unsafe blocks.
     
    CookieStealer2 likes this.
  3. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    Also, you can get a leaf collider by using the hit.ColliderKey and then you'll know for sure that the leaf collider is convex. Check CharacterControlerUtilities.cs, method is called IsTrigger().
     
    CookieStealer2 and steveeHavok like this.
  4. CookieStealer2

    CookieStealer2

    Joined:
    Jun 25, 2018
    Posts:
    119
    Thank you both,

    It's nice to hear that you are working to improve this. For now I managed to make it work by accessing the leaf collider as mentioned.

    Code (CSharp):
    1. public static Material GetMaterial(RigidBody rigidBody, ColliderKey colliderKey)
    2. {
    3.     Material material;
    4.     unsafe
    5.     {
    6.         Collider* colliderPointer = (Collider*)rigidBody.Collider.GetUnsafePtr();
    7.         ChildCollider childCollider;
    8.         colliderPointer->GetLeaf(colliderKey, out childCollider);
    9.         ConvexCollider* childColliderPointer = (ConvexCollider*)childCollider.Collider;
    10.         material = childColliderPointer->Material;
    11.     }
    12.     return material;
    13. }
     
    Goularou and steveeHavok like this.