- Home /
Multitouch iPhone error! Help
Hello,
I'm running into a simple error with Multitouch.
First:
Line referenced = var touch1 = Input.GetTouch(0);
function Update () {
//iPhone Touch code
var tapCount = Input.touchCount;
if(tapCount > 0) {
print(tapCount);
var touch1 = Input.GetTouch(0);
var touch2 = Input.GetTouch(1);
//---------------------------------Touch1--------------------------------------------------
if (touch1.phase == TouchPhase.Began){ // if screen touch...
// create a ray passing through the touch pos:
var iphoneray1: Ray = Camera.main.ScreenPointToRay(touch1.position);
var iphonehit1:RaycastHit;
if (Physics.Raycast(iphoneray1, iphonehit1)){ // do a raycast
var ObjHit1 = iphonehit1.collider.gameObject;
Vehicle.BroadcastMessage("iPhoneInputTap1", ObjHit1.name);
Debug.Log (ObjHit1.name + "Touch1");
}
} else if (touch1.phase == TouchPhase.Ended || touch1.phase == TouchPhase.Canceled) {
Vehicle.BroadcastMessage("iPhoneInputTap1", "TapEnded1");
}
//---------------------------------Touch2--------------------------------------------------
if (touch2.phase == TouchPhase.Began){ // if screen touch...
// create a ray passing through the touch pos:
var iphoneray2: Ray = Camera.main.ScreenPointToRay(touch2.position);
var iphonehit2:RaycastHit;
if (Physics.Raycast(iphoneray2, iphonehit2)){ // do a raycast
var ObjHit2 = iphonehit2.collider.gameObject;
Vehicle.BroadcastMessage("iPhoneInputTap2", ObjHit2.name);
Debug.Log (ObjHit2.name + "Touch2");
}
} else if (touch2.phase == TouchPhase.Ended || touch2.phase == TouchPhase.Canceled) {
Vehicle.BroadcastMessage("iPhoneInputTap2", "TapEnded2");
}
}
Thanks Daniel
You're checking if TouchCount is greater than 0. If it's 1, then your code access touch0 and touch1. You should only access touch1 is TouchCount is 2 or higher.
I never programmed any touchscreen stuff but i guess problem is here:
var touch2 = Input.GetTouch(1);
check if you actually have multiple touches.
@Lovrenc Yes "tapCount" when printed registers 1 or 2 or 3 touches based on the amount of fingers touching screen.
@GrahamDunnett That makes me have to put in TWO fingers for it to register a touch! What are you trying to say?
Listen to your sentence.
Yes "tapCount" when printed registers 1 or 2 or 3
But in this sentence you say:
var touch2 = Input.GetTouch(1); //Yo Yo unity, give me the second touch!!!! GIVE IT TO $$anonymous$$E!
But sometimes, when you only have 1 touch, poor unity cannot give you the second touch. He wants to but he cant. So he pees himself (throws this exception).
Answer by Lovrenc · Aug 04, 2013 at 08:59 PM
function Update () {
//iPhone Touch code
var tapCount = Input.touchCount;
if(tapCount > 0) {
print(tapCount);
var touch1 = Input.GetTouch(0);
//---------------------------------Touch1--------------------------------------------------
if (touch1.phase == TouchPhase.Began){ // if screen touch...
// create a ray passing through the touch pos:
var iphoneray1: Ray = Camera.main.ScreenPointToRay(touch1.position);
var iphonehit1:RaycastHit;
if (Physics.Raycast(iphoneray1, iphonehit1)){ // do a raycast
var ObjHit1 = iphonehit1.collider.gameObject;
Vehicle.BroadcastMessage("iPhoneInputTap1", ObjHit1.name);
Debug.Log (ObjHit1.name + "Touch1");
}
} else if (touch1.phase == TouchPhase.Ended || touch1.phase == TouchPhase.Canceled) {
Vehicle.BroadcastMessage("iPhoneInputTap1", "TapEnded1");
}
//---------------------------------Touch2--------------------------------------------------
if (tapCount > 1 && touch2.phase == TouchPhase.Began){ // if screen touch...
// create a ray passing through the touch pos:
var touch2 = Input.GetTouch(1);
var iphoneray2: Ray = Camera.main.ScreenPointToRay(touch2.position);
var iphonehit2:RaycastHit;
if (Physics.Raycast(iphoneray2, iphonehit2)){ // do a raycast
var ObjHit2 = iphonehit2.collider.gameObject;
Vehicle.BroadcastMessage("iPhoneInputTap2", ObjHit2.name);
Debug.Log (ObjHit2.name + "Touch2");
}
} else if (touch2.phase == TouchPhase.Ended || touch2.phase == TouchPhase.Canceled) {
Vehicle.BroadcastMessage("iPhoneInputTap2", "TapEnded2");
}
}
Okay! Yes! I noticed that and made this :P will this do the same thing?
//iPhone Touch code
var tapCount = Input.touchCount;
if (tapCount > 1) {
switch (tapCount) {
case 1 :
var touch1 = Input.GetTouch(0);
print(tapCount);
break;
case 2 :
touch1 = Input.GetTouch(0);
var touch2 = Input.GetTouch(1);
print(tapCount);
break;
case 3 :
touch1 = Input.GetTouch(0);
touch2 = Input.GetTouch(1);
var touch3 = Input.GetTouch(2);
print(tapCount);
break;
case 4 :
touch1 = Input.GetTouch(0);
touch2 = Input.GetTouch(1);
touch3 = Input.GetTouch(2);
var touch4 = Input.GetTouch(3);
print(tapCount);
break;
case 5 :
touch1 = Input.GetTouch(0);
touch2 = Input.GetTouch(1);
touch3 = Input.GetTouch(2);
touch4 = Input.GetTouch(3);
var touch5 = Input.GetTouch(4);
print(tapCount);
break;
}
}
@Lovrenc I just realized that if i did it this way i would have to put both fingers down at the same time, this would NOT work your example will! ThankYou!