Intermediate Unity 3D for iOS: Part 2/3

This is a tutorial by Joshua Newnham, the founder of We Make Play, an independent studio crafting creative digital play for emerging platforms. Welcome back to our Intermediate Unity 3D for iOS tutorial series! In this tutorial series, you are learning how to create a simple 3D game in Unity called “Nothing but Net”. In […] By .

Leave a rating/review
Save for later
Share

Learn how to make a simple 3D game with Unity!

Learn how to make a simple 3D game with Unity!

Learn how to make a simple 3D game with Unity!

This is a tutorial by Joshua Newnham, the founder of We Make Play, an independent studio crafting creative digital play for emerging platforms.

Welcome back to our Intermediate Unity 3D for iOS tutorial series!

In this tutorial series, you are learning how to create a simple 3D game in Unity called “Nothing but Net”. In the first part of the tutorial, you learned about the following Unity concepts:

  • The Unity3D Interface
  • Assets
  • Materials and Textures
  • Scene Positioning
  • Lighting
  • Camera Positioning
  • Physics and Colliders
  • Prefabs

Everything in your scene looks pretty sharp, but so far everything you’ve done has been through Unity’s visual scene designer. In other words, you haven’t written any code yet!

Well, that’s about to change :] In this part of the tutorial, you’ll start to breathe some life into your game through code, and add some interaction and animation into the scene!

This tutorial picks up where the previous tutorial left off. If you want to start at a “known good” state, you can use the project where we left it off in the previous tutorial. To open it in Unity, go to File\Open Project, click Open Other, and browse to the folder. Note that the scene won’t load by default – to open it, select Scenes\GameScene.

Let’s get started!

Making Sure Everybody Plays Together Nicely

Before you get too deep into code, take a quick look at the diagram below, which shows the functionality and responsibility of each component you’ll be adding to the game, as well as the relationships between the components:

Class diagram for app

At the center is the GameController. This is an abstract GameObject meaning that it is not associated to any physical element on the stage, but rather used to manage the scene. In this instance the GameController is responsible for coordinating the various states of the game activities providing access to the input from the user.

The next Component is the ScoreBoard. This encapsulates methods to update the points and time 3D Text GameObjects in the scene.

Next is the Player who is responsible for responding to user input, and managing the various properties of the ball, including the ball’s position.

Finally, what would your game be without the Ball? This object is responsible for triggering specific events that indicate when the Ball has gone through the hoop, and when the Ball has landed on the ground, signifying that the player’s turn is over.

Scripting, Scripting, Scripting

Unity provides a choice of several different scripting languages; these include Boo (no, I’m not trying to scare you, it’s actually a language!), Javascript (a.k.a. UnityScript), and C#. In general, if you’re coming from a front-end web development background, then UnityScript is probably the best choice.

However, if you are more comfortable with C++, Java, Objective-C, or C#, then C# is a better choice for your scripting tasks. Since most of the readers of this site have an Objective-C background, in this tutorial you’ll be writing your scripts in C#.

Each script will be a Component unto itself, which will be attached to a GameObject. The base class you will be extending is the MonoBehaviour and includes a whole set of pre-defined properties, methods, and hooks.

Note: Are you wondering what “hooks” are? Hooks are callbacks or messages that are propagated to all Components for certain events, one example that we’ll be using is the OnTriggerEnter method which is called when two Colliders intersect each other (where on has the Is Trigger flag set to true but more on this later).

Note: Are you wondering what “hooks” are? Hooks are callbacks or messages that are propagated to all Components for certain events, one example that we’ll be using is the OnTriggerEnter method which is called when two Colliders intersect each other (where on has the Is Trigger flag set to true but more on this later).

Let’s try this out! In the Project panel, select the Scripts folder, click Create, and click C# Script:

Screen Shot 2012 09 25 at 10 06 53 PM

In the Inspector, you will see that it has created a default script for you that looks similar to the following:

using UnityEngine;
using System.Collections;

public class DummyScript : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }
}

The Start() and Update() methods above are known as hook methods; they are called by the engine during each frame update, also known as a “tick”. One of the core behaviors of a game engine is the continuous update and render cycle. An object is moved, then the scene is re-rendered. The object is moved again, and the scene is rendered once more. Lather, rinse, repeat! :]

When a Component is first instantiated, the Awake() method (which isn’t listed here, but you can override it) will be called. Once the Awake() method has been called on all active Components, then the Start() method will be called. The Update() method is called next, and will be called during each frame update or “tick”.

Note: MonoBehaviour has another update method called FixedUpdate(). This method is called by the physics engine and should only be used to update Rigidbody or other physics-based properties. It’s called FixedUpdate() as it’s guaranteed to be called at fixed intervals, unlike the Update() method which is called every “tick”, where the amount of time between ticks can be variable.

Note: MonoBehaviour has another update method called FixedUpdate(). This method is called by the physics engine and should only be used to update Rigidbody or other physics-based properties. It’s called FixedUpdate() as it’s guaranteed to be called at fixed intervals, unlike the Update() method which is called every “tick”, where the amount of time between ticks can be variable.

ScoreBoard

Start off with the ScoreBoard script, which is fairly straightforward. You’ve already created a the script, so just rename it to Scoreboard, then double-click it to open it.

Aha! Bet you didn’t know Unity included MonoDevelop!

MonoDevelop screenshot

Note: MonoDevelop is a full IDE for C# development and discussing all of the features and functionality is out of scope for this tutorial. However, if you limit what you do to editing and saving files, you’ll do just fine. If you want to use more advanced features, you can find out more about MonoDevelop at MonoDevelop.

Note: MonoDevelop is a full IDE for C# development and discussing all of the features and functionality is out of scope for this tutorial. However, if you limit what you do to editing and saving files, you’ll do just fine. If you want to use more advanced features, you can find out more about MonoDevelop at MonoDevelop.

Insert the code below into the new script:

using UnityEngine;
using System.Collections;

public class ScoreBoard : MonoBehaviour
{        

    public TextMesh pointsTextMesh;
    public TextMesh timeRemainingTextMesh;

    void Awake ()
    {       
    }

    void Start ()
    { 
    }

    void Update ()
    {    
    }

    public void SetTime (string timeRemaining)
    {
        timeRemainingTextMesh.text = timeRemaining;     
    }

    public void SetPoints (string points)
    {
        pointsTextMesh.text = points;   
    }   
}

The script above introduces the concept of publicly accessible properties. In this case, those properties are the Points and Time 3D Text objects that are children of your ScoreBoard object.

Making these properties public means they will become visible in the Inspector panel, which will allow you to assign them at design time via the editor. Once the properties have been assigned, you’ll be able to modify their text properties via the setter methods SetTime() and SetPoints().

Once you’ve created the script above, switch back to Unity and attach it to the ScoreBoard object. To do this, simply drag the script object and drop it on top of the Scoreboard object.

Next, drag and drop each of the 3D Text child objects from Part 1 of this tutorial onto the appropriate property in the right hand column as shown below:

This associates the 3DText child objects with the script property. Pretty simple, eh?