- Home /
Help with c# code - Multitouch
hello everyone i have this code working :
using UnityEngine;
using System.Collections;
public class DragObject : MonoBehaviour {
public GameObject Finger1;
void Update(){
foreach (Touch touch in Input.touches) {
if (touch.phase == TouchPhase.Began){
var ray = Camera.main.ScreenPointToRay(Input.touches [0].position);
var hit = Physics2D.GetRayIntersection(ray);
if (hit.collider.tag == "fingers") {
Debug.Log("selezionato : " + hit.collider);
Finger1 = hit.collider.gameObject;
}
}
else if (touch.phase == TouchPhase.Moved && Finger1) {
var ray = Camera.main.ScreenPointToRay(Input.touches [0].position);
var hit = Physics2D.GetRayIntersection(ray);
Debug.Log(hit.point);
Finger1.transform.position = new Vector3(hit.point.x, hit.point.y, 1);
}
else if (touch.phase == TouchPhase.Ended && Finger1) {
Finger1 = null;
}
}
}
}
how can i do this working with two fingers? sorry for bad english, i'm italian ;)
Input.touches [0]
Your code only includes that for the first finger.
Input.touches[1] ??
The trouble is you iterate the whole touch routine with
foreach (Touch touch in Input.touches)
but still only ever specify
Input.touches [0]
http://answers.unity3d.com/questions/167750/how-does-multi-touch-work.html
Replace every Input.touches[0] with touch (since you already have that reference in your foreach)
like this? using UnityEngine; using System.Collections;
public class DragObject : $$anonymous$$onoBehaviour {
public GameObject Finger1,Finger2;
void Update(){
foreach (Touch touch in Input.touches) {
if (touch.phase == TouchPhase.Began){
var ray = Camera.main.ScreenPointToRay(Input.touches [0].position);
var hit = Physics2D.GetRayIntersection(ray);
if (hit.collider.tag == "fingers") {
Debug.Log("selezionato : " + hit.collider);
Finger1 = hit.collider.gameObject;
}
ray = Camera.main.ScreenPointToRay(Input.touches [1].position);
hit = Physics2D.GetRayIntersection(ray);
if (hit.collider.tag == "fingers") {
Debug.Log("selezionato : " + hit.collider);
Finger2 = hit.collider.gameObject;
}
}
else if (touch.phase == TouchPhase.$$anonymous$$oved) {
var ray = Camera.main.ScreenPointToRay(Input.touches [0].position);
var hit = Physics2D.GetRayIntersection(ray);
Finger1.transform.position = new Vector3(hit.point.x, hit.point.y, 1);
ray = Camera.main.ScreenPointToRay(Input.touches [1].position);
hit = Physics2D.GetRayIntersection(ray);
Debug.Log(hit.point);
Finger2.transform.position = new Vector3(hit.point.x, hit.point.y, 1);
}
}
}
}
now the game crash...
Answer by aqib · Feb 15, 2015 at 04:08 PM
You can try this in LateUpdate() Function :
void LateUpdate(){
if (Input.touchCount >= 2) {
Vector3 finger1 = Input.GetTouch(0);
Vector3 finger2 = Input.GetTouch(1);
// now you can do the rest of the code for each touch
}
}
Hope it works.... :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613183351im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Input.GetTouch(index) is NOT the fingerID 1 Answer
IOS GUI slider 1 Answer
Mobile game touch problem (C#) 2 Answers
Touch not working on guiTexture. 1 Answer
Prime 31 Touch Kit detecting if touch hit gui element 1 Answer