hello I was wondering if I can change the physics material of an object's colider. for example, changing it from metal to bouncy. is it even possible?
Yes, you can change `collider.material`, by either assigning a PhysicMaterial:
var myMaterial : PhysicMaterial;
function Start () {
collider.material = myMaterial;
}
or changing individual properties (`collider.material.bouncyness`, etc.).
You can pre-create the desired material in the editor Unity or use available in "Assets/Standard Assets/Physic Materials". Then place it into "Assets/Resources/PhysicMaterials". And for runtime change:
gameObject.collider.material = (PhysicMaterial)Resources.Load("PhysicMaterials/Wood");
Use the following code to assign when you import resources:
public void OnPostprocessModel(GameObject importedObject)
{
CapsuleCollider capColl = importedObject.AddComponent<CapsuleCollider>();
capColl.material = (PhysicMaterial)Resources.Load("PhysicMaterials/Wood");
}
In c#
this.collider.material=new PhysicMaterial(“Wood”);
But don’t forgot we need to refresh rigidbody for applying changes.
before change:
rigidbody.isKinematic = true ;
rigidbody.Sleep();
after change:
rigidbody.isKinematic = false ;
rigidbody.Wakeup();
Yes, that is possible. You can set the active `PhysicMaterial` by setting `collider.material`. To make sure that the materials are included in the scene, you can create an array of PhysicMaterials which you set at design time
Make a public array of PhysicMaterials in the class
public PhysicMaterial materials[]
If you fill this list from the inspector in the Unity editor, you can then assign one of the materials at runtime:
collider.material = materials
*```*
Be careful if you decide to change the properties of a PhysicMaterial object at runtime instead of/without changing the sharedMaterial (or material) property of the colliders using it. In some cases, the physics engine will continue to use the old properties of a PhysicMaterial object. See this forum post of mine for more information.