Bounce and Friction
Part of the Physics and Gameplay section of Coddy's Unity journey. Lesson 12 of 71.
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
EasyGive the trampoline and the ice their own bounce and friction.
- In
Start(), takePhysicsMaterial mat = GetComponent<Collider>().material;and setmat.bounciness = bounce;,mat.dynamicFrictionandmat.staticFrictiontofriction. - Set
mat.bounceCombine = PhysicsMaterialCombine.Maximum;andmat.frictionCombine = PhysicsMaterialCombine.Minimum;, so the surface's own values win.
Press Play: the ball bounces high off the trampoline. Then Check.
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
}
}
All lessons in Physics and Gameplay
1Rigidbody
Gravity and MassPush with AddForceJump with an ImpulseFixedUpdate for PhysicsVelocityKinematic BodiesRecap: Launch Pad4Coroutines
InvokeInvokeRepeating, CancelInvokeYour First CoroutineLoops in CoroutinesStopping CoroutinesRecap: Countdown10Project: Arena Escape
The ArenaHunters2Collisions
OnCollisionEnterWhat Did I Hit?Break on ImpactStanding on the GroundBounce and FrictionCollider ShapesRecap: Breakable WallPractice on your own: Unity playground