Пример #1
0
    private Vector3 TerrainLocationForPosition(Vector3 position)
    {
        Ray ray = Camera.ScreenPointToRay(position);

        TerrainTester.transform.position = Camera.transform.position;
        while (true)
        {
            TerrainTester.transform.position += ray.direction * (Time.deltaTime * 15f);
            SurfaceCollider.TerrainElevationGetter();
            if (TerrainTester.transform.position.y < SurfaceCollider.minCameraElevation)
            {
                break;
            }
            if (Vector3.SqrMagnitude(TerrainTester.transform.position - Camera.transform.position) > (50000f * 50000f))
            {
                return(Vector3.positiveInfinity);
            }
        }
        return(TerrainTester.transform.position);
    }
Пример #2
0
    private void Update()
    {
        if (!IsPointerOverUIObject() || !(EventSystem.current &&
                                          EventSystem.current.currentSelectedGameObject &&
                                          EventSystem.current.currentSelectedGameObject.GetComponentInChildren <InputField>()))
        {
            if (toggle.newMovement)
            {
                float      pinchAmount     = 0f;
                Quaternion desiredRotation = Quaternion.identity;

                Cognitics.Unity.TouchInput.Calculate();

                if (Input.touchCount == 1)
                {
                    isTouch = true;
                }
                else
                {
                    isTouch = false;
                }
                if (!isDragging && isTouch && (Input.GetTouch(0).phase == TouchPhase.Began))
                {
                    InitDrag();
                }
                if (isDragging && isTouch && (Input.GetTouch(0).phase == TouchPhase.Moved))
                {
                    MoveCamera();
                }
                if (isDragging && isTouch && (Input.GetTouch(0).phase == TouchPhase.Ended))
                {
                    FinishDrag();
                }

                if (Cognitics.Unity.TouchInput.pivotPoint == Vector3.zero)
                {
                    Cognitics.Unity.TouchInput.pivotPoint = Camera.main.transform.position;
                }
                if (Mathf.Abs(Cognitics.Unity.TouchInput.pinchDistanceDelta) > 0)
                {
                    pinchAmount = Cognitics.Unity.TouchInput.pinchDistanceDelta;
                }

                if (Mathf.Abs(Cognitics.Unity.TouchInput.turnAngleDelta) > 0)
                {
                    Vector3 rotationDeg = Vector3.zero;
                    rotationDeg.y    = -Cognitics.Unity.TouchInput.turnAngleDelta;
                    desiredRotation *= Quaternion.Euler(rotationDeg);
                }
                if (Cognitics.Unity.TouchInput.touchDirection > 0)
                {
                    Vector3 angle = gameObject.transform.localEulerAngles + new Vector3(Cognitics.Unity.TouchInput.twoTouchDelta / lookSensitivity, 0, 0);
                    angle.x = Mathf.Clamp(angle.x, 10, 80);
                    transform.localEulerAngles = angle;
                }
                if (Cognitics.Unity.TouchInput.pivotPoint != Vector3.zero)
                {
                    transform.RotateAround(Cognitics.Unity.TouchInput.pivotPoint, Vector3.up, desiredRotation.y * rotationSensitivity * -1);
                }
                MoveForward(pinchAmount);
                Cognitics.Unity.TouchInput.twoTouchDelta = 0f;

                //Mathf.Clamp(transform.rotation.y, 10, 80);

                if (!Cognitics.Unity.TouchInput.checkForPoint)
                {
                    checkForPointTimeout += Time.deltaTime;
                    if (checkForPointTimeout >= .5f)
                    {
                        checkForPointTimeout = 0f;
                        Cognitics.Unity.TouchInput.checkForPoint = true;
                    }
                }
            }
            else
            {
                if (toggle.btnPressed)
                {
                    toggle.btnText.text = "Pan";
                    // Touch 0 should be the 'pan'button.
                    // Touch 1 should be the pan movement
                    if (Input.touchCount >= 2)
                    {
                        Pan(Input.GetTouch(1));
                    }
                }
                else
                {
                    toggle.btnText.text = "Hold to Pan";
                    if (Input.touchCount == 1)
                    {
                        Rotate(Input.GetTouch(0));
                    }
                    if (Input.touchCount >= 2)
                    {
                        Touch touch1 = Input.GetTouch(0);
                        Touch touch2 = Input.GetTouch(1);

                        Vector2 TouchDelta        = (touch1.deltaPosition + touch2.deltaPosition) / 2;
                        Vector2 touch1PrevPos     = touch1.position - touch1.deltaPosition;
                        Vector2 touch2PrevPos     = touch2.position - touch2.deltaPosition;
                        double  prevTouchDistance = Vector2.Distance(touch1PrevPos, touch2PrevPos);
                        double  touchDistance     = Vector2.Distance(touch1.position, touch2.position);
                        int     touchDirection    = (touchDistance - prevTouchDistance) > 0 ? 1 : -1;
                        float   translationSpeed  = touchDirection * SpeedSlider * TouchDelta.magnitude * DETAIL_MODE_SPEEDFACTOR;
                        MoveForward(translationSpeed);
                    }
                }
            }
            if (Input.GetMouseButton(1))
            {
                float currentX = Mathf.Lerp(lastX, Input.GetAxis("Mouse X"), MouseSensitivity);
                float currentY = Mathf.Lerp(lastY, -Input.GetAxis("Mouse Y"), MouseSensitivity);
                lastX = currentX;
                lastY = currentY;
                transform.localEulerAngles += new Vector3(currentY, currentX, 0f);
            }
            if (Input.GetMouseButton(2))
            {
                float mouseX = Input.GetAxis("Mouse X");
                float mouseY = -Input.GetAxis("Mouse Y");
                transform.position += new Vector3(-SpeedFactor() * 10.0f * mouseX * Time.deltaTime, SpeedFactor() * 10.0f * mouseY * Time.deltaTime, 0.0f);
            }
        }


        if (Input.GetKey(KeyCode.LeftArrow))
        {
            rotationY         -= RotationSpeed * Time.deltaTime;
            transform.rotation = Quaternion.Euler(rotationX, rotationY, 0.0f);
        }
        if (Input.GetKey(KeyCode.RightArrow))
        {
            rotationY         += RotationSpeed * Time.deltaTime;
            transform.rotation = Quaternion.Euler(rotationX, rotationY, 0.0f);
        }
        if (Input.GetKey(KeyCode.UpArrow))
        {
            rotationX         -= RotationSpeed * Time.deltaTime;
            transform.rotation = Quaternion.Euler(rotationX, rotationY, 0.0f);
        }
        if (Input.GetKey(KeyCode.DownArrow))
        {
            rotationX         += RotationSpeed * Time.deltaTime;
            transform.rotation = Quaternion.Euler(rotationX, rotationY, 0.0f);
        }
        if (!(EventSystem.current &&
              EventSystem.current.currentSelectedGameObject &&
              EventSystem.current.currentSelectedGameObject.GetComponentInChildren <InputField>()))
        {
            if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.I))
            {
                MoveForward();
            }
            if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.K))
            {
                MoveBackward();
            }
            if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.J))
            {
                MoveLeft();
            }
            if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.L))
            {
                MoveRight();
            }
            if (Input.GetKey(KeyCode.C) || Input.GetKey(KeyCode.Space))
            {
                MoveUp();
            }
            if (Input.GetKey(KeyCode.Z))
            {
                MoveDown();
            }
            if (Input.GetKey(KeyCode.KeypadMinus))
            {
                MoveHigher();
            }
            if (Input.GetKey(KeyCode.KeypadPlus))
            {
                MoveLower();
            }
            if (Input.GetKeyDown(KeyCode.R))
            {
                Reset();
            }
        }

        if (surfaceCollider != null)
        {
            surfaceCollider.TerrainElevationGetter();
            float diffY = transform.position.y - ((float)surfaceCollider.minCameraElevation + MinimumElevation * 0.1f);
            if (diffY < 0.0f)
            {
                transform.Translate(Vector3.up * -diffY);
            }
        }
    }
