Пример #1
0
    // ----------------------------------------------------------------------------------------------------
    // Gets the movement to apply to the player
    // ----------------------------------------------------------------------------------------------------
    public override Vector3 GetMovement(CharacterController _controller, float _forwardSpeed, float _backwardSpeed, float _sidestepSpeed)
    {
        int touchCount = iPhoneInputSim.touchCount;

        if (touchCount >= 1 && m_State == ControlState.MovingCharacter)
        {
            iPhoneTouchSim touch0 = iPhoneInputSim.GetTouch(0);

            // Do not recompute target location if we start a jump or we will head towards the jump button
            if (!GetJump(_controller) && touch0.phase != iPhoneTouchPhaseSim.Began)
            {
                // Move
                RaycastHit hit;
                Ray        ray = m_CameraComponent.ScreenPointToRay(new Vector3(touch0.position.x, touch0.position.y));

                Debug.DrawRay(ray.origin, ray.direction * 10.0f, Color.yellow);

                if (Physics.Raycast(ray, out hit))
                {
                    float touchDist = (transform.position - hit.point).magnitude;

                    if (touchDist > m_MinimumDistanceToMove)
                    {
                        m_TargetLocation = hit.point;
                    }

                    m_Moving = true;
                }
            }
        }

        Vector3 movement = Vector3.zero;

        if (m_Moving)
        {
            movement   = m_TargetLocation - m_ThisTransform.position;
            movement.y = 0.0f;
            float dist = movement.magnitude;
            if (dist < 1.0f)
            {
                m_Moving = false;
            }
            else
            {
                movement = movement.normalized * _forwardSpeed;
            }
        }

        return(movement);
    }
Пример #2
0
    // ----------------------------------------------------------------------------------------------------
    // Update is called once per frame
    // ----------------------------------------------------------------------------------------------------
    void Update()
    {
        if (!m_Display && m_Selection == -1 && iPhoneInputSim.touchCount > 0)
        {
            for (int i = 0; i < iPhoneInputSim.touchCount; i++)
            {
                iPhoneTouchSim touch = iPhoneInputSim.GetTouch(i);

                if (touch.phase == iPhoneTouchPhaseSim.Began && guiTexture.HitTest(touch.position))
                {
                    m_Display           = true;
                    m_DisplayBackground = false;
                    guiTexture.enabled  = false;
                }
            }
        }
    }
Пример #3
0
    // ----------------------------------------------------------------------------------------------------
    // Camera control (zoom/rotate)
    // ----------------------------------------------------------------------------------------------------
    void CameraControl(iPhoneTouchSim _touch0, iPhoneTouchSim _touch1)
    {
        if (m_RotateEnabled && m_State == ControlState.RotatingCamera)
        {
            Vector2 currentVector = _touch1.position - _touch0.position;
            Vector2 currentDir    = currentVector / currentVector.magnitude;
            Vector2 lastVector    = (_touch1.position - _touch1.deltaPosition) - (_touch0.position - _touch0.deltaPosition);
            Vector2 lastDir       = lastVector / lastVector.magnitude;
            float   rotationCos   = Vector2.Dot(currentDir, lastDir);

            if (rotationCos < 1)
            {
                Vector3 currentVector3    = new Vector3(currentVector.x, currentVector.y);
                Vector3 lastVector3       = new Vector3(lastVector.x, lastVector.y);
                float   rotationDirection = Vector3.Cross(currentVector3, lastVector3).normalized.z;
                float   rotationRad       = Mathf.Acos(rotationCos);
                m_RotationTarget += rotationRad * Mathf.Rad2Deg * rotationDirection;

                if (m_RotationTarget < 0.0f)
                {
                    m_RotationTarget += 360.0f;
                }
                else if (m_RotationTarget >= 360.0f)
                {
                    m_RotationTarget -= 360.0f;
                }
            }
        }
        else if (m_ZoomEnabled && m_State == ControlState.ZoomingCamera)
        {
            float touchDistance     = (_touch1.position - _touch0.position).magnitude;
            float lastTouchDistance = ((_touch1.position - _touch1.deltaPosition) - (_touch0.position - _touch0.deltaPosition)).magnitude;
            float deltaPinch        = touchDistance - lastTouchDistance;

            m_TargetRelativePositioner.Zoom += deltaPinch * m_ZoomRate * Time.deltaTime;
        }
    }
