Creating our scene

Now that we have our project set up, let's get started with creating our player:

  1. From the Project tab, double-click on the Sprites folder. Once inside, right-click within the Sprites folder and select Import New Asset to select the playerShip.png file from the Chapter 1/Art Assets folder from our example code to bring it in. Once added, confirm that the image's texture type is Sprite by clicking on it and from the Inspector tab, confirm that the Texture Type property is Sprite (2D and UI). If it isn't, simply change it to that, and then click on the Apply button. Have a look at the following screenshot:

    Note

    If you do not want to drag and drop the files, you can also right-click within the Sprites folder in Project Browser (bottom-left corner) and select Import New Asset to select a file from the Chapter 1/Art Assets folder to bring it in.

    Creating our scene

    Note

    The art assets used for this tutorial were provided by Kenney. To see more of their work, please check out www.kenney.nl.

  2. Next, drag and drop the ship into the scene (the center part that's currently dark gray). Once completed, set the position of the sprite to the center of the screen (0, 0) by right-clicking on the Transform component and then selecting Reset Position. Have a look at the following screenshot:
    Creating our scene

    Now, with the player in the world, let's add in a background. However, instead of creating a huge image or copying and pasting a similar image over and over, we will learn how we can use a material with a repeating texture.

  3. From the Project tab, bring the background sprite into the project and then select it and change the Texture Type in the Inspector tab to Texture, and click on Apply.
  4. Now, let's create a 3D cube by selecting Game Object | Create Other | Cube from the top toolbar. Change the object's name from Cube to Background. In the Transform component, change Position to (0, 0, 1) and the scale to (100, 100, 1).

    Since our camera is at 0, 0, -10 and the player is at 0, 0, 0, putting the object at position 0, 0, 1 will put it behind all of our sprites. By creating a 3D object and scaling it, we are making it really large, much larger than the player's monitor. If we scaled a sprite, it would be one really large image with pixilation, which would look really bad. By using a 3D object, the texture that is applied to the faces of the 3D object is repeated, and since the image is tileable, it looks like one big continuous image.

  5. As we won't be using it (and we're a 2D game), remove the BoxCollider component by right clicking on it from the Inspector tab and then selecting Remove Component.
  6. Next, we will need to create a material for our background to use. To do so, under the Project tab, select Create | Material, and name the material BackgroundMaterial. We can create a new folder called Materials to store this, but since this project will only use one it is OK to stay in Sprites. Under the Shader property, click on the drop-down menu and select Unlit | Texture. Click on the Texture box on the right-hand side and select the background texture. Once completed, set the Tiling property's x and y to 25. Have a look at the following screenshot:

    Note

    In addition to just selecting from the menu, you can also drag and drop the background texture directly onto the Texture box and it will set the property.

    Tiling tells Unity how many times the image should repeat in the x and y positions, respectively.

  7. Finally, go back to the Background object in Hierarchy. Under the Mesh Renderer component, open up Materials by left-clicking on the arrow, and change Element 0 to our BackgroundMaterial material. Consider the following screenshot:
    Creating our scene

    You can also add a material to an object by dragging and dropping the material onto the object in the Scene tab.

    Well, at this point we have a color, but we don't have the stars from our image repeating.. This is due to how the Wrap Mode property is set. In Unity 5.2, the default mode is Clamp, which means that the edges of the image will be extended out rather than repeat. Let's fix that now.

  8. From the Project tab, select the background texture object. Once selected, go to the Inspector tab, change the Wrap Mode to Repeat, and then click on Apply. Finally, select the Cube object from Hierarchy and then in the Inspector tab at the top, rename the object to Background.
    Creating our scene

Now, when we play the game, you'll see that we now have a complete background that tiles properly.

Scripting 101

In Unity, the behavior of game objects is controlled by the different components that are attached to them in a form of association called composition. These components are things that we can add and remove at any time to create much more complex objects. If you want to do anything that isn't already provided by Unity, you'll have to write it on your own through a process we call scripting. Scripting is an essential element in all but the simplest of video games.

Unity allows you to code in either C#, Boo, or UnityScript, a language designed specifically for use with Unity and modeled after JavaScript. For this book, we will use C#.

C# is an object-oriented programming language – an industry-standard language similar to Java or C++. The majority of plugins from Asset Store are written in C#, and code written in C# can port to other platforms, such as mobile, with very minimal code changes. C# is also a strongly typed language, which means that if there is any issue with the code, it will be identified within Unity and will stop you from running the game until it's fixed. This may seem like a hindrance, but when working with code, I very much prefer to write correct code and solve problems before they escalate to something much worse.

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

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