Gigi Labs

Monday, May 27, 2013

Unity3D: Space Invaders (Part 1 - Navigation and Materials)

Hello, fellow game developers! :)

In today's article we're going to start working on a clone of the arcade classic, Space Invaders. To prevent Tomohiro Nishikado from suing my ass off, we'll instead call this Ranch Invaders. Through this game we'll learn a number of important concepts including prefabs, linear interpolation, materials, and more. Since we can't learn all this in a single article, this will be spread out over several articles.

If you've been following my previous Unity3D articles, the last project you used will automatically open when you launch Unity. Create a new project from File menu -> New Project... and find a place to put it:

You don't need to import any packages. Once Unity relaunches with your new project, press Ctrl+S to save your .unity (scene file) somewhere.

Great. Now, we'll begin working on the alien, and we'll use it to learn some basics about working with Unity. For the time being, we'll use a simple cube as the alien, so go to GameObject menu -> Create Other -> Cube, just the same as we did in the previous two articles. You should now have a cube sitting in your game world (left portion of the screen):

You can see from the Inspector that the cube is at (0, 0, 0). You will also notice three arrows (red, green and blue) shown on the cube in the game world. You can click and drag one of the arrows to move the cube, and you will see the Position values in the Inspector change. You can also drag one of the squares between the arrows in order to move the cube in two directions (e.g. X and Y) at once.

In the Hierarchy section, there's also an object called Main Camera apart from the Cube you placed. If you select it, you'll see a little preview window:

If you press the Play button at the top of the Unity interface, you'll see the same thing. The main camera represents what the player is seeing.

Select the Cube again. Using the mouse, you can navigate the scene in order to work with the objects in it. If you press Alt and click and drag the left mouse button, you can rotate the view around the currently selected object (the Cube in our case). A middle-click (click the mouse's scroll wheel) and drag pans (moves sideways) the view relative to the selected object. By scrolling the mouse wheel, you can zoom the view towards or away from the camera. Finally, you can use the gizmo at the top-right of the world view to view the scene from particular directions (e.g. from the top).

You can manipulate the object itself by moving, rotating, or scaling it. We have already seen how to move an object (by using the arrows). You can use the W, E and R keys (or the buttons at the top-left of the Unity interface, just below the File menu) to switch between movement, rotation and scaling mode. When you do this, the arrows will change:

The screenshot above shows how the arrows change when you are scaling. You can drag one of the coloured boxes to scale in one direction, or the small box at the centre in order to scale uniformly (in all directions). Rotation has yet another thingy you can drag in order to rotate in different directions.

Once you're comfortable with navigating the scene using these controls, it's time to customise our Alien (Cube) a little bit.

With the Cube selected in the Hierarchy panel, press F2 and rename it to Alien.

Next, right click on the Assets panel and select Create -> Material. Name it Alien as well. Click on the white box next to "Main Color" and select whatever colour you want your alien to be:

Drag the material from the Assets panel onto the cube in the world view:

Other than using a solid colour, you can also select an image that you think would make it look more like an alien. To do this, right click in the Assets panel, select Import New Asset... and find the image you want on your filesystem. Once the image appears in your Assets panel, you just need to select the Alien's material and drag the image into the area that says "None (Texture)":

That's about as alien-like as it can get. This technique of wrapping an image around a 3D object is called texturing, and the image is called a texture.

Great! :) We covered some basic navigation controls in this article, as well as how to apply a material to a 3D object. The material may be either a solid colour or an image (texture).

In the next article, we will learn about linear interpolation, and use it to script the alien's sideways-moving behaviour. Until next time! :)