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:
- We will start this section with the creation of the
ParallaxScroll
script. - 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;
- In the
Start
function, we record the camera's beginning position. We useStart
instead ofAwake
here, in case the camera needs to do any special movement at the beginning of the game.public void Start() { lastPosition = Camera.main.transform.position; }
- Next, we use the
LateUpdate
function to make changes after the camera has moved...