- Home /
Timed intervals?
Hello. I know how to enable objects, but how can I make it so that an object is enabled at the next set time interval? For example: If the time interval is every 5 seconds and the "object enabled" condition is met by the player at 13 seconds (since the start of the scene), then the object will be enabled at 15 seconds. If the player meets the condition at 21 seconds, it will be enabled at 25 seconds and so on. How can I do this?
I am going to have one object run from the start and then the second object will be enabled by the player at some point, but I want them both to be synced up...both object fire projectiles every .3 seconds if that helps put things into context.
Thanks.
EDIT- How would I incorporate something like that into this script?
static var MainLvl = 0; var Upgrade1 : GameObject; var Upgrade2 : GameObject; var Upgrade3 : GameObject;
function Update (){ if (MainLvl >0) {Upgrade1.active = true;} if (MainLvl >1) {Upgrade2.active = true;} if (MainLvl >2) {Upgrade3.active = true;} }
Answer by Statement · Dec 12, 2010 at 12:41 PM
Use InvokeRepeating to test if a condition is met and apply a response.
// This is the condition that the player will set. public var condition : boolean;
function Start() { // Calls TestCondition after 0 seconds // and repeats every 5 seconds. InvokeRepeating ("TestCondition", 0, 5); }
// Because of InvokeRepeating, tis is called every 5 seconds. function TestCondition() { if (condition) { // Condition is met, cancel the repeating invoke // and call OnCondition, which handle the details // for what to do when the condition is true. CancelInvoke("TestCondition"); OnCondition(); } }
// This is called if the condition is met every 5 seconds. function OnCondition() { // Enable that object that you wanted to enable. print("Condition met!"); }
By the way, I am not sure what you're trying to achieve but it sounds that the example on the documentation page seems related;
// Starting in 2 seconds. // a projectile will be launched every 0.3 seconds
var projectile : Rigidbody;
InvokeRepeating("LaunchProjectile", 2, 0.3);
function LaunchProjectile () { var instance : Rigidbody = Instantiate(projectile); instance.velocity = Random.insideUnitSphere * 5; }
Thank you for your help. Is there any way I can use that with multiple if conditions doing different things, without having to have a separate "TestCondition" and "OnCondition" functions? $$anonymous$$y actual script controls many different objects, and if I have that many functions it is going to get very complex and unorganized.
Do you mean that every 5 seconds, it should test a lot of different conditions and for each condition that is met, something special should happen?
Answer by azzogat · Dec 12, 2010 at 12:20 PM
Just store a variable that holds the time and then do a check for variable + 5.0 seconds.
1 (not tested):
var enabledTime: float; var nextEvent: float = 5.0;
// set initial time if(enabledCondition) { enabledTime = Time.time; }
//check timer
if(Time.time == enabledTime+nextEvent) { //do something }
How would I incorporate that into the script in my edited question?
Statement's response is a much more efficient way to address this (that's why I voted it up).
Your answer
Follow this Question
Related Questions
Do empty transforms take up much CPU power? 1 Answer
[solved]how can i enable or disable a component of instantiated objects? 5 Answers
Checking if object intersects? 1 Answer
Select an enemy to attack with buttons? 2 Answers
Enable or Instantiate 3 Answers