Example #1
0
    // method: have the audio source play a random footstep audio, plan to vibrate for the footstep based on the vibration settings, and plan to do this again after the duration of that audio has passed //
    private void playRandomFootstepRecursively()
    {
        // determine the footstep audios not played previously (to prevent adjacent repeats in the consecutive randomization), as well as the corresponding sources and indeces //
        List <AudioClip>   footstepAudiosNotPlayedPreviously        = new List <AudioClip>();
        List <AudioSource> footstepAudiosNotPlayedPreviouslySources = new List <AudioSource>();
        List <int>         footstepAudiosNotPlayedPreviouslyIndeces = new List <int>();

        for (int footstepAudioIndex = 0; footstepAudioIndex < footstepAudios.Length; footstepAudioIndex++)
        {
            if (footstepAudioIndex != previousFootstepAudioIndex)
            {
                footstepAudiosNotPlayedPreviously.Add(footstepAudios[footstepAudioIndex]);
                footstepAudiosNotPlayedPreviouslySources.Add(footstepAudioSources[footstepAudioIndex]);
                footstepAudiosNotPlayedPreviouslyIndeces.Add(footstepAudioIndex);
            }
        }

        // choose a random footstep audio (only out of the footstep audios not played previously); also track its index (out of the entire audios array) //
        int         randomizationIndex        = Random.Range(0, footstepAudiosNotPlayedPreviously.Count);       // determine the randomization index (out of the audios not played previously)
        AudioClip   randomFootstepAudio       = footstepAudiosNotPlayedPreviously[randomizationIndex];
        AudioSource randomFootstepAudioSource = footstepAudiosNotPlayedPreviouslySources[randomizationIndex];
        int         randomFootstepAudioIndex  = footstepAudiosNotPlayedPreviouslyIndeces[randomizationIndex];

        // track the index of the previously played footstep audio as this one (for next time) //
        previousFootstepAudioIndex = randomFootstepAudioIndex;

        // play the random footstep audio //
        randomFootstepAudioSource.PlayOneShot(randomFootstepAudio);

        // if vibration is enabled: plan to vibrate for the footstep after the set vibration prewait duration //
        if (vibrationEnabled)
        {
            Invoke("vibrateForFootstep", vibrationPrewait);
        }

        // determine the interval duration //
        float intervalDuration = randomFootstepAudio.length;   // initialize the interval duration to the played footstep audio's length as a default
        float playerSpeed      = PlayerVelocityReader.speed(); // determine the player's current speed

        if (playerSpeed < speedMid)                            // if the player's current speed below the mid speed reference: interpolate from min to mid
        {
            float playerSpeedRangeRatio = ((playerSpeed - speedMin) / (speedMid - speedMin));
            intervalDuration = speedIntervalCurve.clamped(intervalMax, intervalMid, playerSpeedRangeRatio);
        }
        else                    // otherwise (if the player's current speed is at or above the mid speed reference): interpolate from mid to max
        {
            float playerSpeedRangeRatio = ((playerSpeed - speedMid) / (speedMax - speedMid));
            intervalDuration = speedIntervalCurve.clamped(intervalMid, intervalMin, playerSpeedRangeRatio);
        }

        // plan to do this again after the determined interval duration has passed //
        Invoke("playRandomFootstepRecursively", intervalDuration);
    }
