Reducing friction while button is pressed?

I’ve been looking around for this for a few days, and i can’t seem to get anything to work. I have added a physics material to my object, along with a RigidBody and a PolygonCollider2D. How can i change the value of friction on the physics material, but only when the button is pressed? Or switch to a separate physicsmaterial2d?

I’m doing this in C#, and i’ve tried “collider.dynamicFriction = 0;” “Collider.staticFriction = 0;” "collider.material.dynamicFriction = 0;"as well as some other stuff i can’t seem to remember at the moment. Most of it returns “UnityEngine.collider does not contain a definition for dynamicFriction” or i get “There is no collider attached to the object” while “PolygonCollider2D.material.etc” returns a no definition error.

Sorry if this seems rambled, but it’s getting late and i’m getting frustrated.

Please help!

The easiest and safest way is to change or swap the collider’s PhysicsMaterials2D. Don’t modify the properties of the PhysicsMaterial2D directly as that will most likely lead to other, completely different issues.

So basically you want something like this:

public class FooObject : MonoBehaviour
{
    public  PhysicsMaterial2D   m_slipperyMaterial; // e.g. with friction = 0
    public  PhysicsMaterial2D   m_stickyMaterial; // e.g. with friction >=1 
    private Collider2D          m_collider;

    void Awake()
    {
        m_collider = GetComponent< Collider2D >();
    }

    void Update()
    {
        PhysicsMaterial2D physicsMaterial = Input.GetKey( KeyCode.Space ) ? m_slipperyMaterial : m_stickyMaterial;
        ApplyPhysicsMaterial( physicsMaterial );
    }

    void ApplyPhysicsMaterial( PhysicsMaterial2D physicsMaterial )
    {
        m_collider.sharedMaterial = material;
        // Workaround due to a bug in Unity:
        m_collider.enabled = false;
        m_collider.enabled = true;
    }
}

This is also discussed here, here, and here.

Thanks a ton, my friend.