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 4 of 5 of this article. Click here to view the first page.

Bounce that Basketball!

Select the basketball, and add a Rigidbody onto it by selecting Component > Physics > Rigidbody. Then hit the Play button in the upper center of Unity to preview the gameplay – you’ll see the basketball fall below the floor.

If your basketball “warps” to the middle of the scene when you click play, select the basketball and unclick the Animation checkbox in the properties and try again. It should stay in the right spot now.

But it wouldn’t be much of a game if the ball was allowed to fly out of the scene! :] You’ll need to create a set of boundaries that will constrains the ball to the playing area.

To do this, select the scene.Wall object and select Component > Physics > Mesh Collider. Repeat for the scene.Ground and scene.Background objects. Then select the scene.court object and select Component > Physics > Box Collider.

If you play the scene again, you’ll see that it still falls through the floor. This is because you still haven’t set up a collider for the basketball!

So select the basketball and go to Component > Physics > Sphere Collider to set up a sphere collider for your basketball. It will default to the right size, but you can change the radius if you want in the Inspector’s Sphere Collider section.

Along with the ball reacting to the environment, you’ll also want it to bounce when it collides with an object. To do this, you’ll need to assign a special type of Material that Unity provides called a Physic Material.

Where Materials affect how objects look, Physic Materials determine how the object behaves when a collision occurs. This is associated with the Material property of the GameObjects Collider component.

The following image shows the properties of the Rigidbody attached to the basketball:

On the Project panel, select the Create dropdown menu and then select Physic Material to create a Physic Material , and name it BallPhyMat.

Now set the properties of the Physic Material as shown below. Details on the function of each of the properties shown below is out of scope for this tutorial, but further information can be found at http://docs.unity3d.com/Documentation/ScriptReference/PhysicMaterial.html.

In order to allow the ball to bounce, friction is set fairly low.

To associate the newly created Physic Material to the basketball, select the Physic Material you just created and drag it to the to the basketball’s Collider material property.

Click the play button again, and this time it falls to the floor and bounces, w00t! :]

As for the hoop, you want it to react to the ball as well as detect when the ball goes through the net. Add a Mesh Collider to the hoop mesh (hoop.LeftHoop).

Also, you want to set up a sensor or “trigger” to detect when the ball goes through the hoop. To do this, add a Box Collider to hoop.LeftHoop_001, but shorten the box and position it so it’s just below the net (you can do this by tweaking the values in the Inspector – I changed Center Z to -1.4 and Size Z to 0.2). Also, click the checkbox to set the trigger property to true.

Adding a trigger to the hoop in Unity

Note: to visually resize colliders using the mouse, select the collider’s object and hold down Shift. This will show the handles of the collider, allowing you to resize it using the mouse.

Okay! That takes care of the ball — time to take a look at the Player object! :]

Meeting the Team

In this game, we want the player to bounce the ball and for the ball to not roll through him. Stopping the ball rolling through the Player is easy enough; go ahead and add a Capsule Collider to the player GameObject.

You’ll then need to position and size the capsule – I changed the height to 3.8 and the Center Y to 1.8.

Note: Colliders are shown in the Scene panel in a green outline — unless they are Mesh Colliders , in which case it’s the mesh that shows the collision boundaries.

The approach used to bounce the ball requires that the game can detect when the ball collides with the player’s hand. When this occurs, you will push the ball back down to the ground, just like it happens in real life! To attach the collider at the correct position, you’ll drill down to the player’s skeleton and add the collider to the hand which will be used to bounce the ball.

The above screenshot shows you the children of the player GameObject. Where do these children come from? Good question! :]

These children form the skeleton built to animate the player – the parent is the pelvis which also has the Skeleton Mesh Renderer component attached to it, which is responsible for rendering the mesh,. The children are the bones of the skeleton which were built in Blender. ArmIK_L, ArmIK_R, LegIK_L, LegIK_R are just handles used in Blender and have no function in your app.

Now add a Box Collider to player\BPlayerSkeleton\Pelvis\Hip\Spine\Shoulder_R\UpperArm_R\LowerArm_R\Hand_R and resize the Collider similar to what is shown below, and set the trigger flag to true.

Adding a trigger collider to the hand

Prefabs — and how to Take Advantage of Them

This section is just an optional note that may be useful for you later – feel free to skip if you need a break! :]

A game app normally consists of a lot of objects that are identical but are used multiple times. For instance, you may have created a city using the same building over and over again. An efficient way to do this is to create a master template which you can reuse over and over again. As well, it gives you the advantage of being able to update all of the objects by simply updating the template.

Unity provides the ability to do this through the use of Prefabs. Prefabs allow you to create one master copy of an object and create multiple identical copies of it. However, even if you don’t want to create multiple copies of your object, it still provides an efficient way of creating and managing a setup for each of your game objects.

To create a Prefab, you can either just drag over an object from your Hierarchy panel to your Project panel, or you can explicitly create a Prefab via the Project dropdown and then drag the object(s) from your Hierarchy panel into this object.

Now each time you want to create another object from this template, you just need to drag the Prefab over to the scene. Very easy — and very useful! :]

Note: to make an update to your Prefab, just grab any Prefab of the type you’re interested in, make your updates, and then select the Game Object -> Apply Changes To Prefab option from the toolbar menu. The changes will automagicall propagate to all associated objects!