Ejemplo n.º 1
0
 // Update is called once per frame
 void Update()
 {
     //Make Sphero change color every 30 frames
     if (counter == 0)
     {
         RKUNBridge._RKUNRGB(1.0f, 0.0f, 0.0f);
     }
     else if (counter == 30)
     {
         RKUNBridge._RKUNRGB(0.0f, 1.0f, 0.0f);
     }
     else if (counter == 60)
     {
         RKUNBridge._RKUNRGB(0.0f, 0.0f, 1.0f);
     }
     counter++;
     if (counter == 90)
     {
         counter = 0;
     }
 }
Ejemplo n.º 2
0
    // Update is called once per frame
    void Update()
    {
        reAnchorJoystick();

        int count = Input.touchCount;


        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 (gui.HitTest(touch.position))
                {
                    shouldLatchFinger = true;
                }

                // Latch the finger if this is a new touch
                if (shouldLatchFinger && (lastFingerId == -1 || lastFingerId != touch.fingerId))
                {
                    lastFingerId = touch.fingerId;
                }

                if (lastFingerId == touch.fingerId)
                {
                    // Change the location of the joystick graphic to match where the touch is
                    Vector2 clampedPosition;
                    clampedPosition.x = Mathf.Clamp(guiTouchPos.x /* - (gui.pixelInset.width * 0.25f)*/, guiBoundary.min.x, guiBoundary.max.x);
                    clampedPosition.y = Mathf.Clamp(guiTouchPos.y /* - (gui.pixelInset.height * 0.25f)*/, guiBoundary.min.y, guiBoundary.max.y);

                    gui.pixelInset = new Rect(clampedPosition.x, clampedPosition.y, gui.pixelInset.width, gui.pixelInset.height);

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


        // 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;

        // 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);
        }

        Vector2 inputVector = position;
        float   heading     = Mathf.Atan2(inputVector.y, 0.0f - inputVector.x);
        float   velocity    = inputVector.magnitude;

        //Commented out for weird Sphero v2 calibration
        heading -= Mathf.PI / 2.0f;
        if (heading < 0.0f)
        {
            heading += 2.0f * Mathf.PI;
        }

        float degrees = Mathf.Rad2Deg * heading;

        //Include the heading offset for auto-heading adjust

        /*var headingDiff : float = RKUNBridge.s_calibrateGyroOffset - Input.gyro.attitude.eulerAngles.z;
         * degrees += headingDiff + RKUNBridge.s_calibrateOffset;*/

        while (degrees < 0.0f)
        {
            degrees += 360.0f;
        }
        while (degrees > 359.0f)
        {
            degrees -= 360.0f;
        }

        #if !UNITY_EDITOR
        velocity = velocity * velocityScale;
        if (velocity > velocityScale)
        {
            velocity = velocityScale;
        }
        if (velocity > 0.0f)
        {
            RKUNBridge._RKUNRoll((int)degrees, velocity);
            stopped     = false;
            lastHeading = degrees;
        }
        else if (velocity == 0.0f && stopped == false)
        {
            stopped = true;
            RKUNBridge._RKUNRoll((int)lastHeading, 0.0f);
        }
        #endif
    }
Ejemplo n.º 3
0
 // Use this for initialization
 void Start()
 {
     RKUNBridge._RKUNSetupRobotConnection();
 }