Timer help?

I have a trigger that when it enables it moves my camera toward a certain gameobject, i need it so once it activates a timer starts and after 3 seconds you go back to the menu (scene 0). Thanks!

Here is my script:

using UnityEngine;

public class LookingCamera : MonoBehaviour
{
	Transform target;
	
	void OnTriggerEnter(Collider other)
	{
		if(other.GetComponent<Enemy>() != null)
			target = other.transform;
	}
	
	void OnTriggerExit(Collider other)
	{
		if(target == other.transform)
			target = null;
	}
	
	// Update is called once per frame
	void LateUpdate ()
	{
		if(target != null)
		{
			transform.LookAt(target);
		}
	}
}

well for a time a timer you and just use something like

//in declarations
var timer : float = 3.0;

function countDown()
{
timer -= Time.deltaTime;

if(timer<0)
//load the main menu.
}

then just set a boolean to true which calls the function if true when the event happens.

Another possible solution would be to use a yield statement to pause the routine instead of maintaining a “manual” timer.

MoveCameraTowardsObject(someObject);

yield WaitForSeconds (3);

Application.LoadLevel(0);

Assuming you’ve coded a MoveCameraTowardsObject routine to move the camera around the above snippet would move the camera, wait 3 secs then load what ever level specified.

Here is my script:

using UnityEngine;

public class LookingCamera : MonoBehaviour
{
Transform target;

void OnTriggerEnter(Collider other)
{
if(other.GetComponent() != null)
target = other.transform;
}

void OnTriggerExit(Collider other)
{
if(target == other.transform)
target = null;
}

// Update is called once per frame
void LateUpdate ()
{
if(target != null)
{
transform.LookAt(target);
}
}
}