2019-06-01 net

(Brent) #1

Unity


in the Camera Settings panel. Also make sure the
camera is set to the default 0,0,-10 position.
Orthographic cameras flatten out the images we
see, removing all perspective. This is ideal for 2D
games. We can still use 3D models to make things
look awesome but the camera will render everything
flat. You can test this using the 2D tab over the main
scene window and switching to Game view.
Before you start, create sub folders in your Assets
folder to hold images, scripts, prefabs and materials.
Now locate an image to use as a sky background,
such as a blue linear gradient with a few clouds.
Make sure the image can tile horizontally and size
this image to a 1920 x 1080 ratio.
You can also find or make a simple transparent
PNG to use for your character. I made a simple paper
airplane from three triangles in Photoshop.


STEP 3: SKY BACKGROUND OBJECT
Start by creating a sprite for your sky. To do this,
import a new asset by right-clicking inside your
images folder. In the drop-down list, select Import
New Asset. Select your sky image and import it.
Click on the image in the Project Explorer so you
can see its properties in the Inspector panel. Set
the Texture Type to Sprite (2D and UI) and press the
Apply button in the lower right of the window to
apply the change. This will turn your image into a
sprite. You’ll see the change take effect in the Project
Explorer view. After you have a sky sprite created,
drag it into the Scene window and position it at 0,0,0.
Adjust the scale of the sprite until it fills the
window in Game view. If you imported an image at
1920 x 1080 and your game setting is 16:9 ratio, you
won’t need to adjust the scale – it will be fine at 1.0.
Switch between Scene and Game views by clicking
on the tabs in the main window. Once you are happy
it fills the screen in Game view, zoom out in the
Scene view so you can see the image and some space
around it. Duplicate the sky sprite or drag another
copy from your Project Explorer view into the scene.
Name the new sprite ‘sky1’ and the first one ‘sky2’.
Position the second sky sprite so it lines up with the
other sky horizontally to the left. In my case, my
‘sk y1’ sprite was set at -19.0,0,0 for the position.
To make the sky scroll endlessly, swap their
positions at the moment they are about to scroll off
the screen as they animate horizontally to the left.
Create a new empty object in your scene in the
hierarchy window. You can do this by right-clicking
in that window and selecting Create Empty. Name
it ‘sky’ and drag and drop the two sky sprites onto
it. Now you have a container object to hold the two
sky sprites in case you need to adjust this scrolling
element later.


Left Add some sprites to
your project
Below Place two sky
sprites side by side for
endless horizontal scrolling

STEP 4: SKYANIMATOR SCRIPT
Select the new sky container object in the Hierarchy
window. In the Inspector window click Add
Component. In the option list, scroll to the bottom,
select New Script and enter ‘SkyAnimator’ as the
name of the new script. Press the Create and Add
button. This adds a new script component to the
object. To edit the new script, double-click it in the
script field in the new component.

STEP 5: PUBLIC VARIABLES
Visual Studio will open and you can edit your
script. It will include some default libraries at
the top and a declaration of the class SkyAnimator.
Immediately after that class declaration line, add the
following code to declare some public variables:

public GameObject sky1;
public GameObject sky2;
public float speed = 0.05f;

If you save this class now, you’ll see these three new
public variables added as options in the component
you created in your Unity project. The public keyword
means the variable will be added to the component
as a new field that you can edit or drag and drop
elements into. It’s also accessible by other scripts.
The GameObject keyword indicates the type of variable
we are creating. Game objects are a core class in
Free download pdf