Exemplo n.º 1
0
        public void Update()
        {
            if (!_initialized)
            {
                return;
            }

            bool isGrounded = _wheelComponent.IsGrounded;

            surfacePreset = _wheelComponent.surfacePreset;
            particleCount = 0;

            if (!isGrounded || surfacePreset == null)
            {
                StopParticleEmission();
                StopChunkEmission();
                return;
            }

            UpdateParticles();
            UpdateChunks();
        }
Exemplo n.º 2
0
        public override void Update()
        {
            if (!Active)
            {
                return;
            }

            float newVolume = 0;
            float newPitch  = basePitch;

            if (vc.groundDetection != null)
            {
                for (int i = 0; i < _wheelCount; i++)
                {
                    WheelComponent wheelComponent = vc.Wheels[i];
                    SurfacePreset  surfacePreset  = wheelComponent.surfacePreset;

                    bool hasSlip = wheelComponent.HasLateralSlip || wheelComponent.HasLongitudinalSlip;
                    if (wheelComponent.IsGrounded && surfacePreset != null && surfacePreset.playSkidSounds && hasSlip)
                    {
                        float slipPercent = wheelComponent.NormalizedLateralSlip +
                                            wheelComponent.NormalizedLongitudinalSlip;
                        slipPercent = slipPercent < 0 ? 0 : slipPercent > 1 ? 1 : slipPercent;

                        if (surfacePreset.skidSoundClip != null)
                        {
                            AudioSource source = Sources[i];

                            if (!source.isPlaying)
                            {
                                source.Play();
                            }

                            if (source.clip != surfacePreset.skidSoundClip)
                            {
                                // Change skid clip
                                source.clip = surfacePreset.skidSoundClip;
                                source.time = Random.Range(0f, surfacePreset.skidSoundClip.length);
                                source.time = Random.Range(0f, source.clip.length);
                            }

                            float absAngVel = wheelComponent.angularVelocity < 0
                                ? -wheelComponent.angularVelocity
                                : wheelComponent.angularVelocity;
                            float speedCoeff = vc.Speed / 3f + absAngVel / 20f;
                            speedCoeff = speedCoeff > 1f ? 1f : speedCoeff;
                            newVolume  = slipPercent * surfacePreset.skidSoundVolume * speedCoeff;
                            newVolume  = Mathf.Lerp(_prevVolume[i], newVolume, vc.deltaTime * 12f);

                            float loadCoeff = wheelComponent.wheelController.wheel.load /
                                              wheelComponent.wheelController.maximumTireLoad;
                            loadCoeff = loadCoeff > 1f ? 1f : loadCoeff;
                            newPitch  = surfacePreset.skidSoundPitch + loadCoeff * 0.3f;
                            newPitch  = Mathf.Lerp(_prevPitch[i], newPitch, vc.deltaTime * 18f);
                        }
                    }
                    else
                    {
                        newVolume = 0;
                        newPitch  = basePitch;
                    }

                    SetVolume(newVolume, i);
                    SetPitch(newPitch, i);

                    _prevVolume[i] = newVolume;
                    _prevPitch[i]  = newPitch;
                }
            }
        }
