I’m working with Unity/Android for the first time. I have several objects on the screen all with the same script on each. I want to be able to independently drag each object around. This works fine when I test it in the game tab, but when I actually build on Android it doesn’t work correctly. Instead, after touching an object once, anytime I touch the screen anywhere else it instantly jumps there. Not just when I drag. Could anyone tell me what i’m doing wrong (and maybe a few other tips? I know the way i’m doing this isn’t the best…)
using UnityEngine;
using System.Collections;
public class handleTouch : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
public bool dragging = false;
void Update()
{
var aTouch = Input.GetMouseButton(0);
var touchPos3D = Input.mousePosition;
var firstTouch = false;
if (Application.platform != RuntimePlatform.IPhonePlayer && Application.platform != RuntimePlatform.Android)
{
if (Input.GetMouseButtonDown(0)){
firstTouch = true;
}
}
else
{
aTouch = (Input.touchCount > 0);
touchPos3D = Input.touches[0].position;
if (Input.touches[0].phase != TouchPhase.Moved){
firstTouch = true;
}
}
if (aTouch)
{
Vector3 wp = Camera.main.ScreenToWorldPoint(touchPos3D);
Vector2 touchPos2D = new Vector2(wp.x, wp.y);
if (this.collider2D == Physics2D.OverlapPoint(touchPos2D) && firstTouch == true)
{
dragging = true;
}
else
{
if (dragging == true)
{
this.transform.position = touchPos2D;
}
}
}
else
{
dragging = false;
}
}
}