Hi, I’m hoping someone can help shed some light on this problem I’ve been trying to solve over the past couple of days with no luck.
So the idea is that I’m trying to detect multiple touches on an Android device with the purpose being for each touch to move its’ own gameobject. The problem I am encountering is that when I have 2 touches, and the 1st touch ends/cancels, the 2nd touch also seems to stop with it, what’s weirder is that as soon as I initiate another (originally 1st) touch, the 2nd touch that did not end will suddenly continue to move the gameobject just fine.
This is a rough/simplified representation of the code I have:
public class TrackerHandlerScript : MonoBehaviour {
public static GameObject[] trackerArray = new GameObject[2];
private int _tapCount;
private Object trackerPrefab;
// Use this for initialization
void Start () {
trackerPrefab = Resources.Load("Prefabs/prefab_tracker");
}
// Update is called once per frame
void Update () {
_tapCount = Input.touchCount;
if(_tapCount > 0 ){
for(int i = 0; i < _tapCount; i++){
if(Input.GetTouch(i).phase == TouchPhase.Began){
createTracker(i, Input.GetTouch(i).fingerId);
}
}
}
}
void createTracker(int idx, int id = 0){
if(trackerArray[idx] != null){
Destroy(trackerArray[idx] .gameObject);
}
trackerArray[idx] = Instantiate(trackerPrefab, Vector3.zero, Quaternion.identity) as GameObject;
trackerArray[idx] .GetComponent<TrackerScript>().touch = id;
}
}
The idea here is that I store my 2 gameobjects in an array and only instantiate a tracker into an empty index. This seems to be working as intended.
This is the logic in the gameobject’s script that handles the touch:
public class TrackerScript : MonoBehaviour {
public int idx;
public int touch;
Vector3 pos;
void Update () {
for(int i = 0; i < Input.touchCount; i++){
if(Input.GetTouch(i).fingerId == touch){
if(Input.GetTouch(i).phase == TouchPhase.Ended || Input.GetTouch(i).phase == TouchPhase.Canceled){
Destroy(gameObject);
}
}
}
for(int i = 0; i < Input.touchCount; i++){
if(Input.GetTouch(i).fingerId == touch){
pos = Input.GetTouch(i).position;
}
}
_transform.position = Camera.main.ScreenToWorldPoint(pos)
}
}
So no syntax errors or anything (in case I made a typo or left anything out). The idea here is that I use the passed fingerId from the handler to identify the touch and only move the gameobject if it’s a match. Releasing the touch will destroy the gameobject thus nullifying the index for the tracker array.
Once again, if anyone has any idea what might be wrong with this code/logic, I’m all ears.
P.S. I am on Unity 4.6.3f for full disclosure.
edit: updated some of the code.