Parallax, in a nutshell, means that objects closer to the viewer move relatively faster than objects farther away from the viewer, as the viewer moves. In a 2D side-scroller game, it has the effect of adding the richness of depth, without being truly 3D.
For this experiment, we have chosen to tryout the parallax features of Pro Camera 2D. After some workarounds described herein, we get the following result satisfactory result:
In a 2D system, using orthographic cameras, the simulation of depth, and therefore the effect of parallax, can be achieved by moving each object at a different rate relative to the object's distance away from the viewer (again, nearer: faster and farther: slower).
Since this can be a combersome ordeal to create, depending on the complexity of the scene, i.e. the number of parallax depth layers to manage, it can be worthwhile to save that time and use a pre-existing solution. There are several camera management systems available in the Unity Asset Store, and we chose to try Pro Camera 2D.
Initial setup of Pro Camera 2D is straightforward (see the References below for links to the tutorials and documentation). The camera adding feature (see '+',[add new image]) of the Parallax script enabled the quick creation the needed 13 parallax layers cameras. Associated with each camera in the list (see image) is the Speed setting for that layer, as well as a "Culling Mask" (Unity Layer, more on this in a moment) to specify which objects exist at that parallax layer's depth. That is all the information and configuration required for Pro Camera 2D to manage parallax rendering; i.e. Speed for the rate to move the individual parallax layer camera, and the mask with which to specify which objects that camera will render.
Unfortunately while assigning each layer's Culling Mask to a layer, we quickly run out of available layers.
In all versions of Unity (past, present, and foreseeable future) the number of layers in Unity are internally limited to 32 (for optimization reasons). Additionally, Unity cordons off 7 of them as 'system layers' for specific purposes. The remaining number for general use is 25.
However, camera parallax depth separation is not the only concern represented by Layers. Unity Layers are used for many purposes, most especially for specifying which bodies interact in the Physics system, but they can be utilized by the user (and other Assets!), for anything. Every other use requiring a layer decreases the available number for use as a parallax layer.
In the case of this experiment, there are 14+ parallax layers, in other scenes there may be less, or far more. When configuring the Pro Camera 2D . But it becomes unfeasible to create the number of needed parallax layers to complete the test. There are simply not enough layers to represent the 14 (and more) parallax layers needed for DinoRun2.
In the final configuration for this test, the Pro Camera 2D parallax implementation is still used, but not in the way it was intended. The configuration that achieved the desired effect is as follows:
The final scene rendered by the Pro Camera 2D looks and behaves as expected.
Going forward, even though this worked well as a first approximation, and a visual test, and we learned some things about Pro Camera 2D, as well as (arguably more importantly) Unity Layers and limitations. And, although the described solution configuration gave the results that were desired, it is unlikely that this method will be used for DinoRun2, for other, more compelling reasons.
Those compelling reasons, and an improved method that is already in mind, will be discussed in a future DevStream. We really hope that you have enjoyed reading about this exercise and one of the aspects for implementing 2D games in Unity. Stay tuned for more.
– The Dino Run 2 Development Team