Example #2
0
    // method: adjust the given audio source's volume by the given amount //
    protected virtual void adjustVolumeOfGivenAudioSource(AudioSource audioSource, float amount)
    {
        // calculate the modified max volume (to use only when the volume is increasing) //
        float playerSpeedRangeRatio = ((currentPlayerSpeed() - speedMin) / (speedMax - speedMin));
        float maxVolumeModified     = speedVolumeCurve.clamped(0f, volumeMax, playerSpeedRangeRatio);

        // adjust the volume to be within the range of 0 to either: the modified max volume, if the amount is positive; the max volume, if the amount is not positive //
        float maxVolumeToUse = ((amount > 0f) ? maxVolumeModified : volumeMax);

        audioSource.volume = Mathf.Min(Mathf.Max(0f, (audioSource.volume + amount)), maxVolumeToUse);
    }
    public InterpolationCurve vibrationIntensityCurve = InterpolationCurve.sine; // setting: the curve by which to interpolate the footstep vibration intensity (based on the treading audio volume out of the set max volume)



    // methods //


    // method: vibrate for a footstep //
    private void vibrateForFootstep()
    {
        // determine the vibration intensity based on the current volume of the treading audio out of the max volume //
        float  volumeRatio        = (treadingAudioSource.volume / volumeMax);
        ushort vibrationIntensity = (ushort)(vibrationIntensityCurve.clamped(0f, vibrationIntensityMax, volumeRatio));

        // if each controller is on, respectively: extendedly vibrate it for the set vibration duration by the determined vibration intensity //
        if (Controller.left)
        {
            Controller.left.vibrateExtended(vibrationDuration, vibrationIntensity);
        }
        if (Controller.right)
        {
            Controller.right.vibrateExtended(vibrationDuration, vibrationIntensity);
        }
    }
