Beispiel #1
0
        public override void Initialize(VehicleController vc, AudioMixerGroup amg)
        {
            this.vc = vc;
            this.audioMixerGroup = amg;

            smoothedVolume = new HysteresisSmoothedValue(0, 0.25f, 0.5f);
            smoothedPitch  = new HysteresisSmoothedValue(0, 0.25f, 0.5f);

            foreach (Wheel wheel in vc.Wheels)
            {
                AudioSource a = wheel.ControllerGO.AddComponent <AudioSource>();
                vc.sound.SetAudioSourceDefaults(a, true, true);
                Sources.Add(a);
            }

            RegisterSources();
        }
Beispiel #2
0
        public override void Update()
        {
            if (smoothedVolume == null)
            {
                smoothedVolume = new HysteresisSmoothedValue(0, 0.25f, 0.5f);
            }
            if (smoothedPitch == null)
            {
                smoothedPitch = new HysteresisSmoothedValue(0, 0.25f, 0.5f);
            }

            if (vc.groundDetection != null)
            {
                for (int i = 0; i < vc.Wheels.Count; i++)
                {
                    Wheel wheel = vc.Wheels[i];
                    GroundDetection.GroundEntity groundEntity = vc.groundDetection.GetCurrentGroundEntity(wheel.WheelController);

                    if (wheel.IsGrounded && groundEntity != null)
                    {
                        if (groundEntity.skidSoundComponent.clip != null)
                        {
                            if (Sources[i].clip != groundEntity.skidSoundComponent.clip)
                            {
                                // Change skid clip
                                Sources[i].Stop();
                                Sources[i].clip = groundEntity.skidSoundComponent.clip;
                                Sources[i].time = Random.Range(0f, groundEntity.skidSoundComponent.clip.length);
                            }

                            // Update skid sounds
                            if (wheel.HasForwardSlip || wheel.HasSideSlip)
                            {
                                // Calculate skid volume and pitch
                                float forwardSkid = Mathf.Max(0f, (Mathf.Clamp01(Mathf.Pow((Mathf.Abs(wheel.SmoothForwardSlip) - vc.forwardSlipThreshold), 2f)) * forwardSkidVolume));

                                float sideSkid = Mathf.Clamp01(Mathf.Pow((Mathf.Abs(wheel.SmoothSideSlip) - vc.sideSlipThreshold), 2f)) * sideSkidVolume;

                                float skidPitch = Mathf.Clamp(Mathf.Abs(wheel.WheelController.suspensionForce / (vc.vehicleRigidbody.mass * 3f)), groundEntity.skidSoundComponent.pitch * 0.7f, groundEntity.skidSoundComponent.pitch * 1.3f);

                                float skidVolume = Mathf.Clamp01(forwardSkid + sideSkid) * 30f;

                                Sources[i].time = Random.Range(0f, Sources[i].clip.length);
                                float newVolume = Mathf.Clamp01(skidVolume) * volume * groundEntity.skidSoundComponent.volume * vc.sound.masterVolume;
                                smoothedVolume.Tick(newVolume);
                                SetVolume(smoothedVolume.Value, i);

                                smoothedPitch.Tick(skidPitch);
                                Sources[i].pitch = smoothedPitch.Value;

                                if (!Sources[i].isPlaying && Sources[i].isActiveAndEnabled)
                                {
                                    Sources[i].Play();
                                }
                            }
                            else
                            {
                                Sources[i].Stop();
                            }
                        }
                    }
                    else
                    {
                        smoothedVolume.Tick(0);
                    }

                    SetVolume(smoothedVolume.Value, i);
                }
            }
        }
Beispiel #3
0
        public override void Update()
        {
            if (vc.groundDetection != null)
            {
                for (int i = 0; i < vc.Wheels.Count; i++)
                {
                    Sources[i].volume = 0f;
                    Wheel wheel = vc.Wheels[i];
                    GroundDetection.GroundEntity groundEntity = vc.groundDetection.GetCurrentGroundEntity(wheel.WheelController);

                    if (wheel.IsGrounded && groundEntity != null)
                    {
                        if (groundEntity.surfaceSoundComponent.clip != null)
                        {
                            // Change clips based on surface
                            if (Sources[i].clip != groundEntity.surfaceSoundComponent.clip)
                            {
                                // Change surface clip
                                Sources[i].Stop();
                                Sources[i].clip = groundEntity.surfaceSoundComponent.clip;
                                Sources[i].time = Random.Range(0f, groundEntity.surfaceSoundComponent.clip.length);
                                if (Sources[i].enabled)
                                {
                                    Sources[i].Play();
                                }
                            }

                            // If slipSensitiveSurface (tire only makes noise when under load) make sound dependent on side slip, while on gravel and
                            // similar make sound independent of slip since wheel makes noise at all times (only dependent on speed).
                            float surfaceModifier = 1f;
                            if (groundEntity.slipSensitiveSurfaceSound)
                            {
                                surfaceModifier = (wheel.SmoothSideSlip / vc.sideSlipThreshold) + (wheel.SmoothForwardSlip / vc.forwardSlipThreshold);
                            }

                            // Change surface volume and pitch
                            float newVolume = groundEntity.surfaceSoundComponent.volume * Mathf.Clamp01(vc.Speed / 10f)
                                              * surfaceModifier * volume * groundEntity.surfaceSoundComponent.volume;

                            // Recompile fix
                            if (smoothedVolume == null)
                            {
                                smoothedVolume = new HysteresisSmoothedValue(0, 0.2f, 0.5f);
                            }

                            smoothedVolume.Tick(newVolume);

                            SetVolume(smoothedVolume.Value, i);

                            Source.pitch = pitch * 0.5f + Mathf.Clamp01(vc.Speed / 10f) * 1f;
                        }
                    }
                }
            }
            else
            {
                for (int i = 0; i < vc.Wheels.Count; i++)
                {
                    Sources[i].volume = 0;
                }
            }
        }