Standard delay time 2 second.but I launch the rocket when it hits an object, I want to restart delay.
How can this be?
#pragma strict
var rockettt : GameObject;
var readynow : boolean = true;
function Update () {
if(readynow){
MakeBox();
}
}
function MakeBox () {
if (Input.GetButton ("FireLazer")) {
readynow=false;
Instantiate(rockettt, Vector3(transform.position.x , transform.position.y + 1,0), Quaternion.identity );
yield WaitForSeconds (2);
readynow=true;
}
}
Why don’t you just keep Input.GetButton in the Update() function, make it run MakeBox(), and check for the “readynow” in the MakeBox() function itself.
if you detect a collision, you can just set the public readynow value to true
function Update () {
if (Input.GetKeyDown(KeyCode.L)) {
MakeBox();
}
}
function MakeBox () {
if (readynow) {
readynow = false;
Instantiate(rockettt, Vector3(transform.position.x , transform.position.y + 1,0), Quaternion.identity );
yield WaitForSeconds (2);
readynow=true;
}
}
Instead of doing WaitForSecond() which can not be changed, I would track time manually so you can choose to reset when box collider fires.
public float maxShotTime;
public float currentShotTime;
void Start(){
maxShotTime = 2.0f;
currentShotTime = 0.0f;
}
void MakeBox(){
currentShotTime += Time.DeltaTime;
if (currentShotTime > maxShotTime){
//instantiate your rocket
}
In your script for the rocket prefab reset the time to maxShotTime in collisionEnter function.
public GameObject fireControlObject; //object your previous script is attached
void OnCollisionEnter(Collider other){
//here you can differentiate what it hit if needed
yourScriptFilename script = fireControlObject.GetComponent<yourScriptFilename>();
script.currentShotTime = script.maxShotTime;
//now you can destroy the rocket object
}