Quantcast
Channel: Radiated Pixel » Featured
Viewing all articles
Browse latest Browse all 5

Creating a scrolling background and particle effects for a space flight in Unity

0
0

I want to share something that I built in Unity for a game we are currently developing. It is a loopable scrolling background that can be used for space flight or time warp scenes to give a sense of movement when the foreground is more or less static. First I give you an overview of the questions answered in this tutorial, so you can decide if there is something new and valuable for you ;)

  1. How can I animate a tileable texture?
  2. How can I scroll the texture not only in a straight motion but also make them oscillate like a pendulum?
  3. How do I create multiple particle effects for light fog effects with the new Shuriken particle system?
  4. How do I use multiple cameras with different depths to avoid the need of scaling every attribute of the particle system?

Animating textures with a short script

To animate a texture along a surface you need a mesh with clean texture coordinates. You can of course simply use Unity’s primitives, but for our game I quickly created a tunnel in Maya. I made sure that the UVs have as little distortion as possible.

Tunnel

I then created a tileable grid texture in Photoshop. You may notice in the image below, that the alpha channel is never fully white, because we wanted the grid to be fairly transparent. Also the horizontal lines are blurred to give the impression of some kind of motion blur.

alpha

When you are done with the mesh, texture, material and basic scene setup in Unity you can create the script for animating the texture. I named mine AnimateMaterial.cs. You can do the same and write the following code:

using UnityEngine;
using System.Collections;

public class AnimateMaterial : MonoBehaviour {

  public float scrollSpeedU = 0.0f;
  public float scrollSpeedV = 0.25f;

  public float amplitudeU = 0.0f;
  public float amplitudeV = 0.0f;

  void Update() {
    float offsetU = 0.0f;
    float offsetV = 0.0f;

    if(amplitudeU > 0.0f) {
      offsetU = amplitudeU * Mathf.Sin(scrollSpeedU * Time.time);
    } else {
      offsetU = Time.time * scrollSpeedU;
    }

    if(amplitudeV > 0.0f) {
      offsetV = amplitudeV * Mathf.Sin(scrollSpeedV * Time.time);
    } else {
      offsetV = Time.time * scrollSpeedV;
    }

    renderer.material.SetTextureOffset("_MainTex", new Vector2(offsetU, offsetV));
  }
}

The two amplitudes are set to zero by default. That way the texture gets animated along the U or V axis with the specified speed. If you set the amplitude as well, the animation will switch to the oscillating motion of a pendulum. These are the settings that I have used for the video at the top of the page:

settings

Light fog effects with Shuriken

The particle system is huge, compared to the tunnel object. I have created the blueish system in the middle first and then copied it twice and adjusted the color and position. Creating subsystems is a feature introduced in Unity 4 with the new Shuriken system. You can just select your particle system in the project hierarchy and duplicate it as you would with any other object. All the settings are in the screenshot below, if you have further questions you can of course ask me in the comments or write me a mail.

Shuriken

Camera setup for independent background

Scaling particles is very difficult, as the size and position of a single particle depend on many factors. To ensure that the background scene will look the same when I hand it over to our main developer I created an additional camera.

Cameras

It is very important that you change the tag from MainCamera to Untagged, so that Unity knows which camera to use when you start the game. Also the main camera has to be set to Depth only or it won’t have a “transparent background”, so to speak. If you use more than two cameras (yes, you can), all but the last camera has to be set to Depth only. The order of the cameras can be set by adjusting the Depth value in the inspector.

Camera Settings

I hope this helps some folks in creating their own space shooter, time warp, black hole, jump gate travel, drug abuse effects like in Enter The Void or something completely different!

Kind regards,
Manuel

The post Creating a scrolling background and particle effects for a space flight in Unity appeared first on Radiated Pixel.


Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images