• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by raminsh · Apr 01, 2010 at 05:25 PM · physicscollidermaterialbounce

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Robotron18 · Nov 08, 2010 at 11:02 AM

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");
}
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cnotey · Dec 08, 2011 at 07:54 AM 0
Share

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?

avatar image
5

Answer by Eric5h5 · Apr 01, 2010 at 05:44 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by sathya _m · Oct 31, 2013 at 12:46 PM

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();

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by KvanteTore · Apr 01, 2010 at 05:51 PM

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]
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Vadelma · Oct 13, 2010 at 09:38 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

what value should i change to get football from material 1 Answer

Getting realistic bounces using physics materials. 2 Answers

Disable bouncing effect 0 Answers

Two even colliders causing bounce at transition 1 Answer

Cant find options to change physic material to bouncy 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges