void localGyro(Vector3 gravity, Vector3 userAccel)
        {
            //save the last rotation
            if (didRealUpdateLastFrame)
            {
                didRealUpdateLastFrame = false;
                lastRotation           = myRotation.x;
            }


            //because we are only modifying one component I don't need to copy into a local variable
            myRotation.x = EasyInputUtilities.relativeAngleInAxis(Vector3.up, -gravity, Vector3.right);

            myRotation.x *= -sensitivity;

            //we need to adjust for if the z is parallel with gravity (x undefined)
            if (gravity.z > .97 || gravity.z < -.97)
            {
                //all the way forward or back
                myRotation.x = lastRotation;
            }

            //clamp the up/down rotation if set this way
            if (clampRotation && clampRotationDegreesFromZero != 0f)
            {
                if (myRotation.x <= (360f - clampRotationDegreesFromZero) && myRotation.x >= clampRotationDegreesFromZero)
                {
                    if (myRotation.x <= 180f)
                    {
                        myRotation.x = clampRotationDegreesFromZero;
                    }
                    else if (myRotation.x > 180f)
                    {
                        myRotation.x = (360f - clampRotationDegreesFromZero);
                    }
                }
            }

            //at this point we'd have the correct result but it would be very shaky
            //lerp or slerp doesn't seem to be good enough so manually make it smooth
            if ((lastRotation - myRotation.x) > precision || (lastRotation - myRotation.x) < -precision)
            {
                //only moving in increments of tilt precesion degrees
                didRealUpdateLastFrame  = true;
                transform.localRotation = Quaternion.Euler(myRotation);
            }
        }
        void localAccelerometer(Vector3 accel)
        {
            //accelerometers due to gravity can really only sense 2 axis (can't filter out gravity)
            //here we convert those 2 axis into horizontal and vertical and normalize
            horizontal = EasyInputUtilities.relativeAngleInAxis(Vector3.up, -accel, Vector3.forward) / normalizeDegrees;
            vertical   = EasyInputUtilities.relativeAngleInAxis(Vector3.up, -accel, Vector3.right) / normalizeDegrees;

            horizontal *= -sensitivity * Time.deltaTime * 100f;
            vertical   *= -sensitivity * Time.deltaTime * 100f;

            actionVector3 = EasyInputUtilities.getControllerVector3(horizontal, vertical, tiltHorizontal, tiltVertical);

            switch (action)
            {
            case EasyInputConstants.ACTION_TYPE.Position:
                transform.position += actionVector3;
                break;

            case EasyInputConstants.ACTION_TYPE.Rotation:
                transform.Rotate(actionVector3, Space.World);
                break;

            case EasyInputConstants.ACTION_TYPE.LocalPosition:
                transform.Translate(actionVector3);
                break;

            case EasyInputConstants.ACTION_TYPE.LocalRotation:
                transform.Rotate(actionVector3);
                break;

            case EasyInputConstants.ACTION_TYPE.LocalScale:
                transform.localScale += actionVector3;
                break;

            default:
                Debug.Log("Invalid Action");
                break;
            }
        }
示例#3
0
        void localGyro(Vector3 gravity, Vector3 userAcceleration)
        {
            //save the last rotation
            if (didRealUpdateLastFrame)
            {
                didRealUpdateLastFrame = false;
                lastRotation           = myRotation.y;
            }

            //reset the rotation
            myRotation.y = 0f;

#if !UNITY_EDITOR && UNITY_TVOS
            //because we are only modifying one component I don't need to copy into a local variable
            myRotation.y = EasyInputUtilities.relativeAngleInAxis(Vector3.up, -gravity, Vector3.forward);

            myRotation.y *= -tiltSensitivity;

            //we need to adjust for if the z is parallel with gravity ( y undefined)
            if (gravity.x > .97 || gravity.x < -.97)
            {
                //all the way forward or back
                myRotation.y = lastRotation;
            }
#endif

            myRotation.y += accumulatedRotation;

            //at this point we'd have the correct result but it would be very shaky
            //lerp or slerp doesn't seem to be good enough so manually make it smooth
            if ((lastRotation - myRotation.y) > tiltPrecision || (lastRotation - myRotation.y) < -tiltPrecision)
            {
                //only moving in increments of tilt precesion degrees
                didRealUpdateLastFrame         = true;
                player.transform.localRotation = Quaternion.Euler(myRotation);
            }
        }
示例#4
0
        public void steerBall(Vector3 accel)
        {
            //accelerometers due to gravity can really only sense 2 axis (can't filter out gravity)
            //here we convert those 2 axis into horizontal and vertical and normalize
            if (accel != Vector3.zero)
            {
                horizontal = EasyInputUtilities.relativeAngleInAxis(Vector3.up, -accel, Vector3.forward) / normalizeDegrees;
                vertical   = EasyInputUtilities.relativeAngleInAxis(Vector3.up, -accel, Vector3.right) / normalizeDegrees;

                horizontal *= -sensitivity;
                vertical   *= -sensitivity;
            }
            else
            {
                horizontal = 0f;
                vertical   = 0f;
            }

            actionVectorPosition.x = horizontal;
            actionVectorPosition.y = 0f;
            actionVectorPosition.z = vertical;

            myRigidbody.AddForce(actionVectorPosition);
        }