Time for action – warning the player

A single script attached to our objects in space will warn the player as the objects approach.

  1. We start by creating a new script and name it Alarm.
  2. This script starts with a single variable. It will hold the distance value at which the sound will start to fade in.
    public float warningDist = 100f;
  3. Next, we create the Update function. It starts by checking for an Audio Source component and exiting the function early if there isn't one. The audio variable holds the reference to the attached Audio Source component.
    public void Update() {
      if(audio == null) return;
  4. The function continues by calculating the distance to the player. Because the player never moves, we can just use the position's distance to the origin to make ...

Get Unity Android Game Development by Example Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.