- Home /
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;
}
}
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!
Answer by mcarriere · Sep 09, 2011 at 08:51 PM
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!
Answer by jahroy · Sep 09, 2011 at 11:02 PM
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 */
}
Your answer
Follow this Question
Related Questions
Android: After the third touch all touches get cancelled 1 Answer
Multitouch loses touch even when finger still on screen. 0 Answers
¿Unity messing up fingerIDs? [Android] 1 Answer
Unity Bug, Letting User To touch two UI Buttons as same time (simultaneously) 1 Answer
Move 2 objects at the same time 0 Answers