Example #1
0
    //
    // Fixed Update: For Physics
    //


    void FixedUpdate()
    {
        if (cursor.isOnShelf() && !isPanning && !isRotating && !isZooming)
        {
            return;
        }

        if (cursor.mode == cursor_handle.MODE.BUILD)
        {
            // If hand2d near screen edge pan
            //Debug.Log ("Cursor pos: "+cursor.cursor2d.ToString());
            if (cursor.cursor2d.x < Screen.width / 3.0f)
            {
                PanLeft();
            }
            else if (cursor.cursor2d.x > Screen.width * 2.0f / 3.0f)
            {
                PanRight();
            }
            if (cursor.cursor2d.y < Screen.height / 3.0f)
            {
                PanBottom();
            }
            else if (cursor.cursor2d.y > Screen.height * 2.0f / 3.0f)
            {
                PanTop();
            }

            return;
        }


        // == Movement Code ==
        if (handmode)
        {
            HandRenderer hr = cursor.GetComponentInChildren <HandRenderer> ();

            if (isPanning)
            {
                Vector3 hc;
                bool    hdetected;

                hdetected = hr.queryLeftHand3DCoordinates(out hc);
                if (!hdetected)
                {
                    hdetected = hr.queryRightHand3DCoordinates(out hc);
                    if (hdetected)
                    {
                        Vector3 move = ((righthandorigin.x - hc.x) * handPanSpeed.x * Vector3.ProjectOnPlane(Camera.main.transform.right, Vector3.up)) +
                                       ((hc.z - righthandorigin.z) * handPanSpeed.y * Vector3.ProjectOnPlane(Camera.main.transform.forward, Vector3.up));
                        //Debug.Log ("Move " + move.ToString() + " = " + hc.ToString() + " + " + righthandorigin.ToString());
                        rigidbody.drag = panDrag;
                        rigidbody.AddForce(move, ForceMode.Acceleration);
                    }
                }
                else
                {
                    //if (hdetected) {
                    Vector3 move = ((lefthandorigin.x - hc.x) * handPanSpeed.x * Vector3.ProjectOnPlane(Camera.main.transform.right, Vector3.up)) +
                                   ((hc.z - lefthandorigin.z) * handPanSpeed.y * Vector3.ProjectOnPlane(Camera.main.transform.forward, Vector3.up));
                    //Debug.Log ("Move " + move.ToString());
                    rigidbody.drag = panDrag;
                    rigidbody.AddForce(move, ForceMode.Acceleration);
                }                // else
                                 //	isPanning = false;
            }
            else
            {
                if (isRotating)
                {
                    Vector3    screenCenter = new Vector3(Screen.width / 2, Screen.height / 2);
                    Ray        ray          = Camera.main.ScreenPointToRay(screenCenter);
                    RaycastHit hit;

                    if (Physics.Raycast(ray, out hit))
                    {
                        //Debug.DrawLine(ray.origin, hit.point);

                        Vector3 lhc, rhc;
                        bool    lhdetected, rhdetected;
                        lhdetected = hr.queryLeftHand3DCoordinates(out lhc);
                        rhdetected = hr.queryRightHand3DCoordinates(out rhc);
                        //Debug.Log(lhdetected.ToString() + " " + rhdetected.ToString());
                        if (lhdetected && rhdetected)
                        {
                            Vector3 move = (rhc - lhc);
                            //						Debug.Log("Rot: " + move.ToString());
                            rigidbody.transform.RotateAround(hit.point, Vector3.up, move.z * handTurnSpeed);
                        }                        // else
                                                 //	isRotating = false;
                                                 //Debug.Log("Rota is " + isRotating.ToString());
                    }
                }
                if (isZooming)
                {
                    Vector3 lhc, rhc;
                    bool    lhdetected, rhdetected;
                    lhdetected = hr.queryLeftHand3DCoordinates(out lhc);
                    rhdetected = hr.queryRightHand3DCoordinates(out rhc);
                    if (lhdetected && rhdetected)
                    {
                        float delta = ((rhc.x - lhc.x) - (righthandorigin.x - lefthandorigin.x));

                        if ((transform.position.y < 5.0f && delta >= 0) || (transform.position.y > zoomOut && delta <= 0))
                        {
                            return;
                        }

                        Vector3 move = delta * handZoomSpeed * Camera.main.transform.forward;
                        //Debug.Log (move.ToString());
                        //if (delta > 1) transform.position = transform.position + Vector3.up;
                        rigidbody.drag = zoomDrag;
                        rigidbody.AddForce(move, ForceMode.Force);
                    }                    // else
                                         //	isZooming = false;
                }
            }
        }
        else
        {
            // Rotate camera along X and Y axis
            if (isRotating)
            {
                Vector3    screenCenter = new Vector3(Screen.width / 2, Screen.height / 2);
                Ray        ray          = Camera.main.ScreenPointToRay(screenCenter);
                RaycastHit hit;
                if (Physics.Raycast(ray, out hit))
                {
                    Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin);
                    rigidbody.transform.RotateAround(hit.point, Vector3.up, pos.x * turnSpeed);
                }
            }

            // Move (pan) the camera on it's XY plane
            if (isPanning)
            {
                // Get mouse displacement vector from original to current position
                Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin);

                Vector3 move = (-pos.x * panSpeed * Vector3.ProjectOnPlane(Camera.main.transform.right, Vector3.up)) +
                               (-pos.y * panSpeed * Vector3.ProjectOnPlane(Camera.main.transform.forward, Vector3.up));
                rigidbody.drag = panDrag;
                rigidbody.AddForce(move, ForceMode.Acceleration);
            }

            // Move the camera linearly along Z axis
            if (isZooming)
            {
                /* TODO:
                 * Closer the camera, more the pan speed.
                 */
                if ((transform.position.y < 5.0f && Input.mouseScrollDelta.y >= 0) || (transform.position.y > zoomOut && Input.mouseScrollDelta.y <= 0))
                {
                    return;
                }
                Vector3 move = Input.mouseScrollDelta.y * zoomSpeed * Camera.main.transform.forward;
                rigidbody.drag = zoomDrag;
                rigidbody.AddForce(move, ForceMode.Acceleration);
            }
        }
    }