Having fun in space with trails

At the time of writing this, I’m nearing the end of my second journey in to book writing. (in a very short period of time) In response to a survey I put out about where to put content (a non-UI tutorial in a book about the new UI system), I’m publishing the appendix for my forthcoming Unity title named Unity 3D UI Essentials:

Unity 3D UI Essentials – published by packt Publishing

 

Unity 3D UI Essentials

As the book at the moment is still in preview (pre-order). Full details about the book can be found here on my blog. (some of the notes on the Packt site are still very odd, working with the publisher to fix those Confused smile)

So for a little extra chapter which talks about setting up the 3D Demo scene for Chapter 5, check it out here:

The appendix is all about creating a 3D demo scene for which to build our UI upon.  There are lots of tips and tricks here for 3D scene setup, background cameras with effects and using the infamous trail renderers for drawing a line behind 3D objects.  Enjoy.

 

Note this isn’t my usual sort of tutorial, as I usually explain things a lot more. This is more of a step by step tutorial (that I usually moan about) to setup the scene.


Appendix – The 3D scene sample

As part of Chapter 5 – Screenspace, Worldspace and the camera, I setup a simple 3D demo scene with a few effects to make the UI really stand out and shine. However as the focus of this book it was moved to an appendix for you to follow if you so wish.

Additionally, the appendix is also available online here – http://bit.ly/UnityUIEssentials3DDemoScene <- This post 😀

If you want to skip its creation or just use the completed version to follow through, then you can download the .UnityPackage asset for the demo scene here http://bit.ly/UIEssentialsCh5DemoScene and then just import it into your project as a Custom Package using Assets -> Import package -> Custom Package… in the Unity menu.

This chapter just works through a couple of 3D tips and tricks that you can use in any scene to help liven it up and work through some the niggles of working in 3D scenes.

Setting up for the big game

To make any picture stand out, it needs a really good background, so for Chapter 5 I created this simple 3D space scene for us to paint our UI on to:

3617OS_APP_01

It’s a cool mix of different Canvas UI elements displayed in a 3D scene, all with completely free Assets!

