Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Getting realistic bounces using physics materials. 2 Answers

Two even colliders causing bounce at transition 1 Answer

Disable bouncing effect 0 Answers

Changing physical material impossible? 1 Answer

Cant find options to change physic material to bouncy 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges