UNITY Lesson 1: Table of Contents
1a: Get Unity; Get Started
1b: Add a Character
1c: An Introduction to Scripting in C#
1d: Uniform Motion in Any Direction
1e: Move To a User-Clicked Location
1f: Background and Falling Rocks

UNITY Lesson 1f:
Background and Falling Rocks

BACKGROUND
Earlier in this lesson, you got a background for your game.
Now, we'll put it to use!
  • Drag In Background
    Locate your background in Project-Assets.
    Drag it into your scene in Hierarchy.
    Give it an appropriate name. (Mine is named Desert.)
    See (1) at right.
  • Adjust $z$-values, As Needed
    Make sure that your Inspector-Transform-Position-z values satisfy:   $\displaystyle z_{_{\text{Main Camera}}} < z_{_{\text{Wolf}}} < z_{_{\text{Desert}}}$
    For example, I set:
    • $z_{_{\text{Main Camera}}} = -10$
    • $z_{_{\text{Wolf}}} = 0$
    • $z_{_{\text{Desert}}} = 1$
    Desert must be further from Main Camera than Wolf.
    This way, Wolf appears ‘on top of’ Desert, instead of hidden beneath it!
    (Switch the $z$-values for Wolf and Desert, and see what happens.)
  • Resize Background, As Needed
    As needed, re-size your background to fill up the camera area.
    It can go outside the camera area, as long as you're happy with what you see in Camera Preview.
    See (2) at right.

(1) drag your background into Hierarchy



(2) Your scene should look something like this.
ROCK IMAGE, WITH A TRANSPARENT BACKGROUND
I want rocks randomly falling from the sky and hitting the desert floor.
When Wolf encounters a rock (by—say—running to it), the rock ‘reveals’ itself to be a (randomly-selected) expression (like ‘$\,x\,$’) or sentence (like ‘$\,x = 1\,$’).
The user then decides if it's an expression or a sentence.

So, we need a realistic rock image.

Here's what I do:
  • Check the Asset Store
    The Asset Store may have just what's needed.
    Unfortunately, I didn't find anything free that I like.
  • Find An Image Online
    Next, google ‘rock’ (and similar phrases) to find an image online.
    There are downloadable royalty-free images.
    Or, use a ‘snipping tool’ to ‘cut out’ an image. (Be careful about copyright issues.)
    A rock with a uniform background makes it easier to add transparency (next step).
    The rock I found is shown in (1) at right.
  • Make The Background Transparent
    If you have a favorite graphics editor, use it.
    Or, you can use Visual Studio:
    • Open the rock image in Visual Studio:   File-Open-File...
    • Select the "Click to select an area of similar color" tool.
      See (2a) below.
    • Click on a pixel with the desired color that you want transparent.
      Press ‘delete’ to delete that color.
      (The default is that ‘no color’ is transparent.)
      See (2b) below.
    • Save the file.
      Drag the file from (say) File Explorer into your Unity assets.
      (I chose to make an Assets-Images folder, and put the rock there.)
      See (2c) below.

(1) my rock image

(2a) the ‘Click to select an area of similar color’ tool

(2b) the checkered texture indicates transparency

(2c) drag the file into Unity
PREFABS
There will be many rocks falling from the sky in this game, all using the same image and having (mostly) the same behavior.
It's a perfect opportunity to use a Unity prefab.
What is a Prefab?
Here is information about prefabs from the Unity User Manual:
How To Create a Prefab
Information on Creating Prefabs from the Unity Manual

Creating a Prefab from a GameObject is a one-step process!
Just drag the desired GameObject from the Hierarchy window into the Project window.
That's it!
Make Rock a Prefab
We'll make our ‘falling rock’ a Prefab, as follows:
  • Create a Prefabs Folder
    This step is optional, but encouraged.
    Create a new folder, and name it Prefabs:   Project-Create-Folder
    See (1) at right.
  • Make Rock a GameObject
    You can only make Prefabs from GameObjects.
    At the moment, our rock is just an image.

    Drag the rock image from Assets-Images into Hierarchy.
    Name the new GameObject Falling Rock.
    See (2a) and (2b) at right.
  • Make Rock a Prefab
    Drag the new Falling Rock GameObject into the Assets-Prefabs folder.
    See (3a) below.

    The process of creating a new Prefab Asset turns the original GameObject into a Prefab instance.
    Prefab instances are shown in Hierarchy in blue text.
    The root GameObject of a Prefab is shown with the blue cube Prefab icon.
    See (3b) below.

    After a few moments, the Inspector for Falling Rock changes to reflect that it's now a Prefab.
    See (3c) below.

(3a) our newly-created Prefab

(3b) Prefabs are identified
by the color blue
in Hierarchy

(3c) Inspector changes to reflect that
Falling Rock is now a Prefab.
We don't want any rocks in our scene at the start of game play.
All our rocks will fall from the sky (via a script) once game play begins.
Therefore, you can now safely delete the (blue) Falling Rock that appears in Hierarchy.
This does not get rid of your Prefab! It only deletes a single instance of the Prefab.
With this instance gone from Hierarchy, you'll then need to click on the Falling Rock Prefab in Project-Assets-Prefab, if you want to make any edits.

Instead of deleting Falling Rock that appears in Hierarchy, you could merely uncheck it in Inspector, so it doesn't appear. It is possible to edit the actual Prefab via this particular instance, so it might be convenient to keep it hanging around.
Your choice!

(1) For good organization, create a Prefabs folder.
Note: You can change the size of folder icons using the slider at lower right.



(2a) new Falling Rock GameObject

(2b) Falling Rock in Inspector;
the rock image is automatically supplied
in Sprite Renderer-Sprite