Now that we have our project set up, let's get started with creating our player:
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:The art assets used for this tutorial were provided by Kenney. To see more of their work, please check out www.kenney.nl.
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.
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.
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: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.
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
.Now, when we play the game, you'll see that we now have a complete background that tiles properly.
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.