Exemplo n.º 3
0
        public override void Update()
        {
            if (!Active)
            {
                return;
            }

            for (int i = 0; i < _wheelCount; i++)
            {
                WheelComponent wheelComponent = vc.Wheels[i];
                SurfacePreset  surfacePreset  = wheelComponent.surfacePreset;

                float newVolume = 0;
                float newPitch  = basePitch;

                if (wheelComponent.IsGrounded && surfacePreset != null && surfacePreset.playSurfaceSounds)
                {
                    if (surfacePreset.surfaceSoundClip != null)
                    {
                        AudioSource source = Sources[i];

                        if (!source.isPlaying)
                        {
                            source.Play();
                        }

                        if (source.clip != surfacePreset.surfaceSoundClip)
                        {
                            // Change skid clip
                            source.clip = surfacePreset.surfaceSoundClip;
                            source.time = Random.Range(0f, surfacePreset.surfaceSoundClip.length);
                            source.time = Random.Range(0f, source.clip.length);
                        }

                        float surfaceModifier = 1f;
                        if (surfacePreset.slipSensitiveSurfaceSound)
                        {
                            surfaceModifier = wheelComponent.NormalizedLateralSlip / vc.longitudinalSlipThreshold;
                            surfaceModifier = surfaceModifier < 0 ? 0 : surfaceModifier > 1 ? 1 : surfaceModifier;
                        }

                        float speedCoeff = vc.Speed / 20f;
                        speedCoeff = speedCoeff < 0 ? 0 : speedCoeff > 1 ? 1 : speedCoeff;

                        // Change surface volume and pitch
                        newVolume = surfacePreset.surfaceSoundVolume * surfaceModifier * speedCoeff;
                        newVolume = newVolume < 0 ? 0 : newVolume > 1 ? 1 : newVolume;
                        newVolume = Mathf.Lerp(_prevVolume[i], newVolume, vc.deltaTime * 12f);

                        newPitch = surfacePreset.surfaceSoundPitch * 0.5f + speedCoeff;
                    }
                }
                else
                {
                    newVolume = Mathf.Lerp(_prevVolume[i], 0, vc.deltaTime * 12f);
                    newPitch  = Mathf.Lerp(_prevPitch[i], basePitch, vc.deltaTime * 12f);
                }

                SetVolume(newVolume, i);
                SetPitch(newPitch, i);

                _prevVolume[i] = newVolume;
                _prevPitch[i]  = newPitch;
            }
        }
Exemplo n.º 4
0
        public override void Update()
        {
            if (!Active)
            {
                return;
            }

            // Check if can be updated
            if (vc.groundDetection == null || !vc.groundDetection.IsEnabled)
            {
                return;
            }

            // Check for added/removed wheels and re-init if needed
            int wheelCount = vc.powertrain.wheels.Count;

            if (prevWheelCount != wheelCount)
            {
                initialized = false;
                Initialize();
            }

            prevWheelCount = wheelCount;

            // Update skidmarks
            Debug.Assert(skidmarkGenerators.Count == vc.powertrain.wheels.Count,
                         "Skidmark generator count must equal wheel count");

            int n = skidmarkGenerators.Count;

            for (int i = 0; i < n; i++)
            {
                WheelComponent wheelComponent = vc.powertrain.wheels[i];
                SurfacePreset  surfacePreset  = wheelComponent.surfacePreset;
                if (surfacePreset == null || !surfacePreset.drawSkidmarks)
                {
                    continue;
                }

                bool surfaceMapIsNull = surfacePreset == null;

                int surfaceMapIndex = -1;
                if (!surfaceMapIsNull)
                {
                    surfaceMapIndex = wheelComponent.surfaceMapIndex;
                }

                float intensity = 1f;
                if (surfaceMapIndex >= 0)
                {
                    float latFactor = wheelComponent.NormalizedLateralSlip;
                    latFactor = latFactor < vc.lateralSlipThreshold ? 0 : latFactor - vc.lateralSlipThreshold;
                    float lonFactor = wheelComponent.NormalizedLongitudinalSlip;
                    lonFactor = lonFactor < vc.lateralSlipThreshold ? 0 : lonFactor - vc.lateralSlipThreshold;

                    float slipIntensity = latFactor + lonFactor;
                    float weightCoeff   = wheelComponent.wheelController.wheel.load * 2f /
                                          wheelComponent.wheelController.maximumTireLoad;
                    weightCoeff    = weightCoeff <0 ? 0f : weightCoeff> 1f ? 1f : weightCoeff;
                    slipIntensity *= wheelComponent.surfacePreset.slipFactor * weightCoeff;

                    intensity = wheelComponent.surfacePreset.skidmarkBaseIntensity + slipIntensity;
                    intensity = intensity > 1f ? 1f : intensity < 0f ? 0f : intensity;
                }

                intensity *= globalSkidmarkIntensity;
                intensity  = intensity <0f ? 0f : intensity> maxSkidmarkAlpha ? maxSkidmarkAlpha : intensity;

                float albedoIntensity = 0f;
                float normalIntensity = 0f;

                // TODO - check that fallback surface preset is not null

                skidmarkGenerators[i].Update(surfaceMapIndex, intensity, albedoIntensity, normalIntensity,
                                             wheelComponent.wheelController.pointVelocity, vc.fixedDeltaTime);
            }
        }