void Update() { RaycastHit hit; Touch touch; if (Input.touchCount < 1 || EventSystem.current.IsPointerOverGameObject((touch = Input.GetTouch(0)).fingerId)) { return; } if (touch.phase == TouchPhase.Stationary) { stationaryTime += Time.deltaTime; } else { stationaryTime = 0; } if (stationaryTime >= stationaryTimeLimit) { stationaryTime = 0; touch.phase = TouchPhase.Ended; } switch (touch.phase) { case TouchPhase.Began: Vector3 touchWorldPosition = mainCamera.ScreenToWorldPoint( new Vector3( touch.position.x, touch.position.y, mainCamera.nearClipPlane ) ); Vector3 touchDirection = touchWorldPosition - mainCamera.transform.position; if (Physics.Raycast(touchWorldPosition, touchDirection, out hit)) { if (hit.transform.gameObject == gameObject) { dragged = true; objectPosition = transform.position; objectPositionVector = objectPosition - mainCamera.transform.position; } } //can play pick up sound here soundManager.PlayPickUp(); break; case TouchPhase.Moved: case TouchPhase.Stationary: if (dragged) { DragObject(); TriggerDragEvent(); Handheld.Vibrate(); // Make the object follow the touch raycasting line touchWorldPosition = mainCamera.ScreenToWorldPoint( new Vector3( touch.position.x, touch.position.y, mainCamera.nearClipPlane ) ); touchDirection = touchWorldPosition - mainCamera.transform.position; float x = objectPositionVector.y / touchDirection.y; Vector3 movementVector = x * touchDirection - objectPositionVector; transform.position = objectPosition + movementVector; // Check if the target object intersects with the ray Debug.DrawRay(touchWorldPosition, touchDirection * 100f, Color.red, 0.5f); RaycastHit[] hits = Physics.RaycastAll(touchWorldPosition, touchDirection); if (hits != null) { foreach (var target in hits) { for (int i = 0; i < targets.Count; i++) { if (target.transform.gameObject == targets[i]) { dragged = false; DropObject(); TriggerDropEvent(resultEvents[i]); BillBoardImage imageObject = gameObject.GetComponent <BillBoardImage>(); if (imageObject != null) { imageObject.Trigger(); } } } } } } break; case TouchPhase.Ended: if (dragged) { dragged = false; DropObject(); } break; } }