Example #1
0
    public void LateUpdate()
    {
        EngineThrootle = CarEngine.throttle;
        this.EngineRPM = CarEngine.RPM;

        for (int i = 0; i < this.RPMLevels.Length; i++)
        {

            if ((this.EngineRPM >= this.RPMLevels[i].RpmLow) && (this.EngineRPM < this.RPMLevels[i].RpmHigh))
            {
                if ((i < (this.RPMLevels.Length - 1)) && (this.EngineRPM >= this.RPMLevels[i + 1].RpmLow))
                {
                    this.rpmLevelStages[0].RPMLevel = this.RPMLevels[i];
                    this.rpmLevelStages[0].RPMLevel.Source.mute = false;
                    this.rpmLevelStages[1].RPMLevel = this.RPMLevels[i + 1];
                    this.rpmLevelStages[1].RPMLevel.Source.mute = false;

                }
                else
                {
                    this.rpmLevelStages[0].RPMLevel = this.RPMLevels[i];
                    this.rpmLevelStages[1].RPMLevel = null;
                    if ((this.rpmLevelStages[0].RPMLevel.Source == this.AudioChannel1) && this.AudioChannel2.isPlaying)
                    {
                        this.AudioChannel1.mute = false;
                        this.AudioChannel2.mute = true;
                    }
                    else if ((this.rpmLevelStages[0].RPMLevel.Source == this.AudioChannel2) && this.AudioChannel1.isPlaying)
                    {
                        this.AudioChannel1.mute = true;
                        this.AudioChannel2.mute = false;
                    }
                }
                break;
            }
        }

        for (int j = 0; j < this.rpmLevelStages.Length; j++)
        {
            rpmLevelStage = this.rpmLevelStages[j];
            rpmLevel = rpmLevelStage.RPMLevel;

            if (rpmLevel != null)
            {
                currentRPMDiff = Mathf.Clamp(this.EngineRPM, rpmLevel.RpmLow, rpmLevel.RpmHigh);
                levelRPMDiff = rpmLevel.RpmHigh - rpmLevel.RpmLow;
                rpmTime = (currentRPMDiff - rpmLevel.RpmLow) / levelRPMDiff;
                pitchDiff = rpmLevel.PitchMax - rpmLevel.PitchMin;

                rpmLevelStage.Pitch = rpmLevel.PitchMin + (pitchDiff * rpmLevel.PitchCurve.Evaluate(rpmTime));

                if (EngineThrootle > 0)
                {
                    rpmLevel.Source.clip = rpmLevel.OnClip;
                }
                else
                {
                    rpmLevel.Source.clip = rpmLevel.OffClip;
                }

                rpmLevelStage.Volume = 1f;

                if (this.EngineThrootle > 0f)
                {
                    rpmLevelStage.Volume = (rpmLevelStage.Volume * this.RevUpVolume.Evaluate(this.EngineRPM / CarSetup.EngineMaxRPM)) * volumeScale;
                }
                else
                {
                    rpmLevelStage.Volume = (rpmLevelStage.Volume * this.RevDownVolume.Evaluate(this.EngineRPM / CarSetup.EngineMaxRPM)) * volumeScale;
                }

                if (!rpmLevel.Source.isPlaying)
                {
                    if(!isPaused)
                    {
                        rpmLevel.Source.Play();
                    }
                }
            }
        }

        if ((this.rpmLevelStages[0].RPMLevel != null) && (this.rpmLevelStages[1].RPMLevel != null))
        {
            levelRPMDiff = this.rpmLevelStages[0].RPMLevel.RpmHigh - this.rpmLevelStages[1].RPMLevel.RpmLow;
            stageRPMCoef = (this.EngineRPM - this.rpmLevelStages[1].RPMLevel.RpmLow) / levelRPMDiff;
            this.rpmLevelStages[0].RPMLevel.CurrentFade = this.rpmLevelStages[0].RPMLevel.FadeCurve.Evaluate(1f - stageRPMCoef);
            this.rpmLevelStages[1].RPMLevel.CurrentFade = this.rpmLevelStages[0].RPMLevel.FadeCurve.Evaluate(stageRPMCoef);
            levelStage1 = this.rpmLevelStages[0];
            levelStage1.Volume *= this.rpmLevelStages[0].RPMLevel.CurrentFade;
            levelStage2 = this.rpmLevelStages[1];
            levelStage2.Volume *= this.rpmLevelStages[1].RPMLevel.CurrentFade;
        }

        this.rpmLevelStages[0].Update();
        this.rpmLevelStages[1].Update();
        ApplyHiss();
    }
