GilLabs { GilLabs }

Unity 101: A simple Space Invaders without coding

31 Jan 2017
PT FR

Hi guys,

Today we are going to create a simplified version of Space Invaders using Unity. Programming is not scope of this tutorial. So we are using few scripts for Visual Programming (actually condition-based scripts) I made for teaching Unity at my local university.

Tela inicial

Let’s start creating a new project inside Unity editor by pressing new button in the start screen. Now, let’s naming the project ‘MyGame’ and we can also define the project path where Unity is going to create all default initial project files.

Tela inicial

“MyGame” project created successfully! In the next figure, we can see the filesystem where Unity created three directories: Assets, Library and ProjectSettings.

Assets is where we should put files we’d like to use in our game: scripts, spritesheets, textures, 3d models, etc.

Library is used by Unity for generating assets for each build target. So here we are going to find the real spritesheets files that will be loaded by our game considering the configuration we choose. For example, we can choose a downscaled resolution for Android target and a heavier compression and resolution for Desktop target.

ProjectSettings stores our project configuration: project name and version, tags and layers systems, build targets, etc.

Temp is a temporary folder for assembling our necessary files into final build executable (.exe, .app, .apk, etc).

In a nutshell: Assets, ProjectSetttings are meant for files and configurations that will be changed by ourselves while developing a game. Library e Temp are only for Unity internal stuff, we should not change anything and neither add them to version control.

Tela inicial

A important thing to understand is that Unity projects are made of scenes. Which scene corresponds to a part of the game experience, like for example, a menu scene, level 1 scene, boss level scene, etc. Inside each scene we can find a lot of objects that are meant to player interact with, like menus, characters, power-ups items. These objects are known as GameObjects. And each GameObject have few components that allows to change its main characteristics. We are going to understand better in the next steps of this tutorial.

Unity interface is comprised by these main tabs:

  • Hierarchy: shows which GameObjects are in scene.
  • Inspector: allows to ‘inspect GameObjects’, to see its components and properties or to change project and assets configurations.
  • Project: mirrors the filesystem in path ‘MyGame/Assets/’
  • Scene: is a visualization into a specific point of game scene, where we can see and place GameObjects.
  • Game: depicts the actual rendered game, the game being executed.
  • Console: an important tab for development, shows scritpts warnings and errors.
  • AssetStore: where we can download, buy and sell game assets of any type.

Tela inicial

That’s the default tab layout. We can change it in the top right dropdown menu. Each option has a different tab layout. In addition we can also move freely our tabs and create and create our own layout by clicking ‘Save Layout’ option. In the image I’ve create my ‘CrazyLayout’.

Tela inicial

So far our game is comprised by two GameObjecs: “Main Camera” and “Directional Light”. The list of GameObjecs can be seen in Hierarchy tab:

Tela inicial

In Camera GameObject there are few components: Transform, Camera, GUILayer, FlareLayer and AudioListener.

Tela inicial

The Camera component allows to render the game. It’s like a way to see what’s happening in game execution for a given position. Just like a real camera, it films our game scene and presents it to the player to be seen in Game tab.

Tela inicial

The Transform component has a very important role: it defines the position, rotation and scale of GameObjects in scene space.

Tela inicial

Let’s create few folders in Project tab. Usually it’s recommended to follow a folder naming structure to ease the task of searching for assets while the projects grows up.

Tela inicial

In this project we are going to use the following folders: Prefabs, Scenes and Scripts.

Tela inicial

Let’s save our scene in Scenes/Game.unity.

Tela inicial

Unity has basic 3d elements (cube, sphere, cylinder, …) that we can use in our games. By clicking with right mouse button we see the options of the figure below and add the cube into game scene.

Tela inicial

In Hierarchy tab we can check our added cube.

Tela inicial

And in Inspector tab we can see its components:

  • Transform: stores position, rotation and scale changes
  • MeshFilter: has information about object 3D geometry (aka. Mesh)
  • BoxCollider: an important component to detect collisions during game execution
  • MeshRenderer: renders our cube using geometry information (Mesh data) from MeshFilter component and using informations from Materials.

Tela inicial

We can change cube position by editing Position field of Transform component. It’s also possible to just select and move arrows that shows up when selecting the cube. That kind of visual element is called Gizmo.

Tela inicial

When interacting with those red, blue and green Gizmo we can see the cube moving in X,Z,Y axises. We also see the changes taking effect on Position field of Transform.

