Detect screen tap vs. drag

I have tried to edit the drag iphone script to get it so that when I touch the object, the player picks it up, and when it touches again, the object drops. This works, but I do not want the cube to be picked up or dropped if the player drags his finger before or after touching the object. I just cannot make it work. The script i have seems to only reference one or the other, either touchphase.began or touchphase.ended, but it will not register both. How can I solve this issue? The script is below.

using UnityEngine;
using System.Collections;

public class DragIphone : MonoBehaviour {

    public float spring = 50.0f;
    public float damper = 5.0f;
    public float drag = 10.0f;
    public float angularDrag = 5.0f;
    public float distance = 0.2f;
    public bool attachToCenterOfMass = false;
    public Transform BoxPosition;
    private bool touched = false;
    private SpringJoint springJoint;
    private Touch touch;
    private Touch touch1;
    private Vector2 tposition; 
    void Update (){
        // Make sure the user touched the screen

        if (Input.touchCount==0){
        Camera mainCamera = FindCamera();
        // We need to actually hit an object
        RaycastHit hit = new RaycastHit();
        touch = Input.GetTouch(0);
      //  touch1 = Input.GetTouch(1);
        if(touch.phase == TouchPhase.Began){
        	                  tposition = touch.position;
            if (!Physics.Raycast(mainCamera.ScreenPointToRay(touch.position),  out hit, 100.0f))
            // We need to hit a rigidbody that is not kinematic
            if (!hit.rigidbody || hit.rigidbody.isKinematic)
            if (!springJoint){
                GameObject go = new GameObject("Rigidbody dragger");
                //Rigidbody is automatically added by SpringJoint
                springJoint = (SpringJoint)go.AddComponent ("SpringJoint");
                go.rigidbody.isKinematic = true;
            springJoint.transform.position = hit.rigidbody.transform.position;
            if (attachToCenterOfMass){
                Vector3 anchor =  hit.rigidbody.transform.position;
              //  transform.TransformDirection(hit.rigidbody.centerOfMass)
                anchor = springJoint.transform.InverseTransformPoint(anchor);
                springJoint.anchor = anchor;
                springJoint.anchor =;
            springJoint.spring = spring;
            springJoint.damper = damper;
            springJoint.maxDistance = distance;
            springJoint.connectedBody = hit.rigidbody;
            StartCoroutine("DragObject", hit.distance);
    private int count;
    void start(){
        count = 0;
    IEnumerator DragObject (float distance){
    	   if (touch.phase == TouchPhase.Began && touched == true){
          	          	touched = false;}     

        else if(touch.phase == TouchPhase.Began && touched == false){
        	touched = true;
      // else if(touch.phase == iPhoneTouchPhase.Began && touched == true){
       	//		touched = false;
          //    }
            float oldDrag = springJoint.connectedBody.drag;
            float oldAngularDrag = springJoint.connectedBody.angularDrag;
            springJoint.connectedBody.drag = drag;
            springJoint.connectedBody.angularDrag = angularDrag;
            Camera mainCamera = FindCamera();
            while (touched == true){
              //     touch.phase != iPhoneTouchPhase.Ended &&
                //   touch.phase != iPhoneTouchPhase.Canceled            
        			Ray ray = mainCamera.ScreenPointToRay(touch.position);
                springJoint.transform.position = new Vector3(BoxPosition.transform.position.x, BoxPosition.transform.position.y, BoxPosition.transform.position.z);
                yield return 0;
            if (springJoint.connectedBody)
                springJoint.connectedBody.drag = 0;
                springJoint.connectedBody.angularDrag = 0;
                springJoint.connectedBody = null;
    Camera FindCamera (){
        if (camera)
            return camera;
            return Camera.main;

var firstFinger = Input.GetTouch(0);

  if ( firstFinger.phase == TouchPhase.Moved ) {

    Debug.Log("I am dragging my finger...");


I’ve made few tutorials on dragging and rotating an object by touch(Unity iOS).

These are the Youtube videos:



Get the source code from my website:

Hope it helps.