- Home /
How to delay time reset on collision?
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;
}
}
Answer by jefjohms · Jun 24, 2014 at 01:44 AM
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
}
gives this error.
An embedded statement may not be a declaration or labeled statement
I've tried a little and done with your help.thank you very much ! :)
Answer by doghell · Jun 23, 2014 at 07:48 PM
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;
}
}
in the OnCollisionEnter() function for the projectile (rockettt), you should just set the gun's readynow variable to true. http://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html
If I write into projectile,this time of the rocket does not recognize the term readynow
yea, you can access the public variables and functions of another object/script via theGameObject.Find or GetComponent methods (as described by jefjohms)
Your answer
![](https://koobas.hobune.stream/wayback/20220613152325im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Dragging object out of position 2 Answers
Problem with collision - Collision.other.gameObject is obsolete 1 Answer
Audio.PlayOneSHot won't work? 0 Answers
My rigidbody is floating away, its a child of a parent 0 Answers
ai help: fire shots 1 Answer