- Home /
How do i delay function Update
Hi i'm working on a tron game for class and i'm running into a problem. I have a count down that plays then the vehicle moves and all that works fine but while the vehicle is sitting the player is still able to turn the vehicle left and right while its sitting there. To yield it from moveing i had var speed:int = 0 then i did speed = 0 then yield WaitForSeconds (5); then speed = 130 but that just makes it stop. i dont want to be able to turn either here is my turning script... i was wondering how to delay that script as well. I thought maybe if i could delay the function update for five seconds that would work but i dont know if thats possible.
#pragma strict function Update () { //makeing the bike rotate 90 degrees at a time if(Input.GetButtonDown("RIGHT")) { transform.Rotate(Vector3(0, 90, 0)); } if(Input.GetButtonDown("LEFT")) { transform.Rotate(Vector3(0, -90, 0)); } }
Answer by robertbu · Mar 16, 2013 at 07:34 AM
You could put a boolean value in the file, and check it:
At the top of the file:
var raceStarted = false; // Set this to true when the race starts
Then change the two lines in Update().
if(raceStarted && Input.GetButtonDown("RIGHT")) { transform.Rotate(Vector3(0, 90, 0)); }
if(raceStartee && Input.GetButtonDown("LEFT")) { transform.Rotate(Vector3(0, -90, 0)); } }
its not working =( heres what i have now var raceStarted:boolean = false; function Start (){ pause (); } function Update () { //makeing the bike rotate 90 degrees at a time if(raceStarted && Input.GetButtonDown("RIGHT")) { transform.Rotate(Vector3(0, 90, 0)); } if(raceStarted && Input.GetButtonDown("LEFT")) { transform.Rotate(Vector3(0, -90, 0)); } } function pause () { raceStarted = false; yield WaitForSeconds (5); raceStarted = true; }
"its not working" doesn't give me a lot to go on. I don't have your project, so I cannot see what is happening. Can you describe what it is doing? Can you still turn the vehicle or are never able to turn the vehicle?
wait yes it works never $$anonymous$$d i made a mistake thank you very much for your support and being patient with me.
Same but different, I usually write it this way
void Update(){
if(!raceStarted)return;
//rest of code
}
It does the same, I just like it more since it makes me think it looks more clear.
Just a question of taste though.
(well actually you only check it once and that is it, so slightly saving cycles)
Answer by Azial · Mar 17, 2013 at 11:53 AM
Try this:
yield waitThenStart(3.0); //"waitThenStart" function gets called
function Awake() {
enabled = false;
}
function waitThenStart(startIn : float) {
yield WaitForSeconds(startIn); //wait until the seconds passed we hand over in the parameter
enabled = true; //activate the "Update function"
}
function Update() {
... //normal code we want to exucuted every frame
}
What is that supposed to do?
this
yield waitThenStart(3.0);
is outside any function, this won't even compile.
Oh yes, it will :) You can even keep off the "yield" before I figured out recently.
So the script does this: 1. disables the Update function right away when the objects awake. 2. waitThenStart gets called 3. wait 3.0 seconds then enables the Update function
Your answer
Follow this Question
Related Questions
Im a bit confused on a simple script 1 Answer
Drawing to the scene from an EditorWindow 5 Answers
Aiming with function "Update" not working 0 Answers
Update() doesnt call method or modify values 1 Answer
Update not being called 1 Answer