An useful tip: we can reset all modifications on transform by choosing Reset from component options (Top right small engine icon).

Tela inicial

To ease our task of manipulating cubes, we are going to change snap configuration. In snap mode our cube can be dragged by a fixed amount.

Tela inicial

In Snap Settings window we can configure our GameObjects movement in snap movement. For now let’s leave the default settings as we can see in figure below.

Tela inicial

When pressing Ctrl+D or Cmd+D we can clone the selected GameObject. And when pressing Ctrl/Cmd we move GameObjects in snap increments.

Tela inicial

By cloning 13 cubes, i.e, pressing Ctrl+D or Cmd+D, we can make a line

Tela inicial

The Empty option allows to create an empty GameObject with only Transform component.

Tela inicial

Let’s rename the empty GameObject to Line and then reset Transform component to its default values. After selecting all cubes, we can drag them into Line GameObject on Hierarchy tab. Line is now the parent of all cubes. When selecting or moving Line we are also moving its children.

Tela inicial

Changing scene shading options we can achieve a better visualization for our cubes.

Tela inicial

Now we clone the Line GameObject, what means we are also cloning all its 13 children cubes. In this way we can create two more lines and drag them up. And when select a specific cube we can also delete it by del/Cmd+del. So by repeating these basic steps, we can get the following structure.

Tela inicial

Then we copy one more line to get the body of our character. To ease manipulating these objects, let’s create an empty Body GameObject, reset its Transform and put Line objects inside it as children.

Tela inicial

To create the antenna we need to copy only one cube.

Tela inicial

Let’s create an empty GameObject Antenna and reset its Transform and parenting that last created cube.

Tela inicial

Cloning few more cubes we finish our Antenna.

Tela inicial

The next step is to create Claws GameObject and reset its Transform.

Tela inicial

Clonning few more cubes as arranged in the below image, we finished the claws of our character.

Tela inicial

Now we have a Space Invader. To ease manipulating it, let’s create an empty Cubes GameObject, reset its Transform and drag Body, Antenna and Claws into it. Then we can finally create a SpaceInvader GameObject, reset its Transform and parenting all cubes.

Tela inicial

By selecting Cubes let’s change its scale to (0.2, 0.2, 0.2).

Tela inicial

By clicking in Game tab we can see how our character is going to be seen by the player.

Tela inicial

In our Camera let’s change the propriety Clear Flags to Solid Color and background color to (0,0,0,0).

Tela inicial

Cloning and creating new empty GameObjects as we did in previous steps, we have our Player GameObject.

Tela inicial

In Game tab we can see our game characters.

Tela inicial

To add movement to our game, let’s use some scripts I’ve made beforehand from my Visual Behaviors project. Download or clone this project into Scripts folder.

Tela inicial

By inspecting SpaceInvader GameObject we can add new components by pressing Add Component button.

Tela inicial

Let’s add Move script (from Visual Behaviors).

Tela inicial

Let’s configure Move script.

Tela inicial

CallEvent script allows to change another scripts behaviors.

Tela inicial

Inside Action block, let’s press + button to add an action. So we select a GameObject that should have its behavior changed.

Tela inicial

Now, let’s choose which component method we are going to change. Let’s call ChangeDirectionX, a method that allows to invert current movement direction of Move script.

Tela inicial

Player movement can done by two Move scripts: one for right direction and another for left direction.

Tela inicial

Then we create Bullets GameObject as depicted in below image:

Tela inicial

Let’s add the Move script to Bullets in order to move up the bullets towards space invaders.

Tela inicial

By dragging Bullets into Project tab we can create a prefab, a pre-fabricated object that can be replicated and instantiated during game execution.

Tela inicial

Let’s change the layer of Bullets GameObject.

Tela inicial

Let’s create the Bullets layer.

Tela inicial

And then change the Bullets prefab.

Tela inicial

When adding Generate script to Player GameObject we can allow our player to shoot.

Tela inicial

In SpaceInvader GameObject we are going to add DestroyMe script. Let’s configure our enemy to be destroyed every time it touches a bullet, I mean a GameObject with Bullets layer.

To collision be detected, we need a BoxCollider and a RigidBody components. Let’s freeze Rigidbody rotation and define gravity as zero.

Tela inicial

We can’t forget to activate isTrigger checkbox in BoxCollider of our enemy.

Tela inicial

Let’s add a trigger-enabled BoxCollider in Bullet GameObject:

Tela inicial

So we achieved a working Space Invader! Congrats for your first Unity game!