- Home /
Question by
krshnm4 · Jul 06, 2019 at 11:05 AM ·
cameracolor changefov
How to change color of an object based on camera's FOV?
Hi, I'm a noob in unity and wanted to know how can we change the color of the object if the object is in the camera's FOV for 5 sec or less!
Comment
Answer by Magso · Jul 06, 2019 at 11:26 AM
Use isVisible. To time within 5 seconds create two float variables, when isVisible is true set the first float at Time.time and when is visible is false set the second float at Time.time and if float2 - float1 is less than 5 change the color.
float startTime, endTime;
void Update(){
if(GetComponent<Renderer>().isVisible){
if(startTime == 0){
startTime = Time.time;
}
}
else{
if(startTime != 0){
endTime = Time.time;
if(endTime - startTime < 5){
//change color
}
//reset startTime to 0 to stop a loop from happening.
startTime = 0;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612204433im_/https://answers.unity.com/themes/thub/images/avi.jpg)