Пример #1
0
    // Update is called once per frame
    void Update()
    {
        m_lastMouse   = m_curMousePos;
        m_curMousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));

        // Make sure the user pressed the mouse down
        if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
        {
            // Position relative to the eye-point of the camera
            //m_curMousePos -= transform.position;
            m_origPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));

            // We need to actually hit an object
            RaycastHit hit;
            GameObject gO = null;

            RaycastHit[] rays = Physics.RaycastAll(new Vector3(m_origPos.x, m_origPos.y, 10), -Vector3.forward, 10000);
            for (int i = 0; i < rays.Length; i++)
            {
                if (rays[i].rigidbody)
                {
                    if (rays[i].rigidbody.gameObject.name != "Line" || rays[i].rigidbody.gameObject.name == "Line" && gO == null)
                    {
                        gO = rays[i].rigidbody.gameObject;
                    }
                }
            }

            Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 10000);
            // BJL: If we use a collider instead of a rigidbody and the distance parameter is too large, we'll get a hit all the time.
            // Could this be due to reflection or because the camera is orthographic, or...?
            //if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 10000) && hit.rigidbody && Input.GetMouseButtonDown(0))
            if (Input.GetMouseButtonDown(0) && gO != null)
            {
                m_object = gO;

                if (m_object.name == "Arrow Collider")
                {
                    UndoScript.AddToUndo(LineScript.GetCurrentLine().GetComponentsInChildren <SphereCollider>()[0].gameObject, m_curMousePos);
                }
                else if (m_object.name == "Line")
                {
                    UndoScript.AddToUndo(LineScript.GetCurrentLine());
                }
                else
                {
                    UndoScript.AddToUndo(m_object, m_curMousePos);
                }

                StartCoroutine("DragObject");
            }
            else if (Input.GetMouseButtonDown(1))
            {
                m_lineStart = true;
                LineScript.SetFreePath();
            }
        }
        else if (Input.GetKey(KeyCode.LeftControl) && Input.GetKeyDown(KeyCode.Z) &&
                 UndoScript.GetUndoPos() > 0)
        {
            UndoScript.Undo(m_object);
        }
        else if (Input.GetKey(KeyCode.LeftControl) && Input.GetKeyDown(KeyCode.Y) &&
                 UndoScript.GetUndoObjectsCount() > UndoScript.GetUndoPos())
        {
            UndoScript.Redo(m_object);
        }
        else if (Input.GetKeyDown(KeyCode.Delete))
        {
            DeleteObject();
        }

        if (Input.GetKeyDown(KeyCode.LeftShift))
        {
            m_grid.GetComponent <SpriteRenderer>().enabled = true;
        }
        else if (Input.GetKeyUp(KeyCode.LeftShift))
        {
            m_grid.GetComponent <SpriteRenderer>().enabled = false;
        }

        LineScript.BendLine();

        if (Input.GetMouseButton(1) && m_lineStart)
        {
            LineScript.UpdateLine(m_origPos);
        }
        else if (Input.GetMouseButtonUp(1) && m_lineStart)
        {
            m_lineStart = false;
            LineScript.ResetBend();
            LineScript.SetMouseWheel(0);
        }
    }