Unity3d for iOS Beginner Tutorial: Part 1/3 (bottom)

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/alongu3d/archive/2013/06/01/3111735.html

One finger to rule them.

But wait, you're not completely finished!

If you play the game for a while, you'll notice something strange. You can move a block with one finger, but when you leave it with two fingers the same distance block can be moved. What's going on here?

Recall this line from your movesimple script:

var Ray:ray = Camera.main.ScreenPointToRay                     (input.mouseposition);

It creates a ray from the camera to the touch point. Touch points are represented by input.mouseposition. When you run on an iOS device, input.mouseposition is calculated as the average position of all current touch points. Place your finger, and if the average falls on the character, it will move the character!

You should fix this to avoid disturbing users. You can use the touch-related functions that you can use in Unity to monitor touch events and find a more precise touch location.

Open your movesimple script. Add a flag at the top of the file (where other variables are located) to indicate that you can use touch input:

private var Istouchdevice:boolean = false;

After you do this, you can test run on iOS and the Unity editor. If you only want to test on iOS, then you can skip the logic of the touch check and then simply use the touch-related functions.

Next, add a awake () function to complete a run-time check to see if the game is running in an iOS environment. This awake () function is only called 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. The Runtimeplatform.iphoneplayer indicates that the game is running on iOS.

Finally, the touch input is handled according to the following modification of your update () function:

function Update () {    var Clickdetected:boolean;    var Touchposition:vector3;    Detect click and calculate touch Position    if (istouchdevice) {        clickdetected = (Input.touchcount > 0 &&A mp 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 MA In camera to the touched position.        var Ray:ray = Camera.main.ScreenPointToRay                             (touchposition);        var Hit:raycasthit;         ...

Save your changes.

You are using two new variables, clickdetected and touchposition, to monitor the location of clicks and save clicks. If you run on iOS, you can judge the click by checking that the touch is in the start state. Then calculate the location of the click based on the first touch. If you are not running on iOS, the logic is the same as before.

Close the Xcode project before rebuilding the project. Now re-build in the Build Settings dialog in unity. Select Replace if a warning appears with a folder that already exists for the build.

When the Xcode project is built, open it and run it on your iOS device. Check that you can move the heroic little box and let you move it with two fingers the error has disappeared.

Where are you headed?

Congratulations you have completed learning the basics with unity development and how to deploy to iOS! Here are some of the code downloads from the beginning of the project to the present: Unity Project, Xcode project.

The next part of this tutorial, you will build this very simple game by enhancing the movement of heroic small squares and improving the scene. You will also do some debugging work!

In the meantime, if you have any questions or suggestions about your current location, please jump to the relevant session in the Forum!

Unity3d for iOS Beginner Tutorial: Part 1/3 (bottom)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.