Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class DragObject : MonoBehaviour {
  6.  
  7. private Vector3 mOffset;
  8.  
  9. private float mZCoord;
  10.  
  11. //private Vector3 currentPosition;
  12.  
  13. void OnMouseDown()
  14. {
  15. mZCoord = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
  16. // Store offset = gameObject world pos - mouse world pos
  17. mOffset = gameObject.transform.position - GetMouseWorldPos();
  18. // currentPosition = gameObject.transform.position;
  19. }
  20.  
  21. void OnMouseDrag()
  22. {
  23. transform.position = GetMouseWorldPos() + mOffset;
  24. }
  25.  
  26. /*private void OnMouseUp()
  27. {
  28. transform.position = currentPosition;
  29. }*/
  30.  
  31. private Vector3 GetMouseWorldPos()
  32. {
  33. //pixel coordinates (x,y)
  34. Vector3 mousePoint = Input.mousePosition;
  35.  
  36. //z coordinate of game object on screen
  37. mousePoint.z = mZCoord;
  38.  
  39. return Camera.main.ScreenToWorldPoint(mousePoint);
  40.  
  41. }
  42.  
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement