how would i make a wait command? If it is possible, without Coroutine? Just something like
//do the first thing
wait (0.3f);
//do the second thing
I am using c#. thank you
how would i make a wait command? If it is possible, without Coroutine? Just something like
//do the first thing
wait (0.3f);
//do the second thing
I am using c#. thank you
It is possible without a coroutine, but it’s not very fun.
enum State {
First,
Wait,
Second,
Done
}
float startTime;
public float waitDuration = 1f;
State state = State.First;
void Update() {
if(state == State.First) {
// First thing
state = State.Wait;
startTime = Time.time;
return;
}
if(state == State.Wait) {
if(Time.time - startTime > waitDuration) {
state = State.Second;
} else {
return;
}
}
if(state == State.Second) {
// Do second thing
state = State.Done;
}
}
Or you could just use a coroutine and do this.
void Start() {
StartCoroutine("DoThings");
}
IEnumerator DoThings() {
DoFirstThing();
yield return new WaitForSeconds(1f);
DoSecondThing();
}
Thank you, i’ll definitely use a coroutine XD
EDIT: What exactly is a coroutine? Is it the timer, or the thing you need to start the timer?
Actually, I think Start can be a coroutine itself. It’s the same exact thing, but a bit more succinct. It’s also neat that most Unity callbacks can be used as coroutines.
IEnumerator Start() {
DoFirstThing();
yield return new WaitForSeconds(1f);
DoSecondThing();
}
It’s pretty simple, but coroutines are more elegant - yet they can yield significant amounts of garbage.
const float TimeToWait = 1f;
private float _actualTime;
public void Update()
{
_actualTime += Time.deltaTime;
if (_actualTime >= TimeToWait)
{
Debug.Log("We waited.");
_actualTime = 0; // reset this so it keeps going
}
}
You could use Invoke :
public void DoThat()
{
/Idk
Invoke (StopThat,5) // 5 Is the seconds before the invoke.
}
public void StopThat()
{
//idk
}
Hope i helped!