What is the best way to create a delay between user input and the function being carried out in this example:
function Update () {
camera.fieldOfView = 60;
if (Input.GetKey ("mouse 1"))
camera.fieldOfView = 15;
}
I want it so when 'mouse 1' is inputed, .5 seconds pass and then the field of view changes.
This script is attached to a camera.
If I am using any wrong terminology please tell me. I am fairly new to Unity.
Jessy
2
GetKey fires every frame; you don't want that. The "GetButton" family is the most versatile for what you need, so learn to use that.
http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html
http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.StartCoroutine.html?from=index
class FieldsOfView {
var narrow : float;
var wide : float;
}
var fieldsOfView : FieldsOfView;
var delay : float;
function Update () {
if (Input.GetButtonDown("Fire1")) {
StartCoroutine(ChangeFieldOfView());
enabled = false;
}
}
function ChangeFieldOfView () {
yield WaitForSeconds(delay);
camera.fieldOfView = camera.fieldOfView == fieldsOfView.narrow ? fieldsOfView.wide : fieldsOfView.narrow;
enabled = true;
}