Intermediate Unity 3D for iOS: Part 1/3

This is a tutorial by Joshua Newnham, the founder of We Make Play, an independent studio crafting creative digital play for emerging platforms. Unity is arguably the most popular 3D game engine for iOS – and for many good reasons! Rapid development. Writing your game with Unity is far quicker than trying to write your […] By .

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

Separating the Scoreboard from the Scene

Currently, the scoreboard is a child of the background, but you want it as a separate object. So in the Hierarchy view, drag it from a child of the scene into the root of the Hierarchy. The following dialog will appear – click Continue.

Separating the storyboard from the scene

Note: GameObjects can have children and those children can have children, and so on and so forth. Children of a GameObject don’t directly inherit functionality of their parent (unless explicitly scripted), but rather use their parent’s scene space to position themselves. For instance, if the parent was moved along the x-axis, then all the children of the parent would also be moved e.g. if you held a basketball (your child) and walked forward, the basketball would move forward with you.

To make a child independent so that it doesn’t inherit the translation, orientation, and scale of its parent (also known as the object’s “pose”), then simply drag it out of its parent folder as you did here.

The reason you split out the Scoreboard like this was so you could add some 3D Text objects to it, to show the earned points and remaining time. This is not mandatory but rather an choice for aesthetics and organisation. Doing so helps (visually) de-couple it from the rest of the scene and gives you the choice of creating a prefab (which we will talk about later) that you would use if you were to create multiple levels.

The 3D Text object is — surprise! :] — an object that renders text in a 3D environment.

Add a 3D Text object to the scene by selecting the Menu GameObject -> Create Other -> 3D Text. This will place a new GameObject called New Text into your scene.

Rename this 3D Text object to Points and drag it into your Scoreboard object. Next, associate the scoreboard font with the 3D text by performing the following steps:

Drag the Project\NothingButNet\Fonts\Scoreboard\Font Material material to the Hierarchy\Scoreboard\Points\Mesh Renderer\Materials\Element 0 property

  • Drag the Project\NothingButNet\Fonts\Scoreboard font to the Hierarchy\Scoreboard\Points\Text Mesh\Font property

In the inspector for the 3D Text, set the default text to “0000” and the alignment to right middle aligned. Then use the Scene panel to position the font on the right spot on the Scoreboard. Note you may have to rotate the text to get it to show up right – for me, my settings were X=0, Y=0, Z=2.8, Rotation X=270, Y=180, Z=0.

Now perform the same step for the Time text by duplicating the Points text using Cmd-D and positioning it slightly below. Name the duplicated object Time and set the default text to 00:00.

In the end you should have something that looks like the following:

Screen Shot 2012 09 25 at 7 48 54 PM

Turn the Lights On

Have you noticed that the scene appears pretty dark? That’s because you haven’t added any lights to the scene, so let’s light it up!

Note: There are some considerations to be made when adding lighting to a scene. Adding lighting to a scene comes at a rendering cost. If you have done any Shader programming in the past, then you understand the additional effort to support the rendering of dynamic lighting. Each light requires each rendered object to calculate the final lighting effect depending on the Shader/Material being used, and has a high computational cost.

If at all possible, “bake” all the lighting details into the texture of the object before rendering. Baking is a way of rendering the textures with a static lighting effect applied, so that no additional computation is required to get the same visual effect. Also, use the scene’s ambient light to control the lighting intensity; this can be accessed via the Edit -> Render Settings panel.

In your simple scene, you will add a light. Select GameObject -> Create Other -> Directional Light to add a new directional light GameObject to your scene that will brighten everything up.

A full discussion about lighting is out of scope for this tutorial, but a directional light influences the whole scene depending on where you direct of the light by rotating the Directional Light GameObject. Select the light and use the move and rotate tools in the Scene panel to experiment moving it in different positions.

Setting up directional light in Unity

Camera Position

Now turn your focus (pun fully intended! :]) to the correct positioning of the camera. Camera positioning is not a science, but rather more of an art. Play the part of the director, and drag the camera into place using the movement and rotation tools in the Scene panel.

You can use the preview available in the Game panel directly below the Scene panel where you’re doing your scene building. As well, you can make use of the little preview window that pops up when the camera is selected in the Hierarchy view.

Next, in the you should update your Game screen dimensions to iPhone Wide 480×320, via the dropdown in the upper right of the Game panel. If you do not see this, go to Unity\Build Settings, and switch your platform to iOS. At this point, you should have a view similar to the one below:

Your scene is looking pretty good, isn’t it? Feel like a real director yet? :]

Unity Physics: Colliders and Bodies

It’s time to add Components to your Scene’s GameObjects so that they can react to each other!

Your goal is to have your objects react to each other when they collide. Luckily, Unity contains a fully-integrated physics engine and has packaged it up into a suite of Components that can easily dock onto your GameObjects.

Before you add ‘physics’ capabilities to your objects, you’ll need to first take a look into what ‘Physics’ means in Unity.

Click on the Components -> Physics menu (top toolbar) and have a quick look through the types of components readily available to you.

Colliders define the physical dimensions of your object, which can be independent of the visual shape of the object. In general, the colliders are ordered by complexity, and the more complex the object, the performance cost of using these objects rises.

Where possible, use Box/Sphere to encapsulate your objects, as these Colliders have the least computational load for your application. The other common Collider you’ll use frequently is the Mesh Collider. This uses the 3D model’s mesh to define the boundary of your object. In this case, the visual dimensions will equal the physical dimensions.

In addition to physically colliding with other objects, a Collider can be set up as a trigger that can detect collisions (so you can make something happen programmatically), but not actually cause any collision responses. This will be useful so you can detect when the ball goes through the basketball net.

In order for objects to react, each object must have some form of body in addition to a collider. This is done by either adding a Rigidbody or CharacterController to the GameObject.

The best way to get comfortable with Physics is to play around – let’s make that basketball bounce!