Beginning Unity 3D for iOS: Part 1/3
A beginning Unity 3D tutorial that shows you how to make a simple game for the iPhone. By Christine Abernathy.
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
Beginning Unity 3D for iOS: Part 1/3
50 mins
- Getting Started
- Create the Project
- Getting to Know UI…
- Getting a Hand
- Move It Or Lose It
- You Spin Me Right Round
- It All Comes Down to Point of View
- Scale Tool
- Lonely No More: Adding GameObjects
- Get Moving: Event Functions
- The Right Touch
- Get It All On Camera
- Deploying on iOS
- One Finger to Rule Them All
- Where to Go From Here?
One Finger to Rule Them All
But wait, you’re not done yet!
If you play the game a bit longer, you’ll notice something odd. You can move the Cube by touching it with one finger. However, the Cube will also move when you use two fingers placed at an equal distance from it. What’s going on?
Recall this line of code from your MoveSimple script:
var ray : Ray = Camera.main.ScreenPointToRay
(Input.mousePosition);
This casts a ray from the camera to the touch point. The touch point is represented by Input.mousePosition. When running on an iOS device, Input.mousePosition is calculated as an average position of all current touches. Placing your fingers so that the average falls on the player makes the player move!
You should fix this to avoid confusing the user. You can use touch-related functions available in Unity to detect touch events and find the position of a touch more accurately.
Open your MoveSimple script. Add a flag at the top (where the other variables are) that will indicate that you can use touch input:
private var isTouchDevice : boolean = false;
You’re doing this so you can test on both iOS and the Unity Editor. If you’re only going to test on iOS, then you can bypass the touch check logic and simply use the touch-related functions.
Next add an Awake() function that will perform a runtime check to detect if the game is running in an iOS environment. The Awake() function is called only once, when the game is loaded:
function Awake() {
if (Application.platform == RuntimePlatform.IPhonePlayer)
isTouchDevice = true;
else
isTouchDevice = false;
}
Application.platform returns the platform on which the game is running. RuntimePlatform.IPhonePlayer indicates that the game is running on iOS.
Finally, modify your Update() function as follows to handle touch input:
function Update () {
var clickDetected : boolean;
var touchPosition : Vector3;
// Detect click and calculate touch position
if (isTouchDevice) {
clickDetected = (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began);
touchPosition = Input.GetTouch(0).position;
} else {
clickDetected = (Input.GetMouseButtonDown(0));
touchPosition = Input.mousePosition;
}
// Detect clicks
if (clickDetected) {
// Check if the GameObject is clicked by casting a
// Ray from the main camera to the touched position.
var ray : Ray = Camera.main.ScreenPointToRay
(touchPosition);
var hit : RaycastHit;
...
Save your changes.
You are using two new local variables, clickDetected and touchPosition, to detect clicks and save the click position. If running on iOS, you detect the click by checking that a touch has just begun. Then you calculate the click position based on the position of the first touch. If you’re not running on iOS, the logic is the same as before.
Before rebuilding the project, close the Xcode project. Now, rebuild in Unity from the Build Settings dialog. If presented with a warning about the Build folder already existing, select Replace.
Once the Xcode project is rebuilt, open it and run the game on your iOS device. Verify that you’re able to move the Heroic Cube and that the wrinkle allowing you to move it with two fingers is now gone.
Where to Go From Here?
Congratulations, you’ve just finished learning the basics of developing with Unity and deploying on iOS! Here are some downloads with all of the code from the project up until this point: Unity Project, Xcode Project.
In the next part of the tutorial, you’ll build on this rudimentary game by enhancing the Heroic Cube’s movement and improving the scenery. You’ll also do a bit of debugging!
In the meantime, if you have any questions or comments about what you’ve done so far, jump into the conversation in the forums!