Beginning Unity 3D for iOS: Part 2/3

In this second tutorial,, you’ll add functionality to enrich the project, including better player movement and better game scenery. You’ll also learn how to use Unity Remote for debugging. By Christine Abernathy.

Leave a rating/review
Save for later
Share

Learn how to use Unity to make a simple 3D iOS game!

Learn how to use Unity to make a simple 3D iOS game!

Learn how to use Unity to make a simple 3D iOS game!

Welcome to the second part of the tutorial series on Beginning Unity 3D for iOS!

In the first part of this series, you learned the basics of Unity by building a very simple project and deploying it to an iOS device. You’ll want to be sure to go through that part of the tutorial before moving on to this one.

Now in this second part, you’ll add functionality to enrich the project, including better player movement and better game scenery. You’ll also learn how to use Unity Remote for debugging.

Once again, game on!

Getting Started: A Change of Scenery

Open your Unity project from Part 1 of the tutorial. If you don’t have it already, here are the download links: Unity Project, Xcode Project.

You’ll make your changes for this part of the tutorial in a new scene, so you can easily have the old one for reference later if you’d like.

Select File\Save Scene as… and name your new scene Level_2. The new Level_2 scene should show up in the Project View.

All the assets you had in the previous scene, such as scripts, are now available for use in your new scene. You can make changes to the GameObjects present in the Level_2 scene without affecting the same objects in the Level_1 scene.

Gettin’ Jiggy With It

First you’ll enhance the player’s movement. Currently your Heroic Cube always moves forward and rotates right. That’s not very smooth or exciting.

A Character Controller is a Unity component that you can attach to a GameObject to help create more realistic movement. You manipulate the Character Controller through script functions.

For example, you can call a pre-defined SimpleMove function to move the character. The SimpleMove function takes in a Vector3 input representing the speed with which to perform the move. The character’s movements automatically account for gravity, allowing it to move up slopes and down stairs.

The character also slides around any obstacles it finds in its path without your having to write code to take care of that.

To see what this means, select the Cube GameObject (the player) in the Hierarchy View and select Component\Physics\Character Controller. You’ll see a dialog asking for confirmation to replace the Box Collider with a CharacterController.

Adding CharacterController component warning.

Click Replace.

You should see a new component in the Inspector for the Character Controller. You’re going to create a brand-new script to control the player’s movement, so you no longer need to attach the MoveSimple script to the Heroic Cube in this scene.

Character Controller added.

Click on the gear icon to the right of the MoveSimple script component in the Inspector and select Remove Component. The script should no longer show up in the Inspector.

Remove Move Simple script.

Create a new JavaScript asset by selecting Assets\Create\JavaScript and name it MoveAround. Double-click the new script to open it in the MonoDevelop Editor. Delete the stubbed out functions and add the following code:

var speed : float = 3.0;
var rotateSpeed : float = 3.0;

function Update () {
    var controller : CharacterController = GetComponent(CharacterController);

    // Rotate around y - axis
    transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
    
    // Move forward / backward
    var forward : Vector3 = transform.TransformDirection(Vector3.forward);
    var curSpeed : float = speed * Input.GetAxis ("Vertical");
    controller.SimpleMove(forward * curSpeed);
}

@script RequireComponent(CharacterController)

Save your changes.

The last line in the script specifies that this script can only be attached to a GameObject that has a Character Controller component.

The Update() function gets a handle to the Character Controller component. It then rotates the transform around the y-axis based on inputs representing left/right movements. By default, the left/right movement is controlled by both the left/right arrows and the A/D keys.

The Input.GetAxis() value ranges from -1 to +1. A negative value will result in an anti-clockwise rotation and a positive value a clockwise rotation around the y-axis.

The SimpleMove function moves the character forward or backward, depending on the input obtained from Input.GetAxis(“Vertical”). This input is triggered by either the up/down arrows or the W/S keys. As with the horizontal input, the Input.GetAxis() value ranges from -1 to +1, and negative and positive values will result in backward and forward movement, respectively. You use a speed multiplier to control the rotation angle or the move distance.

Attach the new script to your Heroic Cube. By now you should know the drill.

(Hint: If you don’t remember, it’s Components\Scripts\Move Around. :])

When you’re done, you should see the new Move Around script component in the player’s Inspector.

Move Around script added.

Why don’t you take your Heroic Cube for a spin using the Unity Editor? Click the Play button and experience your newfound freedom – oh, the joy of being able to rotate left and right, and move forwards and backwards!

Use the left/right/up/down arrow keys to move around, and also test out the W/A/S/D keys. The player movements are much smoother now, thanks to the Character Controller component and your scripting genius.

Give yourself a pat on the back before continuing. :]

Debugging with Unity Remote

You might have noticed in the last tutorial that testing your project on your iOS device was a bit cumbersome. The entire process of exporting to an Xcode project and building and running on your device can take quite a bit of time, which really adds up during development.

The good news is there’s a better way! The fastest way to debug your game on iOS is using an app called Unity Remote. This allows you to skip the steps of building your project for iOS, launching Xcode and deploying on your device.

With the Unity Remote iOS app, you can link your iOS device to the Unity Editor and control your Unity Editor from the iOS device. This allows you to quickly debug your game inside the Unity Editor.

Unity Remote is available for free via the App Store. Download and install the app on your device.

Note: To use Unity Remote, your iOS device and your computer should both be on the same Wi-Fi network. In addition, the Unity Editor window must be in the foreground in order for this to work. If you send the Unity Editor window to the background, you’ll get a “Waiting for game view. Press ‘Play'” message on the iOS app.

Note: To use Unity Remote, your iOS device and your computer should both be on the same Wi-Fi network. In addition, the Unity Editor window must be in the foreground in order for this to work. If you send the Unity Editor window to the background, you’ll get a “Waiting for game view. Press ‘Play'” message on the iOS app.

Click Play in the Unity Editor. On your iOS device, launch the Unity Remote app. Select your computer from the list that appears. If your computer is not listed, try entering your computer’s IP address in the Unity Remote’s IP setting.

Unity Remote

When Unity Remote has paired with your computer, your iOS device should be able to control the game running in the Unity Editor. The resolution of the display on the device won’t be ideal, but it’s a great way to quickly prototype your game. However, you’ll want to occasionally build the project for iOS and launch it from Xcode so you can properly test game physics under more realistic conditions.

Test the game on iOS. You’ll find that there’s no way to move the player on your device because your left/right/up/down inputs do not exist on iOS (however, you can still control it on your Mac). Next up, you’ll implement some joystick controls to fix this.

Christine Abernathy

Contributors

Christine Abernathy

Author

Over 300 content creators. Join our team.