Example #1
0
        public override void Vibrate(float leftMotor, float rightMotor)
        {
            if (!IsAttached)
            {
                return;
            }

            UnityGamepad.SetMotorSpeeds(leftMotor, rightMotor);
        }
Example #2
0
        /// <summary>
        /// A coroutine used to play patterns
        /// </summary>
        /// <param name="pattern"></param>
        /// <param name="lowFreqAmplitudes"></param>
        /// <param name="highFreqAmplitudes"></param>
        /// <param name="repeat"></param>
        /// <returns></returns>
        private static IEnumerator RumblePatternCoroutine(long[] pattern, int[] lowFreqAmplitudes,
                                                          int[] highFreqAmplitudes, int repeat, MonoBehaviour coroutineSupport, int controllerID = -1)
        {
            float startedAt    = Time.unscaledTime;
            float currentTime  = startedAt;
            int   currentIndex = 0;

            UnityEngine.InputSystem.Gamepad currentGamepad = GetGamepad(controllerID);

            while (currentIndex < pattern.Length)
            {
                if (currentGamepad == null)
                {
                    yield break;
                }

                int   count              = 0;
                float totalLowFrequency  = 0;
                float totalHighFrequency = 0;

                do
                {
                    float duration = pattern[currentIndex];
                    float lowFrequencyAmplitude = (lowFreqAmplitudes.Length > currentIndex) ? lowFreqAmplitudes[currentIndex] / 255f : 0f;
                    totalLowFrequency += lowFrequencyAmplitude;
                    float highFrequencyAmplitude = (highFreqAmplitudes.Length > currentIndex) ? highFreqAmplitudes[currentIndex] / 255f : 0f;
                    totalHighFrequency += highFrequencyAmplitude;
                    currentTime        += duration / 1000f;
                    count++;
                    currentIndex++;
                } while (currentTime < Time.unscaledTime && currentIndex < pattern.Length);

                currentGamepad.SetMotorSpeeds(totalLowFrequency / count, totalHighFrequency / count);

                while (currentTime > Time.unscaledTime && currentIndex < pattern.Length)
                {
                    yield return(null);
                }
            }
            currentGamepad.SetMotorSpeeds(0f, 0f);
        }