Example #2
0
        public void LateUpdate()
        {
            EngineThrootle = CarEngine.throttle;
            this.EngineRPM = CarEngine.RPM;

            for (int i = 0; i < this.RPMLevels.Count; i++)
            {
                if ((this.EngineRPM >= this.RPMLevels[i].RpmLow) && (this.EngineRPM < this.RPMLevels[i].RpmHigh))
                {
                    if ((i < (this.RPMLevels.Count - 1)) && (this.EngineRPM >= this.RPMLevels[i + 1].RpmLow))
                    {
                        this.rpmLevelStages[0].RPMLevel             = this.RPMLevels[i];
                        this.rpmLevelStages[0].RPMLevel.Source.mute = false;
                        this.rpmLevelStages[1].RPMLevel             = this.RPMLevels[i + 1];
                        this.rpmLevelStages[1].RPMLevel.Source.mute = false;
                    }
                    else
                    {
                        this.rpmLevelStages[0].RPMLevel = this.RPMLevels[i];
                        this.rpmLevelStages[1].RPMLevel = null;
                        if ((this.rpmLevelStages[0].RPMLevel.Source == this.AudioChannel1) && this.AudioChannel2.isPlaying)
                        {
                            this.AudioChannel1.mute = false;
                            this.AudioChannel2.mute = true;
                        }
                        else if ((this.rpmLevelStages[0].RPMLevel.Source == this.AudioChannel2) && this.AudioChannel1.isPlaying)
                        {
                            this.AudioChannel1.mute = true;
                            this.AudioChannel2.mute = false;
                        }
                    }
                    break;
                }
            }

            for (int j = 0; j < this.rpmLevelStages.Length; j++)
            {
                RPMLevelStage rpmLevelStage = this.rpmLevelStages[j];
                RPMLevel      rpmLevel      = rpmLevelStage.RPMLevel;
                if (rpmLevel != null)
                {
                    float currentRPMDiff = Mathf.Clamp(this.EngineRPM, rpmLevel.RpmLow, rpmLevel.RpmHigh);
                    float levelRPMDiff   = rpmLevel.RpmHigh - rpmLevel.RpmLow;
                    float rpmTime        = (currentRPMDiff - rpmLevel.RpmLow) / levelRPMDiff;
                    float pitchDiff      = rpmLevel.PitchMax - rpmLevel.PitchMin;
                    rpmLevelStage.Pitch = rpmLevel.PitchMin + (pitchDiff * rpmLevel.PitchCurve.Evaluate(rpmTime));

                    if (EngineThrootle > 0)
                    {
                        rpmLevel.Source.clip = rpmLevel.OnClip;
                    }
                    else
                    {
                        rpmLevel.Source.clip = rpmLevel.OffClip;
                    }
                    rpmLevelStage.Volume = 1f;

                    if (this.EngineThrootle > 0f)
                    {
                        rpmLevelStage.Volume = (rpmLevelStage.Volume * this.RevUpVolume.Evaluate(this.EngineRPM / CarSetup.EngineData.EngineMaxRPM)) * 1;
                    }
                    else
                    {
                        rpmLevelStage.Volume = (rpmLevelStage.Volume * this.RevDownVolume.Evaluate(this.EngineRPM / CarSetup.EngineData.EngineMaxRPM)) * 1;
                    }
                    if (!rpmLevel.Source.isPlaying)
                    {
                        rpmLevel.Source.Play();
                    }
                }
            }

            if ((this.rpmLevelStages[0].RPMLevel != null) && (this.rpmLevelStages[1].RPMLevel != null))
            {
                float levelRPMDiff = this.rpmLevelStages[0].RPMLevel.RpmHigh - this.rpmLevelStages[1].RPMLevel.RpmLow;
                float stageRPMCoef = (this.EngineRPM - this.rpmLevelStages[1].RPMLevel.RpmLow) / levelRPMDiff;
                this.rpmLevelStages[0].RPMLevel.CurrentFade = this.rpmLevelStages[0].RPMLevel.FadeCurve.Evaluate(1f - stageRPMCoef);
                this.rpmLevelStages[1].RPMLevel.CurrentFade = this.rpmLevelStages[0].RPMLevel.FadeCurve.Evaluate(stageRPMCoef);
                RPMLevelStage levelStage1 = this.rpmLevelStages[0];
                levelStage1.Volume *= this.rpmLevelStages[0].RPMLevel.CurrentFade;
                RPMLevelStage levelStage2 = this.rpmLevelStages[1];
                levelStage2.Volume *= this.rpmLevelStages[1].RPMLevel.CurrentFade;
            }

            this.rpmLevelStages[0].Update();
            this.rpmLevelStages[1].Update();
            ApplyHiss();
            ApplySurface();
        }
Example #3
0
    public void Awake()
    {
        // Pre-Initialize values
        CarEngine = GetComponent(typeof(CarEngine)) as CarEngine;
        CarSetup = GetComponent(typeof(CarSetup)) as CarSetup;

        SetupAudioSources();

        for (int i = 0; i < this.RPMLevels.Length; i++)
        {
            this.RPMLevels[i].Source = ((i % 2) != 0) ? this.AudioChannel2 : this.AudioChannel1;
        }
        this.rpmLevelStages[0] = new RPMLevelStage();
        this.rpmLevelStages[1] = new RPMLevelStage();

        if (this.AudioChannel1 != null)
        {
            this.AudioChannel1.loop = true;
            this.AudioChannel1.volume = 0f;
        }
        if (this.AudioChannel2 != null)
        {
            this.AudioChannel2.loop = true;
            this.AudioChannel2.volume = 0f;
        }

        isPaused = false;
        blnIsTurboInRange = false;

        rpmLevelStage = null;
        rpmLevel = null;

        currentRPMDiff = 0.0f;
        levelRPMDiff = 0.0f;
        rpmTime = 0.0f;
        pitchDiff = 0.0f;
        stageRPMCoef = 0.0f;

        levelStage1 = null;
        levelStage2 = null;

        auxObject = null;

        sources = GetComponentsInChildren<AudioSource>();
    }