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.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
How To Make a 2.5D Game With Unity Tutorial: Part 1
45 mins
- Installing Unity
- Unity Editor Overview
- Setting up an iPhone Unity3D project
- Running the Game on Your iPhone
- Setting up the Scene
- Adding 3D Objects to the Scene
- Beginning Unity3D programming with C#
- Jumping the Shark
- Adding the Clown Fish
- Set Us Up The Bomb
- Prefabricating your Game Objects
- Bombing that shark!
- Where To Go From Here?
Jumping the Shark
Go ahead and download and unarchive the Shark model. As you did before with the airplane, drag the file “shark.obj” onto the “Project” panel inside the “Models” folder and “sharktexture.bmp” inside “Models/Textures”.
From the menu choose “GameObject/Create other/Capsule” – rename the “Capsule” object in “Hierarchy” to “Shark”. In “Inspector” inside the “Mesh Filter” strip click the circle with dot inside and in the popup window double click the Shark model. Now you should see the Shark geometry in the “Scene” and “Game” panels.
Drag “sharktexture” from “Project” panel (it’s inside Models/Textures) onto the “Shark” object in “Hierarchy” – this gives your shark a vigorous mouth and some evil eyes! Pfew – I already want to bomb it!
Make sure “Shark” is selected and set the following properties in the “Inspector”: Position – [20, -3, 8], Scale – [1.2, 1.2, 1.2] – this will put the shark just right off the camera visible box – it’ll start moving from there towards the left side of the screen.
Now since we’d want the shark to interact with our bombs (by exploding, mwhahahahah!) we want the Collider of the shark to match more or less the shark’s geometry. As you see there’s a green capsule attached to the shark inside the scene. This is the shark’s collider. Let’s have it matching this evil predator’s body.
In the “Inspector” find the “Capsule Collider” strip and set the following values: Radius to “1”, Height to “5”, Direction “X-Axis”, Center to [0, 0, 0]. Now you see the capsule collider has rotated and matches more or less the shark’s body – better!
Last – select “shark” model in “Project” panel “Models” folder then in the “Inspector” find “Normals” and select “Calculate”, then scroll down and click “Apply” button.
Right-click inside the “Project” panel in “Class” folder and choose “Create/C Sharp Script”, rename the new script to FishClass. Righ-click and choose “Sync MonoDevelop Project”.
MonoDevelop will pop up. Open the FishClass.cs file and put inside the following code:
using UnityEngine;
using System.Collections;
public class FishClass : MonoBehaviour {
public float speed = 6f;
// Update is called once per frame
void Update () {
if (transform.position.x < -30 || transform.position.x > 30) {
//turn around
transform.Rotate(new Vector3(0,180,0));
transform.Translate( new Vector3(-10, -transform.position.y + Random.Range(-4,-1),0) );
//get new speed
speed = Random.Range(6f,10f);
}
transform.Translate( new Vector3(-speed*Time.deltaTime,0,0) );
}
}
It’s pretty similar to what we have already for the airplane. We have a speed property (per second) and in the Update event handler we use transform.Translate to move the shark.
Notice this time I used:
transform.Translate( new Vector3(x,y,z) );
This is just to demo that some of these methods can take different parameters – however passing separately 3 values or 1 vector is pretty much the same.
Now let’s see what the shark does when it reaches the bounds of the screen (-30 and 30 in this case, so there’s is a moment when the shark is offscreen, so you can’t easily ambush it when entering back).
When the shark reaches left or right bound it turns around moves a bit towards the bound and changes speed. This way it just goes back and forth, back and forth continuously.
The call to transform.Rotate(new Vector3(x,y,z)) obviously rotates the object around the axis by the given values, and transform.Translate(new Vector3(x,y,z)) you already know well.
Pretty easy! Switch back to Unity and drag the “FishClass” script onto the “Shark” object in “Hierarchy”. Now hit Play: you can see the huge shark going back and forth waiting to be bombed. Good job!
Adding the Clown Fish
Let’s do the same procedure again for our ClownFish. I’ll put it into a nice list for quick reference:
- Download and unarchive the ClownFish model.
- Drag “mesh_clownfish.obj” into “Project” panel inside “Models” folder and “clownfish.bmp” inside “Models/Textures”.
- Choose “GameObject/Create other/Capsule” and rename the “Capsule” object in “Hierarchy” to “ClownFish”.
- Click “Mesh Filter” circle-with-a-dot-button and from the popup double click the clownfish geometry.
- Drag the “clownfish” model texture onto “ClownFish” object in “Hierarchy”.
- While having “ClownFish” selected change the following properties in the “Inspector”:
- Position to [-20, -1, 7]
- Rotation to [0, 180, 0]
- Scale to [0.4, 0.3, 0.3]
Hit Play and see what happens – now you have two moving fish without having to write any extra code!
Everything works perfect – the fish go back and forth, plane is wrapping. We need some boooombs!
Set Us Up The Bomb
Download and unarchive the Can model. As usual, drag the “colourcan.obj” file in “Project” panel “Models” folder and “cantex.bmp” file in “Models/Textures”.
From the menu “GameObject/Create Other/Capsule”, rename the object to “Bomb”. From the Mesh Filter popup double click the can geometry. Drag the “cantex” texture onto the “Bomb” object in “Hierarchy”. In “Inspector” “Capsule collider” click this button to open the popup menu:
When the popup menu appears, choose “Reset” – this way the collider automatically will take the size of the geometry assigned – cool!
Next select the “colourcan” model in “Project” panel “Models” folder then in the “Inspector” find “Normals” and select “Calculate”, then scroll down and click “Apply” button.
Now let’s dive into new stuff! Select the bomb object again, and inside the Capsule Collider strip check the “Is Trigger” checkbox – aha! Check this to make the bomb object will trigger event when it collides with other objects.
But for this to happen we need to also assign a Rigid Body to the bomb (as at least one of the colliding objects needs to have a rigid body). From the menu choose “Component/Physics/Rigidbody” (Bomb should be selected in the Hierarchy!).
Once you do this, a new strip should appear in the “Inspector” called “Rigidbody”. Uncheck “Use gravity” (we won’t use gravity) and check “Is Kinematic” to be able to control the body programatically. This was all we needed to have collisions enabled!
Download and save on your disc this bomb releasing sound (which I made myself, lol!)
We would like to play this sound when the airplane releases a bomb, i.e. when the bomb first appears on the screen. Let’s do that!
Right-click in the “Project” panel and choose “Create/Folder”, rename the new folder to “Audio”. Drag the “bahh1.aif” file onto the “Audio” folder. Next drag the “bahh1” sound file from “Project” panel onto the “Bomb” object in “Hierarchy”.
Believe it or not, that’s all we need to do – the sound is attached to the bomb and will play when the bomb appears on screen. Notice how easy some things are with Unity?
Select the “Bomb” object in “Hierarchy” and in “Inspector” find the “Audio Source” strip: see that “Play On Awake” checkbox is checked – this tells the audio to play when the object appears on the scene. Also look at the “Scene” panel – see the bomb has now a speaker attached?