0
0
Unityframework~5 mins

Physics materials (friction, bounce) in Unity

Choose your learning style9 modes available
Introduction

Physics materials help objects in a game behave realistically when they touch or collide. They control how slippery or bouncy an object is.

When you want a ball to bounce on the ground like a real ball.
When you want a character to slide on ice instead of walking normally.
When you want objects to stop moving quickly because of rough surfaces.
When you want to make a trampoline that pushes objects up.
When you want to control how two objects slide against each other.
Syntax
Unity
PhysicMaterial material = new PhysicMaterial();
material.dynamicFriction = 0.5f;
material.staticFriction = 0.5f;
material.bounciness = 0.8f;
material.frictionCombine = PhysicMaterialCombine.Multiply;
material.bounceCombine = PhysicMaterialCombine.Average;

Collider collider = gameObject.GetComponent<Collider>();
collider.material = material;

dynamicFriction controls friction when objects are moving.

staticFriction controls friction when objects are still.

Examples
This makes an ice surface that is very slippery and not bouncy.
Unity
PhysicMaterial ice = new PhysicMaterial();
ice.dynamicFriction = 0.1f;
ice.staticFriction = 0.1f;
ice.bounciness = 0f;

Collider iceCollider = iceObject.GetComponent<Collider>();
iceCollider.material = ice;
This makes a rubber ball that bounces a lot and has some friction.
Unity
PhysicMaterial rubber = new PhysicMaterial();
rubber.dynamicFriction = 0.4f;
rubber.staticFriction = 0.4f;
rubber.bounciness = 0.9f;

Collider ballCollider = ball.GetComponent<Collider>();
ballCollider.material = rubber;
Sample Program

This script creates a bouncy physics material and applies it to the object's collider. It also prints a message to confirm the bounciness value.

Unity
using UnityEngine;

public class MaterialExample : MonoBehaviour
{
    void Start()
    {
        PhysicMaterial bouncyMaterial = new PhysicMaterial();
        bouncyMaterial.dynamicFriction = 0.2f;
        bouncyMaterial.staticFriction = 0.2f;
        bouncyMaterial.bounciness = 0.8f;
        bouncyMaterial.frictionCombine = PhysicMaterialCombine.Average;
        bouncyMaterial.bounceCombine = PhysicMaterialCombine.Maximum;

        Collider col = GetComponent<Collider>();
        if (col != null)
        {
            col.material = bouncyMaterial;
            Debug.Log("Bouncy material applied with bounciness: " + bouncyMaterial.bounciness);
        }
        else
        {
            Debug.Log("No collider found on this object.");
        }
    }
}
OutputSuccess
Important Notes

Always make sure your object has a Collider component before assigning a physics material.

Friction and bounce combine modes affect how materials interact when touching.

Changing physics materials can help make your game feel more natural and fun.

Summary

Physics materials control friction and bounce on objects.

Use them to make surfaces slippery, sticky, or bouncy.

Assign physics materials to colliders to change how objects behave on contact.