Backgrounds and active elements

A slightly more advanced option when building game worlds is to add a level of immersive depth to the scene. Having a static image to show the village looks good, especially when you start adding houses and NPCs to the mix; but to really make it shine, you should layer the background and add additional active elements to liven it up.

We won't add them to the sample project at this time, but it is worth experimenting with in your own projects (or try adding it to this one)—it is a worthwhile effect to look into.

Parallaxing

If we look at the 2D sample provided by Unity, the background is split into several panes—each layered on top of one another and each moving at a different speed when the player moves around. There are also other elements such as clouds, birds, buses, and taxes driving/flying around, as shown in the following screenshot:

Parallaxing

Implementing these effects is very easy technically. You just need to have the art assets available. There are several scripts in the wiki I described earlier, but the one in Unity's own 2D sample is the best I've seen.

Note

To see the script, just download the Unity Projects: 2D Platformer asset from https://www.assetstore.unity3d.com/en/#!/content/11228, and check out the BackgroundParallax script in AssetsScripts.

The BackgroundParallax script in the platformer sample implements the following:

  • An array of background images, which is layered correctly in the scene (which is why the script does not just discover the background sprites)
  • A scaling factor to control how much the background moves in relation to the camera target, for example, the camera
  • A reducing factor to offset how much each layer moves so that they all don't move as one (or else what is the point, might as well be a single image)
  • A smoothing factor so that each background moves smoothly with the target and doesn't jump around

Implementing this same model in your game would be fairly simple provided you have texture assets that could support it. Just replicate the structure used in the platformer 2D sample and add the script. Remember to update the FollowCamera script to be able to update the base background, however, to ensure that it can still discover the size of the main area.

Foreground objects

The other thing you can do to liven up your game is to add random foreground objects that float across your scene independently. These don't collide with anything and aren't anything to do with the game itself. They are just eye candy to make your game look awesome.

The process to add these is also fairly simple, but it requires some more advanced Unity features such as coroutines, which we are going to cover in another chapter. So, we will come back to these later.

In short, if you examine the BackgroundPropSpawner.cs script from the preceding Unity platformer 2D sample, you will have to perform the following steps:

  1. Create/instantiate an object to spawn.
  2. Set a random position and direction for the object to travel.
  3. Update the object over its lifetime.
  4. Once it's out of the scene, destroy or hide it.
  5. Wait for a time, and then start again.

This allows them to run on their own without impacting the gameplay itself and just adds that extra bit of depth. In some cases, I've seen particle effects are also used to add effect, but they are used sparingly.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.78.237