Menu
CoddyTech

Bounce and Friction

Part of the Physics and Gameplay section of Coddy's Unity journey. Lesson 12 of 63.

How a collider bounces and slides comes from its PhysicsMaterial (Unity 6's name; older versions called it PhysicMaterial). bounciness runs from 0 (no bounce) to 1 (keeps all its speed). dynamicFriction slows a body that is sliding, and staticFriction decides how hard a resting body must be pushed before it starts to slide. A collider without a material acts as friction 0.6 and bounciness 0. This super ball gives its own collider a bouncy material:

void Start()
{
    PhysicsMaterial mat = GetComponent<Collider>().material;   // this collider's own copy
    mat.bounciness = 0.9f;
    mat.bounceCombine = PhysicsMaterialCombine.Maximum;
}

A contact has two colliders, so Unity combines their two values. Each material picks how, with bounceCombine for bounciness and frictionCombine for friction, and each takes one of four modes: Average (the default), Minimum, Multiply or Maximum. When the two colliders ask for different modes, the one later in that list wins. On a floor with no material, Average would give the ball (0.9 + 0) / 2 = 0.45. With Maximum it keeps 0.9 of its speed on every bounce, which is 0.9 x 0.9 = 81% of its height, since height goes with the square of speed.

.material gives this collider its own copy of its material, so the changes affect it alone. .sharedMaterial is the material asset itself, shared by every collider that uses it, and it is null on a collider that was never given one.

challenge icon

Challenge

Easy

Give the playground its surfaces. The Surface script is on the blue Trampoline (bounce 0.8, friction 0.6) and on the Ice strip (bounce 0, friction 0.1). The ball and the crate have no materials of their own.

  • In Start(), set this collider's own material to bounce and friction (dynamic and static alike).
  • Whatever lands on a surface must bounce with the surface's full bounce, and whatever slides on it must get the surface's own friction, not a mix with the 0.6 of things without a material.

Press Play to watch the ball bounce, then Check: it also slides the crate across the ice.

Try it yourself

using UnityEngine;

public class Surface : MonoBehaviour
{
    public float bounce = 0f;
    public float friction = 0.6f;

    void Start()
    {
        // Give this collider its own material with bounce and friction
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Physics and Gameplay