示例#1
0
        public void Update()
        {
            groundEntity = vc.groundDetection.GetCurrentGroundEntity(wheel.WheelController);

            // Update skid smoke
            if (smoke != null)
            {
                float forwardSmokeStrength = Mathf.Clamp01(wheel.ForwardSlip - vc.forwardSlipThreshold) * forwardSmokeCoeff;
                float sideSmokeStrength    = Mathf.Clamp01(wheel.SideSlip - vc.sideSlipThreshold * 3f) * sideSmokeCoeff;

                if (wheel.SmoothRPM < 100f)
                {
                    forwardSmokeStrength = 0;
                }

                float newSmokeEmissionRate = 0f;
                if (groundEntity != null && wheel.IsGrounded && (wheel.HasForwardSlip || wheel.HasSideSlip) && prevGroundEntity == groundEntity)
                {
                    newSmokeEmissionRate = groundEntity.smokeIntensity * Mathf.Clamp01(forwardSmokeStrength + sideSmokeStrength);
                    smokeEmissionRate    = Mathf.SmoothDamp(smokeEmissionRate, newSmokeEmissionRate, ref smokeEmissionRateVelocity, 4f);
                }
                else
                {
                    smokeEmissionRate = newSmokeEmissionRate;
                }

                float distanceBias  = Mathf.Clamp01(Mathf.Abs(vc.ForwardVelocity) / 3f);
                var   smokeEmission = smoke.emission;
                smokeEmission.rateOverDistance = distanceBias * smokeEmissionRate;
                smokeEmission.rateOverTime     = (1f - distanceBias) * smokeEmissionRate;
            }

            // Update ground dust
            if (dust != null)
            {
                var dustEmission = dust.emission;
                if (groundEntity != null)
                {
                    dustEmissionRate = 0f;
                    if (wheel.IsGrounded)
                    {
                        dustEmissionRate = Mathf.Clamp01(vc.Speed / 12f);
                        var main = dust.main;
                        main.startColor = groundEntity.dustColor;
                    }

                    dustEmission.rateOverDistance = dustEmissionRate * groundEntity.dustIntensity * 0.1f;
                }
                else
                {
                    dustEmission.rateOverDistance = 0f;
                }
            }

            prevGroundEntity = groundEntity;
        }
示例#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);
                }
            }
        }
示例#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;
                }
            }
        }