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 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