Advertisement
BlueBear

DragAndDropbehaviour.cs

Oct 28th, 2013
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.88 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class DragAndDropBehaviour : MonoBehaviour {
  5.  
  6.     private bool _mouseState;
  7.     private GameObject target;
  8.     private GameObject lastTarget;
  9.     private Vector3 lastPos;
  10.    
  11.     public Vector3 screenSpace;
  12.     public Vector3 offset;
  13.     public float level = 0;
  14.    
  15.     // Update is called once per frame
  16.     public void Update ()
  17.     {
  18.         #region MouseDown
  19.         if (Input.GetMouseButtonDown (1))
  20.         {
  21.             RaycastHit hitInfo;
  22.             target = GetClickedObject (out hitInfo);
  23.             Debug.Log (target);
  24.             if( target == null)
  25.             {
  26.                 return;//target = null;
  27.             }
  28.            
  29.             if (target != null && target.GetComponent<DraggableObject>().isDraggable == true)
  30.             {
  31.                 /*if(target.GetComponent<Pontoon>() != null)
  32.                 {
  33.                     target.GetComponent<Pontoon>().deadlock = false;
  34.                 }*/
  35.                 _mouseState = true;
  36.                 screenSpace = Camera.main.WorldToScreenPoint (target.transform.position);
  37.                 offset = (target.transform.position - Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenSpace.z))) * Time.deltaTime * 10;
  38.             }
  39.         }
  40.         #endregion
  41.        
  42.         #region MouseUp
  43.         if (Input.GetMouseButtonUp (1)) {
  44.             /*if(target.GetComponent<Pontoon>() != null)
  45.             {
  46.                 target.GetComponent<Pontoon>().deadlock = true;
  47.             }*/
  48.             _mouseState = false;
  49.             level = 0;
  50.         }
  51.         #endregion
  52.        
  53.         #region Movement And Boundary Setting
  54.         if (_mouseState) {
  55.             if(target.transform.GetComponent<PlaceableObject>())
  56.             {
  57.                 level = target.transform.GetComponent<PlaceableObject>().level;
  58.                 Debug.Log ("A New Level: " + level);
  59.             }
  60.             //keep track of the mouse position
  61.             var curScreenSpace = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
  62.  
  63.             //convert the screen mouse position to world point and adjust with offset
  64.             var curPosition = Camera.main.ScreenToWorldPoint (curScreenSpace) + offset;
  65.            
  66.             //setting boundaries
  67.             if(curPosition.x < 1)
  68.             {
  69.                 curPosition.x = 1;
  70.             }
  71.             if(curPosition.z < 1)
  72.             {
  73.                 curPosition.z = 1;
  74.             }
  75.             if(curPosition.x > 59)
  76.             {
  77.                 curPosition.x = 59;
  78.             }
  79.             if(curPosition.z > 59)
  80.             {
  81.                 curPosition.z = 59;
  82.             }
  83.            
  84.             //update the position of the object in the world
  85.             curScreenSpace = new Vector3(Mathf.Round(curPosition.x), 0F + this.level, Mathf.Round(curPosition.z));
  86.             target.transform.position = curScreenSpace;
  87.         }
  88.         #endregion
  89.     }
  90.  
  91.     public GameObject GetClickedObject(out RaycastHit hit)
  92.     {
  93.         GameObject target = null;
  94.         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
  95.         if (Physics.Raycast (ray.origin, ray.direction * 10, out hit)) {
  96.  
  97.             target = hit.collider.gameObject;
  98.         }
  99.         return target;
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement