How to Transition Between Ambient Light Values in C#

Hey guys,

I am not a programmer but made a small script to transition audio snapshots and now want to transition between two public ambient light values. Here is my script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.Rendering;
#if UNITY_EDITOR
using UnityEditor;
#endif



public class TFPCaveAudio : MonoBehaviour {

    public AudioMixerSnapshot open;
    public AudioMixerSnapshot cave;
    public float ambientIntensityCave;
    public float ambientIntensityOutdoors;

    void Start () {
    }

    // Use this for initialization
    void OnTriggerEnter(Collider other) {
        if (other.gameObject.tag == "CaveTrigger") {
            cave.TransitionTo (.6f);
            RenderSettings.ambientIntensity = ambientIntensityCave;
        }
    }
    void OnTriggerExit(Collider other) {
        if (other.gameObject.tag == "CaveTrigger") {
            open.TransitionTo (.6f);
            RenderSettings.ambientIntensity = ambientIntensityOutdoors;
        }
    }
}

It does everything right but now I need to blend the two values but am not sure how. Every reference for this is in Java and the little scripting I know I want to be C#.

Thank you for any help

Start a coroutine. Just paste this code in the class and call InitAmbienceTransition instead of setting it directly.

        public void InitAmbienceTransition (float goalAmbience, float duration)
        {
            StartCoroutine (ChangeAmbience (goalAmbience, duration));
        }

        private System.Collections.IEnumerator ChangeAmbience (float goalAmbience, float duration)
        {
            float
                startAmbience = RenderSettings.ambientIntensity,
                progress = 0f;

            while  (progress < 1f)
            {
                progress = Mathf.Clamp01 (progress + Time.deltaTime / duration);
                RenderSettings.ambientIntensity = Mathf.Lerp (startAmbience, goalAmbience, progress);

                yield return null;
            }
        }
2 Likes

Hey @keenanwoodall ,

Thank you very much for the response. Excuse my absolute stupidity but I do not how to input this to the set floats for the ambient values. Tried a few different ways to input it but was not right. Here is how I inputted this but it was an error so know I put it wrong:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.Rendering;
#if UNITY_EDITOR
using UnityEditor;
#endif



public class TFPCaveAudio : MonoBehaviour {

    public AudioMixerSnapshot open;
    public AudioMixerSnapshot cave;
    public float ambientIntensityCave;
    public float ambientIntensityOutdoors;

    void Start () {
    }

    public void InitAmbienceTransition (float goalAmbience, float duration)
    {
        StartCoroutine (ChangeAmbience (goalAmbience, duration));
    }

    private System.Collections.IEnumerator ChangeAmbience (float goalAmbience, float duration)
    {
        float
        startAmbience = RenderSettings.ambientIntensity,
        progress = 0f;

        while  (progress < 1f)
        {
            progress = Mathf.Clamp01 (progress + Time.deltaTime / duration);
            RenderSettings.ambientIntensity = Mathf.Lerp (startAmbience, goalAmbience, progress);

            yield return null;
        }
    }

    // Use this for initialization
    void OnTriggerEnter(Collider other) {
        if (other.gameObject.tag == "CaveTrigger") {
            cave.TransitionTo (.6f);
            //RenderSettings.ambientIntensity = ambientIntensityCave;
            InitAmbienceTransition (ambientIntensityCave); // Test
        }
    }
    void OnTriggerExit(Collider other) {
        if (other.gameObject.tag == "CaveTrigger") {
            open.TransitionTo (.6f);
            RenderSettings.ambientIntensity = ambientIntensityOutdoors;
        }
    }
}

You are getting an error because the method, InitAmbienceTransition, takes two arguments: goalAmbience and duration. you aren’t passing a duration. Do you want me to explain how C# works or give you the solution? I don’t wanna waste your time explaining stuff if you just want working code.

I am not a programmer. To not waist your time a simple explanation would be great but yeah mainly I just want working code. I tried programming in the past but just cannot understand it. How would I pass a duration? I put a float after the goalAmbience but same error. Thanks again

I don’t think this is the place to explain how methods work, so I’ll just post the working code (haven’t tested it, but it should work?)

using UnityEngine;
using UnityEngine.Audio;
using System.Collections;
public class TFPCaveAudio : MonoBehaviour
{
    public float audioTransitionDuration = 0.6f;
    public AudioMixerSnapshot open;
    public AudioMixerSnapshot cave;

    public float ambienceTransitionDuration = 1f;
    public float caveAmbientIntensity;
    public float outdoorAmbientIntensity;

    void OnTriggerEnter (Collider other)
    {
        if (other.gameObject.tag == "CaveTrigger")
        {
            // Transitions audio.
            cave.TransitionTo (.6f);

            // Transitions ambient intensity to cave intensity.
            InitAmbienceTransition (caveAmbientIntensity, ambienceTransitionDuration);
        }
    }
    void OnTriggerExit (Collider other)
    {
        if (other.gameObject.tag == "CaveTrigger")
        {
            // Transitions audio.
            open.TransitionTo (.6f);

            // Transitions ambient intensity to outdoor intensity.
            InitAmbienceTransition (outdoorAmbientIntensity, ambienceTransitionDuration);
        }
    }

    public void InitAmbienceTransition (float goalAmbience, float duration)
    {
        StartCoroutine (ChangeAmbience (goalAmbience, duration));
    }

    private System.Collections.IEnumerator ChangeAmbience (float goalAmbience, float duration)
    {
        float
            startAmbience = RenderSettings.ambientIntensity,
            progress = 0f;

        while (progress < 1f)
        {
            progress = Mathf.Clamp01 (progress + Time.deltaTime / duration);
            RenderSettings.ambientIntensity = Mathf.Lerp (startAmbience, goalAmbience, progress);

            yield return null;
        }
    }
}
1 Like

Thank you very much @keenanwoodall ! Looking over this helps. Already made edits to get additional things in. Thanks!

No problem :slight_smile: