おもちゃラボ

Unityで遊びを作ってます

【Unity Tutorial 】How to make a shooter game

In this article, I'd like to introduce how to use Unity by making a simple shooter game. This article consists of 3 part.

  1. move the rocket and fire bullets.
  2. drop meteorites and collision detection
  3. make UI and game over

We are making the shooter game and scripts according to the book "Unity's textbook" written by me, so please refer to this book as well.

Overview of this shooting game

The game we are going to make will look like this.

f:id:nn_hokuson:20160702231742g:plain

Move your ship with the left and right keys, and shoot the bullet with the space key. Meteorites falls from the top of the screen, you should move ship and shoot the bullet and destroy it. If the meteorite goes beyond the bottom edge of the screen, the game will be over.

Create a Unity project

Let's create a Unity project. On the Unity startup screen, input the project name "Sample Shooting" and select the game type "2D".

f:id:nn_hokuson:20160703084730p:plain:w500

Next, select the screen aspect. Our game use a portrait screen layout, so click the Game tab and select the aspect ratio. Unity does not have a vertical aspect ratio , so click on the bottom "+ button" and select "Aspect Ratio" for a Type field and input "9: 16" for Width & Height field.

f:id:nn_hokuson:20160703085223p:plain:w500

Drag and drop the assets used in this game to Unity's project view. Assets for our game can be downloaded from the following URL.

www.dropbox.com

Let's add a rocket image to a scene view

At first, we gonna add rocket image(In Unity2D, image is called "sprite") on your scene view. Drag and drop the rocket sprite from the project window into the scene view.

f:id:nn_hokuson:20160703090750p:plain:w500

The rocket will be displayed on the screen.

The rocket sprite added in the scene view is also listed in the hierarchy view. Remember that the sprites on the scene view are correspond to thd list in the hierarchy window .

f:id:nn_hokuson:20160703091057p:plain:w500

You can change the position of the rocket from the inspector. When you select "rocket" in the hierarchy view, the rocket details are displayed in the inspector on the right side of the Unity editor.

In this example, change the rocket position to (0, -4, 0).

f:id:nn_hokuson:20160703093231p:plain:w500

Move the rocket

Next, Create a C# script to move the rocket with the left/right key. In Unity, you can use programming languages ​​such as C# or Javascript. Here we gonna develop our game using C#.

Right click on a project window and select "Create" → "C# Script", and
then change the file name to "RocketController.cs".

f:id:nn_hokuson:20160703093649p:plain:w500

Double click on RocketController.cs and open the code editor(Mono develop or Visual Studio) and enter the following script.

using UnityEngine;
using System.Collections;

public class RocketController : MonoBehaviour {
	void Update () {
		if (Input.GetKey (KeyCode.LeftArrow)) {
			transform.Translate (-0.1f, 0, 0);
		}
		if (Input.GetKey (KeyCode.RightArrow)) {
			transform.Translate ( 0.1f, 0, 0);
		}
	}
}

This script checks whether the keys(LertArrow and RightArrow) are pressed by using the Input.GetKey() method. When these keys are pressed, use the Translate() method to move the rocket in the direction of the arrow key. Translate () is a method that moves the object in the direction and distance of the argument. Note that the argument is not the absolute coordinate of the destination.

Next, we attach the RocketController script to the rocket object. In order to move an object as written in a script, you need to attach a script to the object.

Drag and drop "RocketController.cs" from the project view to "rocket" object in hierarchy view.

f:id:nn_hokuson:20160703093927p:plain:w500

Press the play button at the top of the Unity editor. You can move the rocket with the right/left key.

f:id:nn_hokuson:20160703094256g:plain

Let's add bullet sprite

The flow of firing a bullet from a rocket is as follows.

  1. Place a bullet sprite and move it upwards.
  2. Make a "bulletPrefab" based on the bullet you created.
  3. Write a bullet generator script. In this script, make an instance of a bullet from Prefab when you press the Space key.

At first, add the bullet's sprite on the scene view. Drag and drop "bullet" object from the project window to the scene view. Since the position of the bullet is specified by the script, you can place your bullet anywhere.

f:id:nn_hokuson:20160703131017p:plain:w500

After the bullet is added, you should make a script to move the bullet upwards. Right click in the project window and create a script with "Create" → "C# Script" and change the file name to "BulletController.cs".

f:id:nn_hokuson:20160703131228p:plain:w500

Open the "BulletController.cs" and write the following script.

using UnityEngine;
using System.Collections;

public class BulletController : MonoBehaviour {
	void Update () {
		transform.Translate (0, 0.2f, 0);

		if (transform.position.y > 5) {
			Destroy (gameObject);
		}
	}
}

We use the Translate() to move bullets upward (y-axis direction) by 0.2f every frame. This is the very similar way we used to move the rocket.

Even if bullets are out of the screen, bullets will continue to keep moving. Since this is a waste of CPU power, let's destroy the bullets when bullets go beyond the top of the screen (y = 5).

Save the script and attach it to the bullet just as you did with the rocketController.cs. Drag and drop "BulletController.cs" in the project window to the "bullet" in the hierarchy view.

f:id:nn_hokuson:20160703131433p:plain:w500

You attach the script, press the play button and run the game. The bullet moves upwards.

f:id:nn_hokuson:20160703131736g:plain

Make bullet Prefab

Next, we will make a Prefab of the bullet. Prefab is like a blueprint, and you can easily duplicate the same object using Prefab.

Select "bullet" in the hierarchy view to create bullet Prefab and drag & drop it to the project window. Then change the created file name to "bulletPrefab".

f:id:nn_hokuson:20160703131918p:plain:w500

After you create the bullet Prefab, the bullet on the scene view are not necessary. This is because you can create bullets from the Prefab anytime if you have blueprint. So right click in a hierarchy window and select "Delete".

f:id:nn_hokuson:20160703132034p:plain:w500

Create an instance from Prefab

Next, create a script to generate an instance (duplicate) of bullet from bullet Prefab when the space key is pressed.

Open the "RocketController.cs" and modify the script as follows.

using UnityEngine;
using System.Collections;

public class RocketController : MonoBehaviour {

	public GameObject bulletPrefab;

	void Update () {
		if (Input.GetKey (KeyCode.LeftArrow)) {
			transform.Translate (-0.1f, 0, 0);
		}
		if (Input.GetKey (KeyCode.RightArrow)) {
			transform.Translate ( 0.1f, 0, 0);
		}
		if (Input.GetKeyDown (KeyCode.Space)) {
			Instantiate (bulletPrefab, transform.position, Quaternion.identity);
		}
	}
}

In this script, we use the GetKeyDown() to detect that the space key is pressed. GetKey() returns true while the key is pressed, whereas GetKeyDown() return true only once when the key is pressed . When space key is pressed, make bullet instances from bullet Prefab. To create a bullet instance, we use Instantiate(). The Instantiate method requires three arguments:

  • Prefab
  • the position at which the instance
  • the rotation angle of the instance

Since transform.position is specified as the second argument, bullets are generated at the position of the rocket.

Finally, set the bulletPrefab to the "bulletPrefab" variable in RocketController script . Select rocket from the hierarchy window, and find the item "RocketController script" from the inspector, drag and drop "bulletPrefab" from the project window into the "bullet Prefab" field.

f:id:nn_hokuson:20160703152733p:plain:w500

Then bullets are fired each time when you press the space key.

f:id:nn_hokuson:20160703153002g:plain

The project file created this time can be downloaded following URL.

www.dropbox.com

Next article is as follow, please click here! (still Japanese)

nn-hokuson.hatenablog.com