Пример #1
0
    // Update is called once per frame
    void Update()
    {
        ghost.Rotate(axis * Time.deltaTime);

        rot = ghost.rotation;

        // how rotate
        ballRadius = sphereScript.GetViewerSize();
        moveSpeed  = sphereScript.GetWorldSize();

        savedOrientation     = Quaternion.Slerp(savedOrientation, rot, Time.deltaTime * 2f);
        target.localRotation = upTrans * savedOrientation;

        target.localPosition += ballRadius * moveSpeed * Vector3.forward * Time.deltaTime / 1000f;
    }
    void Update()
    {
        time += Time.deltaTime;
        // update moveSpeed, move in accordance with how big the world is
        ballRadius = sphereScript.GetViewerSize();
        moveSpeed  = 1f * sphereScript.GetWorldSize();

        if (!device.IsReading)
        {
            attempt++;
            tryConnect();
        }

        cmd = CheckForReceivedData();
        if (cmd.StartsWith("R"))
        { //Got a rotation command
            // save raw rotation in rot
            ParseAccelerometerDataG(cmd);
        }

        // set new rotation (angular velocity)
        tempRot              = target.localRotation; // save old rotation
        savedOrientation     = Quaternion.Slerp(savedOrientation, rot, Time.deltaTime * 2f);
        target.localRotation = upTrans * firstRec * savedOrientation;

        // applying translation
        diff = tempRot * Quaternion.Inverse(target.localRotation); // difference in rotation
        diff.ToAngleAxis(out rotAngle, out rotAxis);               // calculate how sphere moved
        // rotAxis *= -1;

        // find vector orthogonal to up and rotation-axis vector (which means it never travels along Z-axis this way)
        transDir = Vector3.Cross(rotAxis, upDir).normalized;
        // compute magnitude of translation = angular fraction * circumference
        transDir             *= rotAngle / (360f) * (2f * Mathf.PI * ballRadius);
        target.localPosition += transDir * Time.deltaTime * moveSpeed;

        statusText.text = rot + "\n" + target.localPosition + "\n" + target.localRotation.eulerAngles;
    }