Time for action – creating the parallax background

There is an alternative method of creating and utilizing a second camera, but ours will make use of a single script that additionally allows us to control the speed of each layer:

  1. We will start this section with the creation of the ParallaxScroll script.
  2. This script starts with three variables. The first two keep track of each material and how fast they should scroll. The third keeps track of the camera's last position, so we can track how far it moves in each frame.
    public Material[] materials = new Material[0];
    public float[] speeds = new float[0];
    
    private Vector3 lastPosition = Vector3.zero;
  3. In the Start function, we record the camera's beginning position. We use Start instead of Awake here, in ...

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.