Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class DragAndDropBehaviour : MonoBehaviour {
- private bool _mouseState;
- private GameObject target;
- private GameObject lastTarget;
- private Vector3 lastPos;
- public Vector3 screenSpace;
- public Vector3 offset;
- public float level = 0;
- // Update is called once per frame
- public void Update ()
- {
- #region MouseDown
- if (Input.GetMouseButtonDown (1))
- {
- RaycastHit hitInfo;
- target = GetClickedObject (out hitInfo);
- Debug.Log (target);
- if( target == null)
- {
- return;//target = null;
- }
- if (target != null && target.GetComponent<DraggableObject>().isDraggable == true)
- {
- /*if(target.GetComponent<Pontoon>() != null)
- {
- target.GetComponent<Pontoon>().deadlock = false;
- }*/
- _mouseState = true;
- screenSpace = Camera.main.WorldToScreenPoint (target.transform.position);
- offset = (target.transform.position - Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenSpace.z))) * Time.deltaTime * 10;
- }
- }
- #endregion
- #region MouseUp
- if (Input.GetMouseButtonUp (1)) {
- /*if(target.GetComponent<Pontoon>() != null)
- {
- target.GetComponent<Pontoon>().deadlock = true;
- }*/
- _mouseState = false;
- level = 0;
- }
- #endregion
- #region Movement And Boundary Setting
- if (_mouseState) {
- if(target.transform.GetComponent<PlaceableObject>())
- {
- level = target.transform.GetComponent<PlaceableObject>().level;
- Debug.Log ("A New Level: " + level);
- }
- //keep track of the mouse position
- var curScreenSpace = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
- //convert the screen mouse position to world point and adjust with offset
- var curPosition = Camera.main.ScreenToWorldPoint (curScreenSpace) + offset;
- //setting boundaries
- if(curPosition.x < 1)
- {
- curPosition.x = 1;
- }
- if(curPosition.z < 1)
- {
- curPosition.z = 1;
- }
- if(curPosition.x > 59)
- {
- curPosition.x = 59;
- }
- if(curPosition.z > 59)
- {
- curPosition.z = 59;
- }
- //update the position of the object in the world
- curScreenSpace = new Vector3(Mathf.Round(curPosition.x), 0F + this.level, Mathf.Round(curPosition.z));
- target.transform.position = curScreenSpace;
- }
- #endregion
- }
- public GameObject GetClickedObject(out RaycastHit hit)
- {
- GameObject target = null;
- Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
- if (Physics.Raycast (ray.origin, ray.direction * 10, out hit)) {
- target = hit.collider.gameObject;
- }
- return target;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement