Quantcast
Channel: Intel Developer Zone Articles
Viewing all articles
Browse latest Browse all 3384

Game Development with Unity* - An Example

$
0
0

Download PDF

Collaboration between Intel and Unity Technologies has resulted in x86 native support for Android*, which is available in Unity* 5. To demonstrate this native support, this blog describes how to build a simple game with Unity in the style of Pac-Man* 3D.

What is Unity?

Unity is a cross-platform game engine developed by Unity Technologies and is used to develop video games for PCs, consoles, mobile devices, and websites. Unity Technologies is notable for its ability to target games for multiple platforms. Within a project, developers have control over delivery to mobile devices, web browsers, desktops, and consoles. Supported platforms for this popular game engine include BlackBerry* 10, Microsoft Windows Phone* 8, Microsoft Windows*, Apple OS X*, Android, Apple iOS*, Unity Web Player* (including Facebook*), Sony PlayStation* 3, PlayStation 4, PlayStation Vita, Microsoft Xbox* 360, Xbox One*, Nintendo Wii U*, Nintendo 3DS* line, and Wii*.

Unity is actively used by major companies such as Blizzard, Ubisoft, and Electronic Arts, and various Indie studios. For the Indie  studios, Unity is attractive because it has a free version and is relatively simple to use. Also the Unity Technologies Asset Store lets you buy or get some free assets to create your own game. For more information about Unity, to buy a Unity Pro version, or to get a free personal version, go to:http://unity3d.com/. For more information about the Asset Store, go to: https://www.assetstore.unity3d.com/en/.

Game Development

Introduction

Unity is simple to use, and even a beginning developer can create a real game with Unity. For this blog, I use the popular childhood game, Pac-Man, as the basis for the game, adding some elements from geometric games, which are popular in Google Play*. Thus we will create a game that will resemble a real commercial game.

Preparation

First we need to create a scene. I will not to spend a lot of time explaining how to move or where to put the object, because those actions are subjective. You can find all available game objects in the “GameObject” menu in the Asset Store. The menu provides various 2D and 3D objects, lighting sources, audio objects, and others.

For this example, the protagonist is a sphere, which we will call Player. There are some other spheres in the scene, which we will call Points. Player picks up these points. We also want to count the points that accumulate.

We can assign a script to each object in the scene. These scripts control movement around the scene for its own objects. The scripts should be created using C# or JavaScript*. The tacitly accepted rule is to call each script "controller." For example,  PlayerController or CameraController.

To create a script, follows these steps:

  • In the Project panel, select Create.
  • In the drop-down menu, select С# or JavaScript (in this case, C#)

When you have created the script:

  • Click the object in the Hierarchy panel for which you wrote the script.
  • In the Inspector panel, click the Add Component button, and then choose Script.

Moving Around the Scene

Here is the code for moving around the scene:

using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
	private Rigidbody RigidBody;
	public int SpeedUp = 10;
	void Start()
	{
		RigidBody = GetComponent<Rigidbody> ();
	}
	void FixedUpdate()
	{
		float VerticalMove = Input.GetAxis ("Vertical");
		float HorizontalMove = Input.GetAxis ("Horizontal");

		Vector3 Move = new Vector3 (HorizontalMove, 0, VerticalMove);
		RigidBody.AddForce (Move * SpeedUp);
	}
}

Let's look at this code in more detail.

First, we create a variable of the type RigidBody to tell Unity that this object can physically interact with other objects in the scene and move around the stage.

The Start() function does everything that happens at the moment the game starts. Inside this function we assign a RigidBody to our variable. Thus we can control our object with the help of a variable.

The FixedUpdate() function does everything that happens during the runtime of the game. With the help of Input.GetAxis, we get vertical and horizontal movement once we click the control button. You will need to create the appropriate variables and write values in them​.

Next we need to create a vector, which will move our object. There are two kinds of vectors in Unity: Vector2 and Vector3. We need to use Vector3 because we are developing a 3D game. Let’s create a variable with the type Vector3 and push the variables with horizontal and vertical data into it.

