Guest User

Untitled

a guest
Jul 22nd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Drag2 : MonoBehaviour
  5. {
  6. private float dist;
  7. private bool dragging = false;
  8. private Vector3 offset;
  9. private Transform toDrag;
  10.  
  11. void Update()
  12. {
  13. Vector3 v3;
  14.  
  15.  
  16. if (Input.touchCount < 1)
  17. {
  18. dragging = false;
  19. return;
  20. }
  21.  
  22. Touch[] touch = Input.touches;
  23. for (int i = 0; i < Input.touchCount; i++)
  24. {
  25. Vector3 pos = touch[i].position;
  26.  
  27. if (touch[i].phase == TouchPhase.Stationary)
  28. {
  29. RaycastHit hit;
  30. Ray ray = Camera.main.ScreenPointToRay(pos);
  31. if (Physics.Raycast(ray, out hit) && (hit.collider.CompareTag("Object")))
  32. {
  33. Debug.Log(Input.touchCount);
  34. toDrag = hit.transform;
  35. dist = hit.transform.position.z - Camera.main.transform.position.z;
  36. v3 = new Vector3(0, pos.y, dist);
  37. v3 = Camera.main.ScreenToWorldPoint(v3);
  38. offset = toDrag.position - v3;
  39. dragging = true;
  40. }
  41. }
  42. if (dragging && touch[i].phase == TouchPhase.Moved)
  43. {
  44. v3 = new Vector3(0, Input.mousePosition.y, dist);
  45. v3 = Camera.main.ScreenToWorldPoint(v3);
  46. toDrag.position = v3 + offset;
  47. }
  48. if (dragging && (touch[i].phase == TouchPhase.Ended || touch[i].phase == TouchPhase.Canceled))
  49. {
  50. dragging = false;
  51. }
  52. }
  53. }
  54. }
Add Comment
Please, Sign In to add comment