// Start is called before the first frame update void Start() { Transform[] tr = GetComponentInParent <Transform>().GetComponentsInChildren <Transform>(); foreach (Transform t in tr) { if (t.tag == DRAGGABLE_TAG) { UIPanel = t.gameObject; break; } } if (UIPanel == null) { Debug.Log("No panel found!!!"); } if (UIPanel != null) //Debug.Log(UIPanel.name); { dragableUI = UIPanel.GetComponent <DragableUI>(); } if (dragableUI == null) { Debug.Log("No dragable script found!!!"); } }
void Update() { if (readyToClick) { if (Input.mousePosition != oldMousePosition) { Debug.Log("MOUSE MOVED"); readyToClick = false; mouseMoved = true; gamePieceDragging = true; } } if (Input.GetMouseButtonDown(0)) { if (clickedObject != null) { Debug.Log(clickedObject.name); //if (clickedObject.tag == DRAGGABLE_TAG) //{ // objectToDrag = GetDraggableTransformUnderMouse(); // //Debug.Log(objectToDrag.name); // if (objectToDrag != null) // { // dragableUI = objectToDrag.GetComponent<DragableUI>(); // if (!dragableUI.getGamePiece().isPinned()) // { // offset = objectToDrag.position - Input.mousePosition; // //Debug.Log(objectToDrag.name); // dragging = true; // objectToDrag.SetAsLastSibling(); // originalPosition = objectToDrag.position; // } // } //} } else { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; objectToDrag = GetDraggableTransformUnderMouse(); if (objectToDrag != null) { dragableUI = objectToDrag.GetComponent <DragableUI>(); if (!dragableUI.getGamePiece().isPinned()) { offset = objectToDrag.position - Input.mousePosition; //Debug.Log(objectToDrag.name); dragging = true; objectToDrag.SetAsLastSibling(); originalPosition = objectToDrag.position; } } else if (Physics.Raycast(ray, out hit)) { if (hit.transform != null) { clickedObject = hit.transform.gameObject; Debug.Log("clicked " + clickedObject.name); if (clickedObject.tag == MODEL_TAG) { GamePieceScript = clickedObject.GetComponentInParent <GamePiece>(); Debug.Log(clickedObject.GetComponentInParent <Transform>().name); //Debug.Log(GamePieceScript.transform.gameObject.name); if (GamePieceScript != null) { Debug.Log("ReadyToClick"); readyToClick = true; originalPosition = clickedObject.transform.position; offset = originalPosition - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane)); } } if (clickedObject.tag == DRAGGABLE_TAG) { objectToDrag = GetDraggableTransformUnderMouse(); Debug.Log(objectToDrag.name); if (objectToDrag != null) { dragableUI = objectToDrag.GetComponent <DragableUI>(); if (!dragableUI.getGamePiece().isPinned()) { offset = objectToDrag.position - Input.mousePosition; //Debug.Log(objectToDrag.name); dragging = true; objectToDrag.SetAsLastSibling(); originalPosition = objectToDrag.position; } } } } } } } if (Input.GetMouseButtonUp(0)) { dragging = false; if (gamePieceDragging) { dropPosittion = clickedObject.transform.position; clickedObject.transform.position = originalPosition; //Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //RaycastHit hit; var pointer = new PointerEventData(EventSystem.current); pointer.position = Input.mousePosition; EventSystem.current.RaycastAll(pointer, hitObjects); if (hitObjects.Count <= 0) { //dropped on nothing } if (hitObjects.Count > 0) { foreach (RaycastResult r in hitObjects) { if (r.gameObject.tag == MODEL_TAG) { //here } } } Debug.Log(hitObjects[0].gameObject.name); //objectToDropOn = GetObjectUnderMouse(); // todo clean up this mess!!! } if (readyToClick && !mouseMoved) { GamePieceScript.onClick(); } readyToClick = false; mouseMoved = false; clickedObject = null; GamePieceScript = null; objectToDrag = null; dragableUI = null; gamePieceDragging = false; } if (dragging) { objectToDrag.position = (Input.mousePosition + offset); } if (gamePieceDragging) { clickedObject.transform.position = (Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane)) + offset); } oldMousePosition = Input.mousePosition; }