How To Make a 2.5D Game With Unity Tutorial: Part 1

A while back, you guys said you wanted a tutorial on “How To Make a 2.5D” game.” You guys wanted it, you got it! If you don’t know what a 2.5D game is, it’s basically a 3D game that you squish so the gameplay is only along a 2D axis. Some good examples are Super […] By Marin Todorov.

Leave a rating/review
Save for later
Share
You are currently viewing page 5 of 6 of this article. Click here to view the first page.

Prefabricating your Game Objects

Remember that “Hierarchy” shows what’s on the scene currently, and “Project” holds all your object for you? This has to do something with our goal here – have many bombs loaded on the plane and release them at will into the sea.

What we are going to do is – we are going to prefabricate a game object (it will be ready and set to appear on the scene), but we won’t add it to the scene- we are going to instantiate (or clone if you are a sci-fi fan) this “prefab” into a real living game object on the scene.

Right-click inside the “Project” panel and choose “Create/Folder”, rename it to “Prefabs”. Right-click “Prefabs” and choose “Create/Prefab”. Rename the new prefab to “BombPrefab”. Notice the little cube icon is white – this indicates an empty prefab.

Now – drag the “Bomb” from Hierarchy onto the “BombPrefab” in “Project”. Notice the cube icon is now blue – means a full prefab, ready to be cloned. Also important – look at the “Hierarchy” panel now – “Bomb” font changed to blue color – that means this object now is an instance of a prefab.

Now that we have our bomb cookie cutter set, we don’t need the original bomb object on the scene – right-click on “Bomb” in “Hierarchy” and choose “Delete”.

Let’s get coding! Switch to MonoDevelop and open up the PlayerClass.cs. under the “speed” property declaration add:

public GameObject bombPrefab;

Have you guessed already? In this property we’ll hold a reference to the BombPrefab and we’ll make instances as we wish. Notice the property type is “GameObject”, as I said earlier everything in the game is a GameObject (much like NSObject in Cocoa), so it’s safe to set that type for just about anything.

Now switch back to Unity and select the “Player”. As you expected in the “Inspector” under “Script” there’s a new property “BombPrefab”. Let’s set its value: drag the “BombPrefab” from “Project” panel onto the “Inspector” where it says “None(GameObject)” and drop – now the field indicates it has BombPrefab prefab attached as value. Cool!

We’re going to need a C# class also for the bomb – right-click inside “Project” in “Class” folder and choose “Create/C Sharp Script”, rename it to “BombClass”. Right-click and “Sync MonoDevelop Project” – MonoDevelop pops up. Open up BombClass.cs and replace the contents with this code:

using UnityEngine;
using System.Collections;

public class BombClass : MonoBehaviour {
	private float ySpeed = -4f;

	// Update is called once per frame
	void Update () {
		transform.Translate( new Vector3(0f, ySpeed*Time.deltaTime, 0f) );
		if (transform.position.y < -11) {
			Destroy(this.gameObject);
		}
	}
}

This is pretty similar to everything we've done up to now - we translate the object every frame and when out of the screen bounds we react appropriately. In the bomb case we just want to destroy the object since we can always make new ones from our bomb prefab.

In the code note that "this" refers to the C# bomb class, while the gameObject property refers to the object on the scene - so we destroy the object on the scene and all components attached to it. We'll have a look at the game object hierarchy in Part 2 when you access components attached to an object programatically.

Bombing that shark!

Finally the part you've been waiting for - gratuitous violence! :]

Open up PlayerClass.cs. At the end of the Update method add:

if (Input.anyKeyDown) {
	GameObject bombObject = (GameObject)Instantiate(bombPrefab);
	bombObject.transform.position = this.gameObject.transform.position;
}

Let's go over this line by line.

  1. Input is the class giving you access to keyboard, mouse, accelerometer and touches. Input.anyKeyDown is true when a key is pressed, this happens only once - i.e. when the button was first pressed; then Input.anyKeyDown is false again until another key is pressed. anyKeyDown is actually a handy abstraction - it actually is true when a mouse button was clicked, keyboard key was pressed or (!) a tap on the iPhone's screen was received.
  2. (GameObject)Instantiate(bombPrefab) is the magic line that creates an instance from a prefab and adds it to the scene.
  3. Finally we set the position of the bomb to be same as the airplane's.

Cool - we have our bomb created when the player taps the screen, it then starts to fall down and when out of the screen destroys itself.

Let's give it a try! Switch back to Unity and hit Play - now if you click inside the "Game" panel (simulating a tap) you will see a bomb is created where the plane is at that point.

Click many times - many bombs are created. You should also hear the bomb sounds. But the bombs don't fall down! Why? Can you figure out what's the problem by yourselves already? Answer after the break.

Cans in Unity game not falling down

I hope you figured it out, but here's what's the problem: you haven't yet assigned the BombClass script to the Bomb Prefab - that's why the bombs don't fall down. Drag "BombClass" from your "Project" panel "Class" folder onto "BombPrefab" in "Prefabs" folder in the same panel. Check in the "Inspector" if you see the Bomb Class Script strip. Now hit Play again. That's better!

Properly falling cans in Unity game

Still not perfect though - those sharks don't die when you hit them. Since we already configured the colliders and the rigidbody component of the bomb, we just need to add the code to react to collision. Switch back to MonoDevelop and add this new method to the BombClass:

void OnTriggerEnter(Collider obj) {
	if (obj.gameObject.name == "Shark") {
		//reset shark
		obj.gameObject.transform.rotation = Quaternion.identity;
		obj.gameObject.transform.position = new Vector3(20f, -3f, 8f);
		Destroy(this.gameObject);
	}
}

Let's go over the code line by line again:

  1. OnTriggerEnter is a method called when the body attached collides with another body, and the 2nd body is passed as a parameter.
  2. Here we check if the object the bomb hits is called "Shark".
  3. If the shark is hit, then 1st reset the object rotation.
  4. 2nd, we reset the shark back to its original position.
  5. Finally, we call destroy this.gameObject to make the bomb disappear from the scene.

Pretty easy again! Isn't it? That's about all you need - switch back to Unity, and run your game! Hit sharks disappear and new ones come in.

You can also choose from the menu "File/Build&Run" and when Xcode pops up hit "Run" in Xcode - now you have the game on your iPhone - sweet!

A Simple 2.5D Game Created with Unity!

Contributors

Over 300 content creators. Join our team.