Menu
CoddyTech

Keep the Speed

Lesson 3 of 19 in Coddy's Unity Game: Brick Breaker course.

A Brick Breaker ball never slows down. Its PhysicsMaterial does most of the work: bounciness 1 keeps all of a bounce's speed, and friction 0 stops the walls from dragging it. The walls have no material, and Unity averages the two colliders' values by default: the ball's 1 and a wall's 0 make a bounce at half speed. bounceCombine = Maximum and frictionCombine = Minimum let the ball's own values win.

The physics is still not exact: a moving paddle adds its speed, and small errors add up over hundreds of bounces. So every physics step the ball keeps its direction and gets its speed set back:

rb.linearVelocity = rb.linearVelocity.normalized * 7f;

A ball slowed to 2 units a second flies at 7 again on the next step.

challenge icon

Challenge

Easy

Make the ball bounce forever at one speed.

  1. In Ball's Awake(), make new PhysicsMaterial("Bouncy") with bounciness 1, both frictions 0, bounceCombine = PhysicsMaterialCombine.Maximum and frictionCombine = PhysicsMaterialCombine.Minimum; set it on GetComponent<Collider>().material.
  2. Add FixedUpdate(): if launched, rb.linearVelocity = rb.linearVelocity.normalized * speed;

Press Play and launch: the ball bounces off every wall without slowing. Then Check.

Try it yourself

using UnityEngine;
using UnityEngine.InputSystem;

public class Ball : MonoBehaviour
{
    public Transform paddle;
    public float speed = 9f;

    Rigidbody rb;
    bool launched;

    void Awake()
    {
        rb = GetComponent<Rigidbody>();
    }

    void Update()
    {
        if (launched) return;
        transform.position = paddle.position + Vector3.up * 0.5f;
        if (Keyboard.current.spaceKey.wasPressedThisFrame) Launch(new Vector3(0.3f, 1f, 0f));
    }

    public void Launch(Vector3 direction)
    {
        launched = true;
        rb.linearVelocity = direction.normalized * speed;
    }
}

All lessons in Unity Game: Brick Breaker

Practice on your own: Unity playground