Performance: Physics materials (friction, bounce)
This affects the physics simulation performance and the smoothness of object interactions in the game.
var sharedMat = Resources.Load<PhysicMaterial>("SharedMaterial");
collider.material = sharedMat; // reuse one shared materialvar mat = new PhysicMaterial(); mat.dynamicFriction = 1.0f; mat.staticFriction = 1.0f; mat.bounciness = 1.0f; collider.material = mat; // creating new material per object
| Pattern | Physics Calculations | Memory Usage | CPU Load | Verdict |
|---|---|---|---|---|
| Unique material per object | High (many calculations) | High (many instances) | High (complex friction/bounce) | [X] Bad |
| Shared physics material | Low (shared calculations) | Low (single instance) | Low (simple reuse) | [OK] Good |
PhysicsMaterial2D bouncyMaterial = new PhysicsMaterial2D(); bouncyMaterial.bounciness = 1.0f; Collider2D col = gameObject.GetComponent<Collider2D>(); col.sharedMaterial = bouncyMaterial; Rigidbody2D rb = gameObject.GetComponent<Rigidbody2D>(); rb.velocity = new Vector2(0, -10);What will happen when this object hits the ground?
PhysicsMaterial2D slippery = new PhysicsMaterial2D(); slippery.friction = 0f; Collider2D col = gameObject.GetComponent<Collider2D>(); col.material = slippery;But the object still slides slowly. What is the likely mistake?
sharedMaterial property to assign PhysicsMaterial2D, not material which does not exist on Collider2D.Pass the test and this pattern moves unknown