Example #4
0
    // at each update: //
    private void Update()
    {
        // if either: the locomotion dependencies are met, the stuck failsafe dependencies are met: //
        if (locomotionDependencies.areMet() || dependenciesStuckFailsafe.areMet())
        {
            // if: regular gauge treading is controlled via toggling (instead of holding), regular gauge input just began: invert the tracking of whether the regular treading toggle is on //
            if (!heldVersusToggled && locomotionInputEnabledAndAllowed() && controller.inputPressing(inputsLocomotion))
            {
                regularTreadingToggle = !regularTreadingToggle;
            }
            // if regular gauge treading is not controlled via toggling or locomotion input is not enabled: ensure that the regular gauge treading toggle is off //
            if (heldVersusToggled || !locomotionInputEnabledAndAllowed())
            {
                regularTreadingToggle = false;
            }

            // if this treader is experiencing significant input: //
            if (experiencingSignificantInput())
            {
                // if a gauge of treading is beginning: broadcast the treading gauge beginning event //
                if (controller.inputPressing(inputsLocomotion) || controller.inputTouching(inputsLocomotion))
                {
                    // broadcast treading gauge beginning event //
                    if (treadingGaugeBeginningEvent != null)
                    {
                        treadingGaugeBeginningEvent();
                    }
                }

                // based on whether any touchpad input is being used: determine the max treading speed //
                float maxSpeed = 0f;                                                                                                       // initialize the max treading speed to 0
                if (controller.inputTouched(inputsLocomotion))                                                                             // if any touchpad input is being used: set the max treading speed to the corresponding max treading speed gauge:
                {
                    maxSpeed = ((controller.touchpadPressed || !slowerSpeedEnabled || regularTreadingToggle) ? speedMax : slowerSpeedMax); // if the touchpad is being pressed or the slower guage speed is disabled or regular treading is currently toggled on: use the normal gauge; otherwise (if the touchpad is merely significantly being touched): use the slower gauge
                }
                else                                                                                                                       // otherwise (if no touchpad input is being used): simply set the max treading speed to the normal max treading speed setting
                {
                    maxSpeed = speedMax;
                }

                // initialize the treading velocity (which is just for x and z) as a proportionality (that is, without scaling it to the appropriate magnitude/speed yet) //
                Vector3 treadingVelocityProportionalityForwardPart   = FloatsVector.zeroes; // define the forward part to be the forward direction of this treader
                Vector3 treadingVelocityProportionalityRightwardPart = FloatsVector.zeroes; // initialize the rightward part as a zeroes vector
                if (controller.inputTouched(inputsLocomotion))                              // if any touchpad input is being used: proportion the velocity parts based on the touchpad input position's coordinates' signage
                {
                    // proportion the forward part of the velocity based on the touchpad's y (used for z) input position //
                    treadingVelocityProportionalityForwardPart = directionReferenceTransform.forward * controller.touchpadY;

                    // proportion the rightward part of the velocity based on the touchpad's x input position //
                    treadingVelocityProportionalityRightwardPart = directionReferenceTransform.right * controller.touchpadX;
                }
                else                            // otherwise (if no touchpad input is being used): only proportion the treading velocity forward part – using the forward direction of this treader
                {
                    treadingVelocityProportionalityForwardPart = directionReferenceTransform.forward;
                }
                Vector3 treadingVelocity = treadingVelocityProportionalityForwardPart + treadingVelocityProportionalityRightwardPart;                           // initialize the treading velocity as the sum of both the forward and rightward parts

                // trim out the y axis of the treading velocity //
                treadingVelocity = treadingVelocity.withYZero();

                // calculate the treading speed //
                float speed = 0f;                               // initialize the treading speed to 0
                if (controller.inputTouched(inputsLocomotion))  // if any touchpad input is being used:
                {
                    // set the speed to be the max treading speed, curved by the distance of the input position on the touchpad away from the center, using the curve setting //
                    speed = touchpadSpeedDistanceCurve.clamped(0f, maxSpeed, controller.touchpadDistance);
                }
                else                            // otherwise (if no touchpad input is being used): simply set the treading speed to the max treading speed
                {
                    speed = speedMax;
                }

                // scale the treading velocity to the calculated treading speed //
                treadingVelocity = treadingVelocity.normalized * speed;

                // if the player's speed in the direction of the treading velocity is not yet at or past the treading velocity's speed: //
                if (Vector3.Dot(MoonMotionPlayer.velocity, treadingVelocity.normalized) < speed)
                {
                    // determine the appropriate responsiveness factor //
                    float appropriateResponsivnessFactor = responsivenessFactorSkiing;
                    if (!Skier.skiing)
                    {
                        bool driftingWithinOneSecondAgo = dependencyDriftingWithinOneSecondAgo.isMet();
                        if (!driftingWithinOneSecondAgo)
                        {
                            appropriateResponsivnessFactor = responsivenessFactorNonskiingNondrifting;
                        }
                        else
                        {
                            float timeAgoDriftingBoosting  = (Time.time - BoostingDriftingTracker.timePlayerWasLastBoostingDrifting);
                            float timeAgoDriftingLaunching = (Time.time - LaunchingDriftingTracker.timePlayerWasLastLaunchingDrifting);
                            float timeAgoDrifting          = Mathf.Min(timeAgoDriftingBoosting, timeAgoDriftingLaunching);

                            appropriateResponsivnessFactor = responsivenessFactorNonskiingDriftingCurve.clamped(responsivenessFactorNonskiingDriftingMin, responsivenessFactorNonskiingDriftingMax, (timeAgoDrifting / 1f));
                        }
                    }

                    // hone the player's velocity x and z axes to the treading velocity x and z axes by the determined treading velocity x and z axes' magnitudes in proportion to the current frame's duration times the appropriate responsivness factor //
                    Vector3 honingVector = treadingVelocity.magnitudes().withYZero() * Time.deltaTime * appropriateResponsivnessFactor;
                    playerRigidbody.velocity = MoonMotionPlayer.velocity.honedTo(treadingVelocity, honingVector);
                }
            }
            // otherwise (if this treader is not currently experiencing significant input): if the player is not currently skiing: //
            else if (!Skier.skiing)
            {
                // determine whether significant input is currently ceasing //
                bool significantInputCeasing = (locomotionInputEnabledAndAllowed() && (controller.inputUnpressing(inputsLocomotion) || controller.inputUntouching(inputsLocomotion)));

                // determine whether the other treader is currently experiencing significant input //
                bool otherTreaderIsCurrentlyExperiencingSignificantInput = (otherTreaderActive() && other.experiencingSignificantInput());

                // if: this treader is currently ceasing to experience significant input, the other treader is not currently experiencing significant input //
                if (significantInputCeasing && !otherTreaderIsCurrentlyExperiencingSignificantInput)
                {
                    // stop the player's x and z movement (zero the player velocity's x and z axes) //
                    playerRigidbody.velocity = MoonMotionPlayer.velocity.withXAndZZero();
                }
            }
        }
    }