- Home /
Couple of questions regarding instantiate delays
First question: I have a tank that fires a prefab projectile which is instantiated when i click the mouse button. Is there a way that i can delay the instantiation of the projectile. Sort of like a reload time? Current script:
var rocket : Rigidbody; var speed = 400.0;
function BombsAway () { var rocketClone : Rigidbody = Instantiate(rocket, transform.position, transform.rotation); rocketClone.velocity = transform.forward * speed; }
function Update () { if (Input.GetButtonDown("Fire1")) { BombsAway();
} }
Second question: I'm trying to get objectives pop up on the screen for 5 seconds. I've managed to do it using GUI text on the bottom of the screen which instantiates upon going through a collider, then destroys itself after 5 seconds. However, when it instantiates it duplicates itself for everything that goes through it. Is there a way to only instantiate the text if a tagged object goes through? OR is there a way to delay it so it only instantiates once? Heres the code for one of the trigger colliders:
var prefab : GameObject; var delay = 5.0;
function OnTriggerExit () {
Instantiate (prefab); yield WaitForSeconds(delay); }
Sorry if this all seems confusing, i'm not great at explaining what i'm doing. All of this is really new to me! Thanks for all your help in advance
why you dont put yield WaitForSeconds(delay); to the first script"to wait to the tank shoots"
You can't just add a "yield" to either of these scripts without other modifications. In the 1st script, you can't add yield to an "Update" function. In the 2nd script, the OnTriggerExit will get called for every trigger, whether there's a yield inside it or not.
Answer by duck · Nov 23, 2010 at 07:52 AM
For the first script, you can either manually track and compare the Time.time value, like this:
var rocket : Rigidbody; var speed = 400.0; var reloadTime = 2.0; private var nextFireTime = 0;
function BombsAway () { var rocketClone : Rigidbody = Instantiate(rocket, pos, rot); rocketClone.velocity = transform.forward * speed; }
function Update () { if (Input.GetButtonDown("Fire1") && Time.time > nextFireTime) { BombsAway(); nextFireTime = Time.time + reloadTime;
} }
Or you could use a coroutine (which basically means a function that makes use of "yield"), like this:
var rocket : Rigidbody; var speed = 400.0; var reloadTime = 2.0;
function Start() { while (true) { if (Input.GetButtonDown("Fire1")) { BombsAway(); yield WaitForSeconds(reloadTime);
} yield; } }
function BombsAway () { var rocketClone : Rigidbody = Instantiate(rocket, pos, rot); rocketClone.velocity = transform.forward * speed; }
To make it so your trigger function only creates the instance once, you could use a boolean var, like this:
var prefab : GameObject; private var created = false;
function OnTriggerExit () { if (!created) { Instantiate (prefab); created = true; } }
Or to make it so only objects with a certain tag trigger it:
var prefab : GameObject;
function OnTriggerExit (c : Collider) { if (c.CompareTag("Player")) { Instantiate (prefab); } }
Or to make it so that it only triggers a maximum of once per N seconds:
var prefab : GameObject; var delay = 5.0; private var waitUntil = 0.0;
function OnTriggerExit () { if (Time.time > waitUntil) { Instantiate (prefab); waitUntil = Time.time + delay; } }
Brilliant! I added your modifications and they work perfectly. I have been struggling for this over the past week, thank you very much.