- Home /
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;
}
The physics material of the other object matters too. Also note the friction "combine" settings of both must reflect your desired outcome.
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.
"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.
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.
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;
}
Had the same problem and this works. However, I can't imagine it is good for performance.
is this still the way to do this?
and why shared$$anonymous$$aterial and not physics$$anonymous$$aterial2D?
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 ?