Exemple #1
0
    void Update()
    {
        if (CanUseControls())
        {
            accelerometer.ReadInput(); // Wrapper takes accelerometer values and calculates tilts.
            float yaw      = accelerometer.Yaw();
            float pitch    = accelerometer.Pitch();
            float roll     = accelerometer.Roll();
            float minPitch = isEvading ? 55 : 66; // If already evading, more intense tilt needed to stop.
            if (pitch >= minPitch && Mathf.Abs(roll) <= 45F)
            {
                SetEvading(true);
            }
            else
            {
                SetEvading(false);
                transform.rotation = Quaternion.Euler(0, 0, yaw); // Rotate player to match device yaw.
            }

            if (isAnimating)
            {
                animationTime += Time.deltaTime;

                if (animationTime > animationLength)
                {
                    isAnimating = false; // Animation is finished.
                }

                float ap    = Mathf.Min(animationTime, animationLength) / animationLength; // [0 - 1];\
                float scale = isEvading ? 1F - 0.2F * ap : 0.8F + 0.2F * ap;               // If evading, 1.0->0.8. Otherwise, 0.8->1.0
                transform.localScale = new Vector3(scale, scale, 1F);
            }
        }
    }