Game Development Reference
In-Depth Information
Parallax scrolling
OK. Breathe. We're about to explain parallax scrolling , which is an effect where
objects further in the distance move slower than objects closer to the camera.
From what I understand, many of you may now need to take a moment to stop
hyperventilating. Go for it, I'll wait.
As a developer, we have been asked numerous times about how to implement
parallax scrolling in a 2D game. Cerulean Games, my game studio, has even had
the elements of parallax scrolling as the "do or die" requirement to close a project
deal with a client. In reality, this is incredibly easy to accomplish, and there are
a number of ways to do this.
In Power Rangers Samurai SMASH! (developed by Cerulean Games for Curious
Brain; you can find it in the iOS App Store), we implemented a simple check that
would see what the linear velocity of the player is and then move the background
objects in the opposite direction. The sprites were layered on the Z plane, and each
was given a speed multiplier based on its distance from the camera. So, as the player
moved to the right, all parallax scrolling objects would move to the left based on
their multiplier value. That technique worked, and it fared us and our client quite
well for the production of the game. This is also a common and quick way to manage
parallax scrolling, and it's also pretty much how we're going to manage it in this
game as well.
OK, enough talk! Have at you! Well, have at the code:
1.
Create a new script called ParallaxController and make it look like
the following code:
using UnityEngine;
using System.Collections;
public class ParallaxController : MonoBehaviour
{
public GameObject[] clouds;
public GameObject[] nearHills;
public GameObject[] farHills;
public float cloudLayerSpeedModifier;
public float nearHillLayerSpeedModifier;
public float farHillLayerSpeedModifier;
public Camera myCamera;
 
Search WWH ::




Custom Search