Physics Material in Unity: What It Is and How to Use It
PhysicMaterial is an asset that defines how a collider interacts physically with other objects, controlling properties like friction and bounciness. It helps simulate realistic surface behaviors such as sliding or bouncing when objects collide.How It Works
A PhysicMaterial in Unity acts like a surface coating that changes how objects slide or bounce when they touch each other. Imagine putting rubber soles on shoes to prevent slipping or adding a bouncy ball's surface to make it rebound. This material controls two main things: friction, which slows down sliding, and bounciness, which makes objects bounce back after hitting something.
When two objects collide, Unity looks at their physic materials to decide how much they should stick, slide, or bounce. If both have high friction, they grip tightly and don’t slide much. If they have high bounciness, they rebound like a ball hitting the ground. This lets you create realistic or fun physical interactions in your game world.
Example
using UnityEngine; public class BouncyBall : MonoBehaviour { void Start() { // Create a new physic material PhysicMaterial bouncyMaterial = new PhysicMaterial("Bouncy"); bouncyMaterial.bounciness = 1f; // Maximum bounce bouncyMaterial.dynamicFriction = 0f; // No friction when moving bouncyMaterial.staticFriction = 0f; // No friction when still bouncyMaterial.frictionCombine = PhysicMaterialCombine.Minimum; bouncyMaterial.bounceCombine = PhysicMaterialCombine.Maximum; // Assign it to the collider Collider col = GetComponent<Collider>(); if (col != null) { col.material = bouncyMaterial; } } }
When to Use
Use physic materials when you want to control how objects physically interact in your game. For example, add a slippery material to ice surfaces so characters slide, or a sticky material to mud so they slow down. Use bouncy materials for balls, trampolines, or any object that should rebound after hitting something.
This helps make your game feel more natural or adds fun effects by changing how objects move and react on contact.
Key Points
- Physic materials control friction and bounciness of colliders.
- They affect how objects slide or bounce on contact.
- You can create and assign them in the Unity Editor or by code.
- Combining materials on two colliding objects determines the final physical behavior.