With the help of RigidBody.AddForce, the vector will make our object move. But our object will move slowly, so I created a SpeedUp variable to correct the movement speed.

Interaction with Points

We want to make sure that when Player touches Points, it destroys them. Also, let’s create a counter that counts the number of selected Points and types a message in the center of the screen, such as: “Congratulations! You picked up all points!”

Let’s add this codestring to our script:

<code>
void OnTriggerEnter(Collider other)
{
	if (other.gameObject.CompareTag ("point"))
	{
		other.gameObject.SetActive (false);
	}
}</code>

For interactions between objects or identification of interactions, the object's Collider is used. In our case when the Collider of Player contacts the Collider of another object, the object is destroyed. That is why we have the Collider of the other object as a parameter of the OnTriggerEnter function. We can assign a tag to all objects on the scene. This tag shows us which object has interacted with Player. The function other.gameObject.CompareTag signals us about a collision with Points with help from the tag “point”. Of course, we need to assign the tag "point" to all points we have and check Is Trigger in the Collider menu.

The next step is for the object “destroying” to use the other.gameObject.SetActive(false) function.

You can change the tag of objects in this section of Inspector:

You can change the tag of objects in this section of Inspector

We start to work with the UI because we want to create a counter of picked points. That is why we need to add the codestring to the top of the script as follows:

using UnityEngine.UI;

Before we start coding, we’ll add two text objects in the GameObject – UI – Text menu. Let's call them CountText and Message.

We’ll also add a private variable CountOfPickedPoint of type int and two public variables called CountText and Message.

The Start() function will be as follows:

<code>
void Start()
	{
		RigidBody = GetComponent<Rigidbody> ();
		CountOfPickedPoint = 0;
		SetCountOfPickedPointsText ();
		Message.text = "";
	}</code>

The OnTriggerEnter() function will become as follows:

void OnTriggerEnter(Collider other)
	{
		if (other.gameObject.CompareTag ("point"))
		{
			other.gameObject.SetActive (false);
			CountOfPickedPoint = CountOfPickedPoint + 1;
			SetCountOfPickedPointsText ();
		}
	}

The SetCountOfPickedPointsText () is a function as follows:

void SetCountOfPickedPointsText()
	{
		CountText.text = "Count of Picked Points : " + CountOfPickedPoint.ToString();
		if(CountOfPickedPoint >= 47)
		{
			Message.text = "Congratulations!You picked up all of the points!";
		}
	}

Thus, our text variables and counter will reset at the moment the game starts. Counter will increase and CountText will change when our Player interacts with Points. If the value of Counter is equal to or greater than the number of points on the scene, we get a message saying we successfully finished the game. Thus, we’ve finished our PlayerController.

Camera

Now we want to correct the camera options to follow Player. To do this, we change the CameraController.

  • We measure the distance between camera and player at the moment the game starts.
  • During runtime, we сhange the location of the camera by a predetermined distance.

Thus, our camera follows Player everywhere. The CameraController script is as follows:

<code>
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour
 {
	public GameObject Player;
	private Vector3 OffSet;
	void Start ()
{
		OffSet = transform.position - Player.transform.position;
	}

 
void LateUpdate ()
{
		transform.position = Player.transform.position + OffSet;
	}
}
</code>

That’s it! Plain and simple!

Build the game

Here are the steps to build the game:

Step 1

Build the game - step 1

Step 2

Build the game - step 2

Step 3

After clicking Build, specify the save location. That’s it! Your APK now has native x86 support.

About the Author

Artem Gruzdev works in Software & Service Group at Intel Corporation. His main interest is optimization of performance and parallel programming. In his current role as an Software Engineering Intern Artem works closely with PARSEC-3.0 Benchmark Suite developers to achieve the best possible performance on Intel platforms. Artem holds a Bachelor’s degree in Applied Mathematics and Computer Science from the N.I. Lobachevsky State University of Nizhni Novgorod.


Viewing all articles
Browse latest Browse all 3384

Trending Articles