Ok so I want to take an object and rotate it on a dial into a trigger zone. How can I code for it to count down while it’s in the trigger zone to 1 second but if you move it out of the trigger zone before the time is up it resets the time and you just keep moving without any action
taken place
If you’re rotating it, then add to the timer while the rotation > X and set the timer to zero while the rotation < X.
That makes sense, how would I say “when timer hits zero; set off event”?
Not sure if I actually get what you’re asking for, but to me it seems like a simple conditional:
if(timer ==0f){
Event();
}
You could use WaitForSeconds in a co-routine, and stop it if the object leaves the area.
You could use a timer as mentioned above (although I’d say <= 0 instead of == 0 due to floating point imprecision and the fact you would likely be subtracting deltaTime).
ok, so how does a co-routine work?
A coroutine runs ‘parallel’ to the rest of your game - meaning, mostly ‘at the same time.’
Each ‘thread’ gets to run until it tells Unity it’s done for a while, then the next thread in line gets a go.
if you have
function One() {
while ( 1 ) {
Debug.Log("One");
yield;
}
}
function Two() {
while ( 1 ) {
Debug.Log("Two");
yield;
}
}
function Start() {
One(); Two();
}
You’ll get “One - Two - One - Two” over and over in your log.
You can think of Update as a sort of ‘naturally-occuring coroutine.’