- Home /
How to change material friction from code for a circle collider 2d?
I have a sprite, to which I attached a Circle Collider 2D.
And then during runtime, i need to access the Material in this collider and change its friction value.
This works:
transform.Find("Body/Leg").collider2D.sharedMaterial.friction = 5f;
But the problem is, that this changes the friction setting globally for all objects which use this same material.
If it wasn't a 2D collider, but a normal 3D collider i could simply do:
transform.Find("Body/Leg").collider.material.dynamicFriction = 5;
But the collider2D doesn't have any material property, (like for example collider2D.material), it only has collider2D.sharedMaterial, and changing that changes the value globally and i need to only change the friction value for that one object instance i'm working with.
I also tried this, but it does nothing:
transform.Find("Body/Leg").renderer.material.SetFloat("Friction", 5f);
Answer by SilentSin · Sep 26, 2014 at 05:32 AM
Yet another aspect of 3D functionality that is missing from 2D.
Just assign a copy of the material on awake.
PhysicsMaterial2D mat = new PhysicsMaterial2D(collider2D.sharedMaterial.name + " (Instance)");
mat.friction = collider2D.sharedMaterial.friction;
// etc.
collider2D.sharedMaterial = mat;
Not a solution i was hoping for, but a functional workaround is better than nothing :) Hopefully unity will fix it in the future.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Change Material of quad in script c# unity 2d 1 Answer
Multiple Cars not working 1 Answer
Moving on x / z axis prevents me from falling 2 Answers
Unity 2018 2D Character Jump Glitch 1 Answer