Hi I need to know how to delay a line of code and heres the code im using its in java.
So heres what I need, I need to delay the transform.Translate for about five seconds, so if you can do this for me it would be so much help. Thanks
var speed : float = 0;
function Update () {
transform.Translate(Vector3(0,0,speed) * Time.deltaTime);
}
EDIT Per Wolfram’s point - my first answer was not a good plan
However, it strikes me that perhaps you want the speed increase to be delayed by 5 seconds - and then ramp up as it ramped over that period. In which case you need a rollling buffer of the speed value:
var speeds = new List.<float>();
function Update() {
speeds.Add(speed);
if(Time.time >= 5) {
transform.Translate(Vector3(0,0,speeds[0]) * Time.deltaTime);
speeds.RemoveAt(0);
}
}
Assuming by “delay” you merely mean “I don’t want it to be executed during the first 5 seconds”, you can do something like this:
function Update () {
if(Time.time>=5)
transform.Translate(Vector3(0,0,speed) * Time.deltaTime);
}
Time.time contains the total time since your game started. If you want to measure the time from a different point (for example, if your Update() is part of an object that is being instantiated), you can store the value in Start() with startTime=Time.time, and then use the difference (Time.time-startTime) in Update.