Пример #4
0
    // ----------------------------------------------------------------------------------------------------
    // Returns an array of iPhoneTouchSim representing all touches that are currently active
    // ----------------------------------------------------------------------------------------------------
    private static void UpdateTouches()
    {
        // Put the touches in an array to have same code behavior as on iPhone
        m_TouchCount = 0;
        if (Input.GetMouseButton(0) || Input.GetMouseButtonUp(0))
        {
            m_Touches[m_TouchCount]   = new iPhoneTouchSim(GetDeltaPosition(m_TouchCount, Input.mousePosition), Input.mousePosition, iPhoneTouchSim.FingerId.Mouse, MouseUtility.GetMousePhase());
            MouseUtility.LastPosition = m_Touches[m_TouchCount].position;
            m_TouchCount++;
        }

        // Also move the jump button to the suggested position so it does not interact with other control setups
        if (KeyboardUtility.KeyboardTouched())
        {
            m_Touches[m_TouchCount]      = new iPhoneTouchSim(GetDeltaPosition(m_TouchCount, KeyboardUtility.Position), KeyboardUtility.Position, iPhoneTouchSim.FingerId.Arrows, KeyboardUtility.GetKeyboardPhase());
            KeyboardUtility.LastPosition = m_Touches[m_TouchCount].position;
            m_TouchCount++;
        }

        // On the iPhone, taps are ordered
        if (Input.GetMouseButtonDown(0))
        {
            m_LastPcFingerId = iPhoneTouchSim.FingerId.Mouse;
        }
        else if (KeyboardUtility.KeyboardJustPressed())
        {
            m_LastPcFingerId = iPhoneTouchSim.FingerId.Arrows;
        }

        // Swap mouse & keyboard if mouse is last
        if (m_TouchCount == 2 && m_LastPcFingerId == iPhoneTouchSim.FingerId.Mouse)
        {
            iPhoneTouchSim touch = m_Touches[0];
            m_Touches[0] = m_Touches[1];
            m_Touches[1] = touch;
        }
    }
Пример #5
0
    // ----------------------------------------------------------------------------------------------------
    // Update is called once per frame
    // ----------------------------------------------------------------------------------------------------
    void Update()
    {
        if (!m_EnumeratedJoysticks)
        {
            m_Joysticks           = FindObjectsOfType(typeof(Joystick)) as Joystick[];
            m_EnumeratedJoysticks = true;
        }

        // Check for double taps
        if (m_TapTimeWindow > 0)
        {
            m_TapTimeWindow -= Time.deltaTime;
        }
        else
        {
            m_TapCount = 0;
        }

        int touchCount = iPhoneInputSim.touchCount;

        if (touchCount == 0)
        {
            Reset();
        }
        else
        {
            for (int i = 0; i < touchCount; i++)
            {
                iPhoneTouchSim touch = iPhoneInputSim.GetTouch(i);

                if (m_Gui.HitTest(touch.position))
                {
                    if (m_LastFingerId == -1 || m_LastFingerId != touch.fingerId)
                    {
                        m_LastFingerId = touch.fingerId;

                        if (m_TapTimeWindow > 0)
                        {
                            m_TapCount++;
                        }
                        else
                        {
                            m_TapCount      = 1;
                            m_TapTimeWindow = m_TapTimeDelta;
                        }

                        // Latching the new touch
                        foreach (Joystick joystick in m_Joysticks)
                        {
                            if (joystick != this)
                            {
                                joystick.LatchedFinger(touch.fingerId);
                            }
                        }
                    }
                }

                if (m_LastFingerId == touch.fingerId)
                {
                    Vector2 guiTouchPos = touch.position - m_GuiTouchOffset;

                    Rect pixelInset = m_Gui.pixelInset;
                    pixelInset.x     = Mathf.Clamp(guiTouchPos.x, m_GuiBoundary.Min.x, m_GuiBoundary.Max.x);
                    pixelInset.y     = Mathf.Clamp(guiTouchPos.y, m_GuiBoundary.Min.y, m_GuiBoundary.Max.y);
                    m_Gui.pixelInset = pixelInset;

                    // Finger just released
                    if (touch.phase == iPhoneTouchPhaseSim.Ended)
                    {
                        Reset();
                    }
                }
            }
        }

        // Get joystick position clamped and deadzoned
        m_Position.x = (m_Gui.pixelInset.x + m_GuiTouchOffset.x - m_GuiCenter.x) / m_GuiTouchOffset.x;
        m_Position.y = (m_Gui.pixelInset.y + m_GuiTouchOffset.y - m_GuiCenter.y) / m_GuiTouchOffset.y;

        float absoluteX = Mathf.Abs(m_Position.x);
        float absoluteY = Mathf.Abs(m_Position.y);

        if (absoluteX < m_DeadZone.x)
        {
            m_Position.x = 0.0f;
        }
        else if (m_Normalize)
        {
            m_Position.x = Mathf.Sign(m_Position.x) * (absoluteX - m_DeadZone.x) / (1.0f - m_DeadZone.x);
        }

        if (absoluteY < m_DeadZone.y)
        {
            m_Position.y = 0.0f;
        }
        else if (m_Normalize)
        {
            m_Position.y = Mathf.Sign(m_Position.y) * (absoluteY - m_DeadZone.y) / (1.0f - m_DeadZone.y);
        }
    }
