gradual ambient light intensity change
I am in a dark tunnel so no light except low ambient. As I come out of the tunnel I want to increase the intensity - I have an empty gameobject at the end of the tunnel and when the player gets within a certain distance I get the brightness to increase but how do I do this over time? Or rather, as the player gets closer the brightness increases and further the brightness decreases. The code below just acts light a light switch rather than gradual
using UnityEngine;
using System.Collections;
public class ambientIncrease : MonoBehaviour {
public float liteChange = 5f;
public Transform Player;
void Start () {
}
void Update () {
if (Vector3.Distance (transform.position, Player.position) <= liteChange) {
RenderSettings.ambientLight = new Color(.5f, .5f, .5f, 255);
}
}
}
Answer by tonibm1999 · Nov 28, 2017 at 07:44 PM
You're just setting the ambient light to a certain value, which is constant.
You need to get a variable value for light color depending on distance.
I know what it is doing and why...I figured out how to do your observation and vary the value based on distance but now need to clamp the values. I guess I could make another script that enables and disables this script if the player goes to far from target
Answer by shadowpuppet · Nov 28, 2017 at 08:10 PM
This works for changing the brightness...but I am now having trouble with limits. I don't want it to be brighter than .5 or darker than .25. right now it is getting too dark or bright but I can't seem to figure out the syntax for an if statement
void Update () {
distance = Mathf.Round(Vector3.Distance(gameObject.transform.position, Target.transform.position))*.04f;
RenderSettings.ambientLight = new Color(.5f, .5f, .5f, 255)* distance;
}
}
// tried adding these two if conditions but get errors.how do I clamp that range?
if(RenderSettings.ambientLight.Color <= ( .5f, .5f, .5f, 255)) {
if(RenderSettings.ambientLight.Color >= ( 2.5f, 2.5f, 2.5f, 255)) {
I cheezed a solution by making a short triggerzone in the tunnel exit so that as long as I am in that trigger zone the lighting will go up or down as I move toward or away from exit. moving in and out several times eventually messes up the clamping I want a little bit ( like 12 times back and forth) but realistically no player would do that as there is nothing to go back for...it's the start of a new level
Your answer
![](https://koobas.hobune.stream/wayback/20220612143530im_/https://answers.unity.com/themes/thub/images/avi.jpg)