please write example code that changes a capsule colliders physics material from “playerFriction” to “playerSlide”. I’m seriously not getting this and all the other threads are from 2013 with all of the code being deprecated.
I’m like 95% sure nothing has changed with the APIs for Colliders and PhysicMaterials in at least 10 years.
There is some example code right on this page for how to create a physicmaterial and assign it to a collider:
Nice, that’s not what the old guides say. So I already have a physics material I want to apply, how would I “load” it in the script so I can apply it? Do I have to create the physics materials in the script? can it not pull the ones I created from my project file?
You can create a public/serialized PhysicMaterial field and reference it in your script like any other unity object such as Textures, Meshes, Components, etc…
public class Foo : MonoBehaviour
{
public PhysicMaterial someMaterial; //assigned in the editor
public void ChangeMaterial()
{
var collider = GetComponent<Collider>();
collider.sharedMaterial = someMaterial;
}
}
2 Likes