Multitouch doesn't work
Hi. I am new in Unity. I would like to know how to make multi-touch because I have tested it and doesn't work, thanks. Code in C#: public class TouchScript : MonoBehaviour {
public float smoothTime = 0.1F;
private Vector3 velocity = Vector3.zero;
public GameObject wall1;
public GameObject wall2;
public Touch touch;
void Start()
{
Input.multiTouchEnabled = true;
}
void Update () {
Touch tap1 = Input.GetTouch(0);
Touch tap2 = Input.GetTouch(1);
if (Input.touchCount >= 2) {
if (tap1.position.x < Screen.width / 2) {
Vector3 targetPosition = new Vector3 (-350, tap1.position.y - 300, transform.position.z);
wall1.transform.localPosition = Vector3.SmoothDamp (wall1.transform.localPosition, targetPosition, ref velocity, smoothTime);
}
if (tap2.position.x > Screen.width / 2) {
Vector3 targetPosition2 = new Vector3 (350, tap2.position.y - 300, transform.position.z);
wall2.transform.localPosition = Vector3.SmoothDamp (wall2.transform.localPosition, targetPosition2, ref velocity, smoothTime);
}
}
if (Input.touchCount == 1) {
if (tap1.position.x < Screen.width / 2) {
Vector3 targetPosition = new Vector3 (-350, tap1.position.y - 300, transform.position.z);
wall1.transform.localPosition = Vector3.SmoothDamp (wall1.transform.localPosition, targetPosition, ref velocity, smoothTime);
}
if (tap1.position.x > Screen.width / 2) {
Vector3 targetPosition2 = new Vector3 (350, tap1.position.y - 300, transform.position.z);
wall2.transform.localPosition = Vector3.SmoothDamp (wall2.transform.localPosition, targetPosition2, ref velocity, smoothTime);
}
}
}
}
Answer by UnityCoach · Apr 30, 2017 at 06:43 PM
Hi, there are several things that may cause a problem.
When you use opposable conditions, make sure to use else if, like :
if (Input.touchCount >= 2)
{
if (tap1.position.x < Screen.width / 2)
{
}
if (tap2.position.x > Screen.width / 2)
{
}
}
else if (Input.touchCount == 1)
{
if (tap1.position.x < Screen.width / 2)
{
}
else if (tap1.position.x > Screen.width / 2)
{
}
}
Now, you also want to debug position. I'd suggest you first output debug logs with the value you get, then make sure they can be compared with the others. Touch positions are screen space in pixels, while Transform positions are in world space, so unless you're making your scene so that 1 unit = 1 pixel, it won't work. Good luck!
@UnityCoach If my game object is on the canvas it will detect it as screen space or world space? Thanks.