Problem with tracking multiple object with multitouch
So I have been working on this problem for days now, I just can't get it to work. I want to Instantiate three different object on three different touches and make them follow finger which they were instantiated with. I managed to do that, every object instantiates and follow specific finger but problems start when i start removing fingers from my screen. I have to remove them by opposite order in which i placed my fingers in first place. If i remove say first finger (Id=0), second finger takes id from first one and third from second and then objects kinda mess up eachother. I am new to scripting in unity so i need some help, I feel like I am close to solution but can't quite get it to work. Here's my code.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
using System.Collections.Generic;
public class oneforall : MonoBehaviour
{
public GameObject itemToSpawn;
private GameObject itemToSpawnInstance;
public GameObject itemToSpawn1;
private GameObject itemToSpawnInstance1;
public GameObject itemToSpawn2;
private GameObject itemToSpawnInstance2;
void Update() {
for (int i = 0; i < 10; i++) {
var MyTouch = Input.touches[i].fingerId;
if (MyTouch == 0)
{
Touch touch = Input.touches[0];
switch (touch.phase)
{
case TouchPhase.Began:
var touchPos = Input.touches[0].position;
var createPos = Camera.main.ScreenToWorldPoint(new Vector3(touchPos.x, touchPos.y, 1));
itemToSpawnInstance = (GameObject)Instantiate(itemToSpawn, createPos, Quaternion.identity);
break;
case TouchPhase.Moved:
Vector3 newPos = Camera.main.ScreenToWorldPoint(Input.touches[0].position);
itemToSpawnInstance.transform.position = new Vector3(newPos.x, newPos.y, itemToSpawnInstance.transform.position.z);
break;
case TouchPhase.Ended:
Destroy(itemToSpawnInstance);
break;
}
}
if (MyTouch == 1)
{
Touch touch = Input.touches[1];
switch (touch.phase)
{
case TouchPhase.Began:
var touchPos1 = Input.touches[1].position;
var createPos1 = Camera.main.ScreenToWorldPoint(new Vector3(touchPos1.x, touchPos1.y, 1));
itemToSpawnInstance1 = (GameObject)Instantiate(itemToSpawn1, createPos1, Quaternion.identity);
break;
case TouchPhase.Moved:
Vector3 newPos1 = Camera.main.ScreenToWorldPoint(Input.touches[1].position);
itemToSpawnInstance1.transform.position = new Vector3(newPos1.x, newPos1.y, itemToSpawnInstance1.transform.position.z);
break;
case TouchPhase.Ended:
Destroy(itemToSpawnInstance1);
break;
}
}
if (MyTouch == 2)
{
Touch touch = Input.touches[2];
switch (touch.phase)
{
case TouchPhase.Began:
var touchPos2 = Input.touches[2].position;
var createPos2 = Camera.main.ScreenToWorldPoint(new Vector3(touchPos2.x, touchPos2.y, 1));
itemToSpawnInstance2 = (GameObject)Instantiate(itemToSpawn2, createPos2, Quaternion.identity);
break;
case TouchPhase.Moved:
Vector3 newPos2 = Camera.main.ScreenToWorldPoint(Input.touches[2].position);
itemToSpawnInstance2.transform.position = new Vector3(newPos2.x, newPos2.y, itemToSpawnInstance2.transform.position.z);
break;
case TouchPhase.Ended:
Destroy(itemToSpawnInstance2);
break;
}
}
}
}
}
Your answer
Follow this Question
Related Questions
2D Platformer Projectile wont face mouse position? 0 Answers
Why isn't my BoxCollider2D doing anything? 2 Answers
Virtual controller and fire bullet shot in 2D 0 Answers
how to make my 2d top down character face the direction of the cursor 1 Answer
Why is my script firing 2 bullets on the client side? 0 Answers