Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
3 captures
13 Jun 22 - 14 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
0
Question by klolololol · Apr 17, 2015 at 09:58 AM · materialphysics2dcollider2dfriction

Changing friction from code not working (2D)

I want to change a collider's physic's material from code, I use the code below. I see the material's friction changing in the inspector, however the collider behaves as if the friction wouldn't change.

     void checkOnGround() {
         Transform t;
         foreach(Collider2D c in collidingTiles) {
             t = c.transform;
             if(t.position.y - transform.position.y < -Misc.TILE_SIZE * 0.75f) {
                 onGround = true;
                 myCollider.sharedMaterial.friction = 0.8f;
                 return;
             }
         }
         onGround = false;
         myCollider.sharedMaterial.friction = 0f;
         return;
     }
Comment
Add comment · Show 4
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 AlwaysSunny · Apr 17, 2015 at 08:23 AM 0
Share

The physics material of the other object matters too. Also note the friction "combine" settings of both must reflect your desired outcome.

avatar image klolololol · Apr 17, 2015 at 08:55 AM 0
Share

That shouldn't be the problem. If I change the friction in the inspector to 0 than run the game, it's slippery, if I change it to 0.8 then it's sticky, no matter what the code does.

avatar image AlwaysSunny · Apr 17, 2015 at 11:13 PM 0
Share

"I see the material's friction changing in the inspector, however the collider behaves as if the friction wouldn't change."

Confirm that you're saying the code above appears to successfully change the material at runtime, you witness the change in the inspector, but the object's behavior does not appear to reflect this change. Correct? The only thing I'm aware of that would create this behavior was mentioned in my previous comment.

Changing phys materials at runtime isn't something I recall ever doing personally. The docs don't seem to mention any extra step being necessary, but there are plenty of hidden quirks in Unity.

Perhaps setting the collider's material to null, changing a saved reference to the material, then re-applying it to the collider would invoke some kind of refresh? Worth a try.

If your changes are only ever instantaneous changes between two states, you might try changing between two physics materials; one for each state.

avatar image klolololol · Apr 21, 2015 at 09:10 AM 0
Share

I've attached a material to my other colliders too, I don't change the friction from code, I change the material itself as you suggested. Still no luck, I don't know what to think.

2 Replies

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

Answer by klolololol · Apr 21, 2015 at 02:39 PM

I had to disable than enable the collider, that's the only solution I've found. It's a workaround, but this part of unity is buggy.

Here's the modified code:

     void checkOnGround() {
         Transform t;
         foreach(Collider2D c in collidingTiles) {
             t = c.transform;
             if(t.position.y - transform.position.y < -Misc.TILE_SIZE * 0.75f) {
                 onGround = true;
                 myCollider.sharedMaterial.friction = 20.0f;
                 myCollider.enabled = false;
                 myCollider.enabled = true;
                 return;
             }
         }
         onGround = false;
         myCollider.sharedMaterial.friction = 0.0f;
         myCollider.enabled = false;
         myCollider.enabled = true;
         return;
     }


Comment
Add comment · Show 2 · 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 brlan10 · Oct 19, 2016 at 03:45 AM 0
Share

Had the same problem and this works. However, I can't imagine it is good for performance.

avatar image Deeeds brlan10 · May 06, 2018 at 07:04 PM 0
Share

is this still the way to do this?

and why shared$$anonymous$$aterial and not physics$$anonymous$$aterial2D?

avatar image
0

Answer by alpha_ml · May 31 at 10:28 AM

This still has not been fixed, and the enabled/disabled workaround still works. Any update from a unity dev on why this is ?

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Colliders2D not working under same parent? 2 Answers

How to check place on empty for placement polygon Rigidbody2d? 0 Answers

Velocity doesnt change properly 1 Answer

Bullets go through collider even with continuous collision detection! 0 Answers

How does Unity want me to add Friction? 0 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