Пример #6
0
    // ----------------------------------------------------------------------------------------------------
    // Update is called once per frame
    // ----------------------------------------------------------------------------------------------------
    void Update()
    {
        int touchCount = iPhoneInputSim.touchCount;

        if (touchCount == 0)
        {
            ResetControlState();
        }
        else
        {
            int              i;
            iPhoneTouchSim   touch;
            iPhoneTouchSim[] touches = iPhoneInputSim.touches;

            // Must fix the bad bool design to prevent getting useless touches here
            iPhoneTouchSim touch0    = iPhoneInputSim.GetTouch(0);
            iPhoneTouchSim touch1    = iPhoneInputSim.GetTouch(1);
            bool           gotTouch0 = false;
            bool           gotTouch1 = false;

            if (m_State == ControlState.WaitingForFirstTouch)
            {
                for (i = 0; i < touchCount; i++)
                {
                    touch = touches[i];

                    if (touch.phase != iPhoneTouchPhaseSim.Ended || touch.phase != iPhoneTouchPhaseSim.Canceled)
                    {
                        m_State                 = ControlState.WaitingForSecondTouch;
                        m_FirstTouchTime        = Time.time;
                        m_FingerDown[0]         = touch.fingerId;
                        m_FingerDownPosition[0] = touch.position;
                        m_FingerDownFrame[0]    = Time.frameCount;
                        break;
                    }
                }
            }

            if (m_State == ControlState.WaitingForSecondTouch)
            {
                for (i = 0; i < touchCount; i++)
                {
                    touch = touches[i];

                    if (touch.phase != iPhoneTouchPhaseSim.Canceled)
                    {
                        if (touchCount >= 2 && touch.fingerId != m_FingerDown[0])
                        {
                            m_State                 = ControlState.WaitingForMovement;
                            m_FingerDown[1]         = touch.fingerId;
                            m_FingerDownPosition[1] = touch.position;
                            m_FingerDownFrame[1]    = Time.frameCount;
                            break;
                        }
                        // Move
                        else if (touchCount == 1)
                        {
                            if (touch.fingerId == m_FingerDown[0] && (Time.time > m_FirstTouchTime + m_MinimumTimeUntilMove || touch.phase == iPhoneTouchPhaseSim.Ended))
                            {
                                m_State = ControlState.MovingCharacter;
                                break;
                            }
                        }
                    }
                }
            }

            if (m_State == ControlState.WaitingForMovement)
            {
                for (i = 0; i < touchCount; i++)
                {
                    touch = touches[i];

                    if (touch.phase == iPhoneTouchPhaseSim.Began)
                    {
                        if (touch.fingerId == m_FingerDown[0] && m_FingerDownFrame[0] == Time.frameCount)
                        {
                            touch0    = touch;
                            gotTouch0 = true;
                        }
                        else if (touch.fingerId != m_FingerDown[0] && touch.fingerId != m_FingerDown[1])
                        {
                            m_FingerDown[1] = touch.fingerId;
                            touch1          = touch;
                            gotTouch1       = true;
                        }
                    }
                    if (touch.phase == iPhoneTouchPhaseSim.Moved || touch.phase == iPhoneTouchPhaseSim.Stationary || touch.phase == iPhoneTouchPhaseSim.Ended)
                    {
                        if (touch.fingerId == m_FingerDown[0])
                        {
                            touch0    = touch;
                            gotTouch0 = true;
                        }
                        else if (touch.fingerId == m_FingerDown[1])
                        {
                            touch1    = touch;
                            gotTouch1 = true;
                        }
                    }
                }

                if (gotTouch0)
                {
                    if (gotTouch1)
                    {
                        Vector3 originalVector = m_FingerDownPosition[1] - m_FingerDownPosition[0];
                        Vector3 currentVector  = touch1.position - touch0.position;
                        Vector3 originalDir    = originalVector / originalVector.magnitude;
                        Vector3 currentDir     = currentVector / currentVector.magnitude;
                        float   rotationCos    = Vector2.Dot(originalDir, currentDir);

                        if (rotationCos < 1.0f)
                        {
                            float rotationRad = Mathf.Acos(rotationCos);
                            if (rotationRad > m_RotateEpsilon * Mathf.Deg2Rad)
                            {
                                m_State = ControlState.RotatingCamera;
                            }
                        }

                        if (m_State == ControlState.WaitingForMovement)
                        {
                            float deltaDistance = originalVector.magnitude - currentVector.magnitude;
                            if (Mathf.Abs(deltaDistance) > m_ZoomEpsilon)
                            {
                                m_State = ControlState.ZoomingCamera;
                            }
                        }
                    }
                }
                else
                {
                    m_State = ControlState.WaitingForNoFingers;
                }
            }

            if (m_State == ControlState.RotatingCamera || m_State == ControlState.ZoomingCamera)
            {
                for (i = 0; i < touchCount; i++)
                {
                    touch = touches[i];

                    if (touch.phase == iPhoneTouchPhaseSim.Moved || touch.phase == iPhoneTouchPhaseSim.Stationary || touch.phase == iPhoneTouchPhaseSim.Ended)
                    {
                        if (touch.fingerId == m_FingerDown[0])
                        {
                            touch0    = touch;
                            gotTouch0 = true;
                        }
                        else if (touch.fingerId == m_FingerDown[1])
                        {
                            touch1    = touch;
                            gotTouch1 = true;
                        }
                    }
                }
                if (gotTouch0)
                {
                    if (gotTouch1)
                    {
                        CameraControl(touch0, touch1);
                    }
                }
                else
                {
                    m_State = ControlState.WaitingForNoFingers;
                }
            }
        }
    }