x


how to change physics material of a colider in runtime

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?

more ▼

asked Apr 01 '10 at 05:25 PM

raminsh gravatar image

raminsh
173 15 15 21

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

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");
}
more ▼

answered Nov 08 '10 at 11:02 AM

Robotron18 gravatar image

Robotron18
113 3 5 12

This doesn't seem to work for me. I keep getting an error message saying the editor is expecting a semicolon on line 78, which ends up being in between the E and the S in the word Resources.

Anyone have any ideas?

Dec 08 '11 at 07:54 AM Cnotey
(comments are locked)
10|3000 characters needed characters left

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.).

more ▼

answered Apr 01 '10 at 05:44 PM

Eric5h5 gravatar image

Eric5h5
80.1k 41 132 519

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Oct 13 '10 at 09:38 PM

Vadelma gravatar image

Vadelma
1

(comments are locked)
10|3000 characters needed characters left

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[i]
more ▼

answered Apr 01 '10 at 05:51 PM

KvanteTore gravatar image

KvanteTore
1.5k 8 15 33

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1865
x1683
x809
x117

asked: Apr 01 '10 at 05:25 PM

Seen: 3577 times

Last Updated: Dec 08 '11 at 07:54 AM