- Home /
How to randomly chenge camerabackground color every 5 seconds?
I want to change camera background color randomly from 50 to 200 every 5 seconds. How to do that? i am using below scrip but its not working.
public class Camera_cript : MonoBehaviour { Color bgcolor = Color.white; Color current = Color.black; Camera camera; public float duration = 3.0F;
// Use this for initialization
void Start ()
{
camera = GetComponent<Camera> ();
}
void Update ()
{
float t = Mathf.PingPong(Time.time, duration) / duration;
camera.backgroundColor = Color.Lerp(current,bgcolor,0.5f);
}
}
Answer by Ekta-Mehta-D · Aug 21, 2015 at 10:29 AM
Have you set camera clearFlags ?
Add this line of code in Start() :
camera.clearFlags = CameraClearFlags.SolidColor;
For every 5 seconds :
private float nextActionTime = 0.0f; public float period = 0.1f;
void Update ()
{
if (Time.time > nextActionTime )
{
nextActionTime = Time.time + period;
// execute block of code here
}
}
after working for few hours as i am just 20 days old to unity, i've come to this code below. Its working just fine as 99% i wanted it to. Thank you for your support miss. :)
using UnityEngine; using System.Collections;
public class Camera_cript : $$anonymous$$onoBehaviour { Color bgcolor; Color current;
float duration = 1.0f;
float t = 0.0f;
float smoothnes = 0.02f;
float increment = 0.02f;
public Camera camera1;
public void Start () {
StartCoroutine (Change_color());
}
IEnumerator Change_color()
{
current = bgcolor;
bgcolor = new Color (Random.value, Random.value, Random.value);
while (t < 2)
{
camera1.backgroundColor = Color.Lerp (current, bgcolor, t);
t += increment;
yield return new WaitForSeconds (increment);
}
t = 0.0f;
current = bgcolor;
yield return new WaitForSeconds (Random.Range (3,6));
StartCoroutine (Change_color());
}
}
Your answer
Follow this Question
Related Questions
Randomly Transition Camera Background Color? 2 Answers
C# Randomize Background.Color Issues 1 Answer
How do I loop a Color32.Lerp? 3 Answers
How can I create a gradient color background for my game, without using skybox? 2 Answers
HSV to RGB 1 Answer