Hello,
I’m trying to make a script where a trigger switch camera, but with a slight delay.
I have been reading a lot about “Yield” and “Wait for seconds” but I can’t get it to work in my script. I would appreciate if someone tried to help me.
Here is my script:
var camera1 : Camera;
var camera2 : Camera;
function OnTriggerEnter(other : Collider)
{
if(other.CompareTag("Player"))
{
camera1.enabled = true;
camera2.enabled = false;
yield WaitForSeconds(1);
camera1.enabled = false;
camera2.enabled = true;
}
}
henry96
September 30, 2011, 1:20pm
2
When I want to use WaitForSeconds, I usually make another function. It looks like this :
var camera1 : Camera;
var camera2 : Camera;
function OnTriggerEnter(other : Collider)
{
if(other.CompareTag("Player"))
{
camera1.enabled = true;
camera2.enabled = false;
Switch ();
}
}
function Switch ()
{
yield WaitForSeconds(1);
camera1.enabled = false;
camera2.enabled = true;
}
Hope this alteration will solve your issue!!! Cheers!
Maybe this will help you…:
var camera1 : Camera;
var camera2 : Camera;
function OnTriggerEnter(other : Collider)
{
if(other.CompareTag("Player"))
{
StartCoroutine("ShowAndWaitUntilHide");
}
}
function ShowAndWaitUntilHide(){
camera1.enabled = true;
camera2.enabled = false;
yield WaitForSeconds(1);
camera1.enabled = false;
camera2.enabled = true;
}
Hopefully this will work for you!
-Grady
system
September 30, 2011, 2:55pm
3
possibly something like this? i usually use a simple timer like this, may not be that peformance friendly, but it works for me you may have to play with the timing a bit:
var camera1 : Camera;
var camera2 : Camera;
var timer : float = 2;
function OnTriggerEnter(other : Collider)
{
if(other.CompareTag("Player"))
{
camera1.enabled = true;
camera2.enabled = false;
if(timer <0.1){
camera1.enabled = false;
camera2.enabled = true;
}else{
timer -= 0.01;
}
}
}