Пример #3
0
    static public void Calculate()
    {
        pinchDistance   = pinchDistanceDelta = 0;
        turnAngle       = turnAngleDelta = 0;
        terrainTester   = GameObject.Find("TerrainTester");
        SurfaceCollider = terrainTester.GetComponent <Cognitics.UnityCDB.SurfaceCollider>();

        if (Input.touchCount == 2)
        {
            Touch touch1 = Input.touches[0];
            Touch touch2 = Input.touches[1];

            if (touch1.phase == TouchPhase.Moved || touch2.phase == TouchPhase.Moved || Input.GetKey(KeyCode.F))
            {
                Ray ray = Camera.main.ViewportPointToRay(new Vector2(.5f, .5f));
                terrainTester.transform.position = Camera.main.transform.position;

                while (true)
                {
                    if (!checkForPoint)
                    {
                        break;
                    }
                    terrainTester.transform.position += ray.direction * 15f;
                    SurfaceCollider.TerrainElevationGetter();
                    if (terrainTester.transform.position.y < SurfaceCollider.minCameraElevation)
                    {
                        break;
                    }
                    if (Vector3.SqrMagnitude(terrainTester.transform.position - Camera.main.transform.position) > (50000f * 50000f))
                    {
                        pivotPoint    = Vector3.positiveInfinity;
                        checkForPoint = false;
                    }
                }

                pivotPoint = terrainTester.transform.position;

                touchDirection = Vector2.Dot(touch1.deltaPosition, touch2.deltaPosition);
                twoTouchDelta  = touch1.deltaPosition.y + touch2.deltaPosition.y;
                pinchDistance  = Vector2.Distance(touch1.position, touch2.position);
                float prevDistance = Vector2.Distance(touch1.position - touch1.deltaPosition, touch2.position - touch2.deltaPosition);

                pinchDistanceDelta = pinchDistance - prevDistance;

                if (Mathf.Abs(pinchDistanceDelta) > minPinchDistance)
                {
                    pinchDistanceDelta *= pinchRatio;
                }
                else
                {
                    pinchDistance = pinchDistanceDelta = 0;
                }

                turnAngle = Angle(touch1.position, touch2.position);
                float prevTurn = Angle(touch1.position - touch1.deltaPosition, touch2.position - touch2.deltaPosition);
                turnAngleDelta = Mathf.DeltaAngle(prevTurn, turnAngle);

                if (Mathf.Abs(turnAngleDelta) > minTurnAngle)
                {
                    turnAngleDelta *= pinchTurnRatio;
                }
                else
                {
                    turnAngle = turnAngleDelta = 0;
                }
            }
        }
    }