コード例 #1
0
ファイル: Joystick.cs プロジェクト: DragonJawad/Roll-the-Ball
    void Update()
    {
        // Check if the control settings have been changed
        if (!touchPad && !controlsChanged && Control.controlsChangeCheck)
        {         // If controls have been recently changed, apply changes
            // Indicate that the controls change has been noticed
            controlsChanged = true;

            // Reset the size of the joystick
            ResetJoystickSize();
        }

        if (!enumeratedJoysticks)
        {
            // Collect all joysticks in the game, so we can relay finger latching messages
            //Joystick[] joysticks = Object.FindObjectsOfType( Joystick ) as Joystick[];
            //		Joystick[] joysticks = UnityEngine.Object.FindObjectsOfType( typeof(Joystick) ) as Joystick[];
            enumeratedJoysticks = true;
        }

        int count = Input.touchCount;

        // Adjust the tap time window while it still available
        if (tapTimeWindow > 0)
        {
            tapTimeWindow -= Time.deltaTime;
        }
        else
        {
            tapCount = 0;
        }

        if (count == 0)
        {
            ResetJoystick();
        }
        else
        {
            for (int i = 0; i < count; i++)
            {
                Touch   touch       = Input.GetTouch(i);
                Vector2 guiTouchPos = touch.position - guiTouchOffset;

                bool shouldLatchFinger = false;
                if (touchPad)
                {
                    if (touchZone.Contains(touch.position))
                    {
                        shouldLatchFinger = true;
                    }
                }
                else if (gui.HitTest(touch.position))
                {
                    shouldLatchFinger = true;
                }

                // Latch the finger if this is a new touch
                if (shouldLatchFinger && (lastFingerId == -1 || lastFingerId != touch.fingerId))
                {
                    if (touchPad)
                    {
                        /*  Deprecated: Separate jump button
                         *      // Change the alpha for tap effect
                         *      Color color = gui.color;
                         *      color.a = 0.25f;
                         *      gui.color = color;
                         */

                        // Activate jump
                        player.GetComponent <PlayerBall>().Jump();
                        // Change the jump button's alpha
                        jumpButton.GetComponent <JumpButton>().ChangeAlpha(0.1f);

                        lastFingerId  = touch.fingerId;
                        fingerDownPos = touch.position;
//						fingerDownTime = Time.time;
                    }

                    lastFingerId = touch.fingerId;

                    // Accumulate taps if it is within the time window
                    if (tapTimeWindow > 0)
                    {
                        tapCount++;
                    }
                    else
                    {
                        tapCount      = 1;
                        tapTimeWindow = tapTimeDelta;
                    }

                    /*	if(!touchPad){
                     *      // Tell other joysticks we've latched this finger
                     *      foreach ( Joystick j in joysticks )
                     *      {
                     *              if ( j != this )
                     *                      j.LatchedFinger( touch.fingerId );
                     *      }
                     *      } */
                }

                if (lastFingerId == touch.fingerId)
                {
                    // Override the tap count with what the iPhone SDK reports if it is greater
                    // This is a workaround, since the iPhone SDK does not currently track taps
                    // for multiple touches
                    if (touch.tapCount > tapCount)
                    {
                        tapCount = touch.tapCount;
                    }

                    if (touchPad)
                    {
                        // For a touchpad, let's just set the position directly based on distance from initial touchdown
                        position.x = Mathf.Clamp((touch.position.x - fingerDownPos.x) / (touchZone.width / 2), -1, 1);
                        position.y = Mathf.Clamp((touch.position.y - fingerDownPos.y) / (touchZone.height / 2), -1, 1);
                    }
                    else
                    {
                        // Change the location of the joystick graphic to match where the touch is
                        Rect clamp = new Rect(Mathf.Clamp(guiTouchPos.x, guiBoundary.min.x, guiBoundary.max.x),
                                              Mathf.Clamp(guiTouchPos.y, guiBoundary.min.y, guiBoundary.max.y),
                                              gui.pixelInset.width, gui.pixelInset.height);
                        gui.pixelInset = clamp;
                        //		gui.pixelInset.x =  Mathf.Clamp( guiTouchPos.x, guiBoundary.min.x, guiBoundary.max.x );
                        //		gui.pixelInset.y =  Mathf.Clamp( guiTouchPos.y, guiBoundary.min.y, guiBoundary.max.y );
                    }

                    if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
                    {
                        ResetJoystick();
                    }
                }
            }
        }

        if (!touchPad)
        {
            // Get a value between -1 and 1 based on the joystick graphic location
            position.x = (gui.pixelInset.x + guiTouchOffset.x - guiCenter.x) / guiTouchOffset.x;
            position.y = (gui.pixelInset.y + guiTouchOffset.y - guiCenter.y) / guiTouchOffset.y;
            playerScript.GetMoveInput(new Vector3(position.x, 0, position.y));
        }

        // Adjust for dead zone
        var absoluteX = Mathf.Abs(position.x);
        var absoluteY = Mathf.Abs(position.y);

        if (absoluteX < deadZone.x)
        {
            // Report the joystick as being at the center if it is within the dead zone
            position.x = 0;
        }
        else if (normalize)
        {
            // Rescale the output after taking the dead zone into account
            position.x = Mathf.Sign(position.x) * (absoluteX - deadZone.x) / (1 - deadZone.x);
        }

        if (absoluteY < deadZone.y)
        {
            // Report the joystick as being at the center if it is within the dead zone
            position.y = 0;
        }
        else if (normalize)
        {
            // Rescale the output after taking the dead zone into account
            position.y = Mathf.Sign(position.y) * (absoluteY - deadZone.y) / (1 - deadZone.y);
        }

        // Make the ball move
        //playerScript.GetMoveInput(new Vector3(position.x, 0, position.y));
        //player.GetComponent<PlayerBall>().GetMoveInput(new Vector3(position.x, 0, position.y));
    }
コード例 #2
0
    }     // end SimplifyNormalTilt()

    public void SendTilt(Vector2 tiltSend)
    {
        playerScript.GetMoveInput(new Vector3(tiltSend.x, 0, tiltSend.y));
    }     // end SendTilt