First off for our big example, let’s grab a few things:

  1. Start a new 3D project or just create a brand new scene for our playground.
  2. Add the standard Particle project asset that comes with Unity (either when setting up the project or through Assets -> Import Package -> Particles in the menu)
  3. Grab the Skybox Volume 2 Nebula asset package from the store (it’s free and fantastic for demos – http://bit.ly/NebulaSkybox)
  4. Grab the Free SciFi Fighter asset package from the store (it’s pretty – http://bit.ly/SciFiFighter)

Note, the SciFi fighter when imported doesn’t use its own folder (very annoying), so after you have imported it, create a new folder called SciFi Fighter and move its assets to this folder (Materials folder, Meshes Folder, Textures folder and SciFi_Fighter_AK5 scene), just to keep the project structure tidy.

It is truly amazing what free stuff is available for game creators these days!

The initial 3D scene

I almost had a little too much fun creating this basic scene and even learned some new tricks along the way.  It’s not crucial for the UI setup so you can skip it if you want to but it’ll only take 5 minutes or so to complete.

  1. Start with a new scene (if you haven’t done so already) and save it as Example_UI
  2. Drag the fbx mesh asset in to the scene from the Meshes folder (either from the project root or the SciFi Fighter folder you created)
  3. Change the Rotation of the SciFi Figher to X 357, Y 18, Z 317. This gives a nice outbound flight path for the fighter.
  4. Drag the Main Camera (in the hierarchy) as a child of the SciFi Fighter and reset its transform (using the cog icon in the top right hand corner of the inspector with the Main Camera selected, then select Reset). This ensures that the camera is always following the fighter and keeps it in view.
  5. Set up the Main Camera’s transform as follows:
    • Position : X 38, Y 48, Z -47
    • Rotation : X 31, Y 340, Z 30
  6. Remove the GUILayer, Flare layer and Audio Listener from the Main Camera by right clicking on each component and selecting Remove Component, these just get in the way here because it’s following the ship.
  7. Set the Clear Flags to Depth Only and the Depth to 0 of the Main Camera, since this is just a tracking camera in front of a background (which we’ll add in a bit)
  8. Add a Directional Light using Create -> Light -> Directional Light to the scene to light the way.

Here’s where you should be up to now. No UI yet, just the beginnings of a scene:

3617OS_APP_02

Next let’s smarten it up a bit with a background skybox and even a trail for the ship:

  1. Add a new Layer to the project (through the Layer Menu in the top–right corner of the inspector for every GameObject) called Background (this just helps identify background objects for the background camera). This should be set in User Layer 8 (but any position in the Layer array will do)

3617OS_APP_03

The Add Layer option in the inspector

  1. Add a new Camera to the scene (using Create -> Camera ) and rename it to BackgroundCamera
  2. Set the Depth for the BackgroundCamera to -1
  3. Set the Layer for the BackgroundCamera to the new Background layer using the Layer combo box shown earlier.
  4. Configure the Culling Mask of the BackgroundCamera to just the Background layer by selecting Nothing in the drop down selection and then selecting (For an added bonus, also remove the Background layer from the Culling Mask of the Main Camera so it does not render it)

3617OS_APP_04

  1. Add a Skybox component (Add Component -> Rendering -> Skybox) to the Background Camera and set the Custom Skybox property to one of the skyboxes from the SkyBox Volume 2 folder. You do this by dragging the DSG.mat (the material, not the skybox images) asset to the Custom Skybox property of the Skybox Component.  I personally went for the DeepSpaceGreen Skybox (in the folder of the same name under the SkyBox Volume 2 folder).
  2. Finally add a new Particle System as a child of the BackgroundCamera (Right-Click on the BackgroundCamera and select Particle System) with the following settings (any settings I don’t alter are the defaults):
    • Transform
      • Position : X = 83, Y = 28, Z = 76
      • Rotation : X = 10, Y = 240, Z = 1.8
    • Particle System
      • Duration = 20
      • Start Lifetime = 20
      • Start Speed = 10
    • Emission Rate = 5
    • Shape
      • Shape = Box
      • Box X = 50
      • Box Y = 50
      • Box Z = 5
    • Renderer Max Particle Size = 0.005

The Particle System should now look like the following screenshot:

3617OS_APP_05

All this does is to simply add some flavor to our scene so it’s not boring J

Note: I’ve found in situations where you want a background and a moving target that you want to follow, it is best to have the background and the target using separate cameras.

After all that our demo scene now looks like the following:

3617OS_APP_06

Lastly, let’s kick our ship in to action and make it at least look like it’s moving:

  1. Add a Rigidbody component (Add Component -> Physics -> Rigidbody) to the SciFi_Fighter_AK5 and uncheck the Use Gravity property (it’s space, there is no Gravity!)
  2. Add two new Empty GameObjects as children to the SciFi_Fighter_AK5 (right click the fighter and select Create Empty). Name them Engine1 and Engine2
  3. For Engine1, set its Transform Position to X -3.24, Y -0.04, Z -1.69 (Rotation values should all be 0 and Scale values should all be 1)
  4. For Engine2, set its Transform Position to X 3.49, Y -0.04, Z -1.69 (Rotation values should all be 0 and Scale values should all be 1)

If for some reason this does not place the engine GameObjects in the exhaust ports of the ship, just position them manually in relation to the SciFi Fighter.

  1. Select BOTH of the Engine’s in the project hierarchy, then in the inspector, click on Add Component and select Effects -> Trail Renderer
  2. Still with both Engines selected, expand the Materials section of the Trail Renderer in the inspector and set the Size to 2
  3. Drag the FlameE material from the Standard Assets\Particles\Sources\Materials folder on to Element 0 of the Trail Renderer Materials
  4. Drag the Smoke Trail material from the Standard Assets\Particles\Sources\Materials folder on to Element 1 of the Trail Renderer Materials
  5. Set the Time to 2, Start width to 5 and End Width to 0.5

The Trail render is a great way of adding simple trails to GameObjects, whether it’s a light cycle, a car or in this case a space ship.  They float along behind the object as it moves round the scene.

The Trail Render in the inspector should now look like the following screenshot:

3617OS_APP_07

Almost there, one last thing remains. If you run the scene now you won’t see any change, no trails, the ship just sits there and does nothing. Simply put it’s doing nothing because we haven’t told it to do anything, for the trail renderer to work the ship has to move.

For the last touch, create a new C# script called ShipMove and replace its contents with the following:

Now simply attach the script to the SciFi_Fighter_AK5 GameObject and hit play and your fighter will be screaming through the stars on its way to oblivion.

3617OS_APP_08

Our sample scene is complete.

Note the complete scene is also available as a Unity Package and can be downloaded from here: http://bit.ly/UIEssentialsCh5DemoScene

Now return to Chapter 5 and start adding some UI to this amazing scene you have created.


More to come

In the run up to the book’s publication, I’m also going to publish the preview chapter on my blog as well.  FYI It’s chapter 1 which walks through the legacy GUI system and gives a highlight overview of the new UI “Awesomesauce” system Open-mouthed smile

SO keep your ears peeled and half an eye on the books forum here.

%d bloggers like this: