using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Change_color : MonoBehaviour
{
private Color[] Allcolors = {Color.blue, Color.yellow, Color.grey, Color.green, Color.cyan, Color.red, Color.white};
public float speed = 0.1f;
public Color StartColor;
public Color EndColor;
public bool repeat = true;
float TimeStart;
void Start ()
{
TimeStart = Time.time;
wait ();
}
IEnumerator wait()
{
Debug.Log ("Start");
if (repeat == false)
{
repeat = false;
}
while(repeat == true)
{
int RDcolor = Random.Range (0, Allcolors.Length);
yield return new WaitForSeconds (2f);
Debug.Log ("end");
float T = (Mathf.Sin(Time.time - TimeStart) * -3);
GetComponent<Camera> ().backgroundColor = Color.Lerp (Allcolors[RDcolor], Allcolors[RDcolor], T);
yield return new WaitForSeconds (2f);
repeat = true;
}
}
void Update ()
{
StartCoroutine ("wait");
}
}
This code is meant to change the colour of my background screen every two seconds. The background changes successfully, however, it changes so quickly that it could give someone an epileptic shock. I really need the colour change to be slower.
This should go to start: StartCoroutine (“wait”); It will repeat forever until some other script stops it. Now it will start each frame because you have it in your Update()
float T = (Mathf.Sin(Time.time - TimeStart) * -3);
You can adjust the frequency of this change by changing the value passed to the sin function. (I notice this version does NOT actually take the speed member into account here.) Multiplying this input by a SMALLER number, will decrease the frequency/rate of change.
e.g.
float T = (Mathf.Sin(0.1f*(Time.time - TimeStart)) * -3); // will change at 1/10th (0.1f) the speed.
However, I also see that given the two WaitForSeconds(2f) commands you have, the line that actually changes the color will only be called once every four seconds. I suspect what you want is TWO coroutines, one to set the “target” color every 4 seconds, and one, to run every frame (aprox) [or just use Update()], that actually lerps the current color towards the destination color, taking X (TransitionTime) seconds to complete the transition. e.g.
float T = (Mathf.Sin((Time.time - TimeStart)/TransitionTime) * -3);
Let us know if you need some help with how to do that.