x


question about touches for iOS/Android

i recently started porting our app to android tablet and i have some problems with touches.

basically we have camera that rotates around object, left click for selecting objects, and multitouch for zooming.

it is easy to identify one from two touches, but how to register that i, for example, started rotating camera and i do not want any selection? i hope i am clear enough.

currently now if i am rotating camera, if any object is below my finger it gets selected, this is not what i want. it was easier to solve this problem with mouse clicks.

any help appreciated.

[EDIT] here is the code i have so far:

    //COUNT HOW MANY TOUCHES WE HAVE
    var count = Input.touchCount;

    //IF WE HAVE AT LEAST ONE
    if (count == 1)
    {
            //GET TOUCH STRUCT FOR EACH
            var touch : Touch = Input.GetTouch(0);

            //DETERMINE THE PHASE OF EACH TOUCH

                    //WHEN TOUCH BEGAN
            if (touch.phase == TouchPhase.Began)
            {

            }

            //WHEN TOUCH IS MOVED
            if(touch.phase == TouchPhase.Moved){                

                   // Get movement of the finger since last frame
                   touchDeltaPosition = Input.GetTouch(0).deltaPosition;

                    x += touchDeltaPosition.x * xSpeed*0.01; 
                    y -= touchDeltaPosition.y * ySpeed*0.01; 


            }


    //FOR ZOOMING IF WE HAVE MULTITOUCH
    }else if(count==2){

        //IF WE HAVE MOVEMENT OF BOTH TOUCHES PERFORM ZOOM
        if(Input.GetTouch(0).phase==TouchPhase.Moved && Input.GetTouch(1).phase==TouchPhase.Moved){

            distance += Input.GetTouch(0).deltaPosition.y*0.1;

        }
    }
more ▼

asked Sep 01, 2011 at 02:26 PM

pretender gravatar image

pretender
632 354 257 269

I can help if you can post your code

Sep 01, 2011 at 03:33 PM DaveA

ok, i added the code i have so far. it is not much since i do not know how to register if it is tap or drag... thank you for looking into this!

Sep 01, 2011 at 03:40 PM pretender
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Here, I wrote this in C#, so you'll probably have to convert it to JS if you want to keep your code the same:

using UnityEngine;
using System.Collections;

public enum MoveMode { None, Rotate, Zoom };

public class ZoomPan : MonoBehaviour {

    private MoveMode currentMode;
    private float lastTouchTime;
    private const float touchTimeBuffer = 1f;

    void Start ()
    {
        currentMode = MoveMode.None;
        lastTouchTime = 0;
    }

    void Update ()
    {
        Touch touch0, touch1;
        int touchCount = Input.touchCount;

        if (currentMode == MoveMode.None)
        {
            if (touchCount == 1)
            {
                touch0 = Input.GetTouch(0);

                if (touch0.phase == TouchPhase.Began)
                {
                    lastTouchTime = Time.time;
                }
                else if (Time.time - lastTouchTime > touchTimeBuffer)
                {
                    currentMode = MoveMode.Rotate;
                }
            }
            else if (touchCount == 2)
            {
                touch0 = Input.GetTouch(0);
                touch1 = Input.GetTouch(1);

                currentMode = MoveMode.Zoom;
            }
        }
        else if (currentMode == MoveMode.Rotate)
        {
            if (touchCount == 0)
            {
                currentMode = MoveMode.None;
            }
            else
            {
                touch0 = Input.GetTouch(0);

                Vector2 deltaPosition = touch0.deltaPosition;

                // do whatever you want here with your delta position
            }
        }
        else if (currentMode == MoveMode.Zoom)
        {
            if (touchCount == 0)
            {
                currentMode = MoveMode.None;
            }
            else if (touchCount >= 2)
            {
                touch0 = Input.GetTouch(0);
                touch1 = Input.GetTouch(1);

                float zoomDistance = Vector2.Distance(touch0.deltaPosition, touch1.deltaPosition) * Time.deltaTime;

                // do whatever you want with zoom distance.
            }
        }
    }
}

A few notes to what I did here: I created an enumeration that defines what "Mode" the game is in. This way, we kinda lock ourselves into the state that the user starts in, and stay there until they take their fingers off of the screen. (Which seems to be what you want)

What this does is it originally sets the game in "MoveMode.None". If you put down two fingers within 1 second, you're set to "Zoom" until you let go of the screen. If you put down one finger and hold it for a second, you're in "Rotate" until you let go of the screen. You can increase or decrease this buffer by changing the constant "touchTimeBuffer" to whatever value in seconds that you want.

While this is what you asked for, you might want to rethink the usage, if you look at other apps (look at google maps, for instance) they will automatically switch from pan to zooming, no matter if you "meant to" or not. I also disregarded if both touchphases are "Moved", because some people pinch by putting one finger down, and moving only one of them.

Good luck, hope this helps!

more ▼

answered Sep 09, 2011 at 08:51 PM

mcarriere gravatar image

mcarriere
121 3 5

(comments are locked)
10|3000 characters needed characters left

Maybe I'm missing something, but it sounds like you simply want to ignore your "select object" code whenever any of the touches are moving.

Here is one way to do that:

function tabletSelectObject ()
{
    /* if any fingers are moving, get outta here */

    for ( var thisTouch : Touch in Input.touches ) {

        if ( thisTouch.phase == TouchPhase.Moved ) {
            return;
        }
    }

    /* code to select objects here */
}
more ▼

answered Sep 09, 2011 at 11:02 PM

jahroy gravatar image

jahroy
3.7k 36 43 73

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4690
x1151
x112

asked: Sep 01, 2011 at 02:26 PM

Seen: 3596 times

Last Updated: Sep 09, 2011 at 11:02 PM