How to implement multitouch in Unity3d on a mobile device?

I use OnMouseDown()to handle clicks, but it's not possible to implement multi-touch.

The program includes objects that increase when pressed and then reduced. If there is one touch, everything works fine. But when you try to click several objects at the same time, they do not work.

I am trying to solve a problem, but it does not work, objects do not scale, and multi-touch does not work.

the code:

using UnityEngine;
using System.Collections;

public class OnTouch : MonoBehaviour {
public AudioClip crash1;
public AudioClip hat_closed;
public AudioClip hat_open;
public bool c;
public bool c1;
public bool c2;

void OnMouseDown(){

if (this.name == "clash") {
  GetComponent<AudioSource>().PlayOneShot(hat_open);
  c=true;
}
if (this.name == "clash 1") {
  GetComponent<AudioSource>().PlayOneShot(hat_closed);
  c1=true;
}

if (this.name == "clash 2") {
  GetComponent<AudioSource> ().PlayOneShot (crash1);
  c2=true;
}           

transform.localScale += new Vector3(0.05f, 0.05f, 0);

 }

void Update(){          
if (c) {transform.localScale = Vector3.Lerp (this.transform.localScale, new Vector3 (0.2f, 0.2f, 0), Time.deltaTime*10f);}
if (c1) {transform.localScale = Vector3.Lerp (this.transform.localScale, new Vector3 (0.2f, 0.2f, 0), Time.deltaTime*10f);}
if (c2) {transform.localScale = Vector3.Lerp (this.transform.localScale, new Vector3 (0.25f, 0.25f, 0), Time.deltaTime*10f);}
 }
}
+4
source share
1 answer

You really should not use mouse events for touch devices. Unity gives you the convenience of displaying the first touch of a mouse event, but thatโ€™s it.

Unity :
Touch
Input.GetTouch

(, , ..), :

.GetTouch

public class TouchTest : MonoBehaviour 
{
    void Update () 
    {
        Touch myTouch = Input.GetTouch(0);

        Touch[] myTouches = Input.touches;
        for(int i = 0; i < Input.touchCount; i++)
        {
            //Do something with the touches
        }
    }
}
+2

All Articles