Example #1
0
    // une coroutine est une fonction qui est lancée en parallèle au reste,
    // et sur laquelle on peut faire "pause"
    // c'est pratique pour "attendre" x secondes avant de faire un truc
    IEnumerator WaitAndGo()
    {
        SendMessage("Say", ":)");
        SendMessage("PlaySound", "idle");

        // on change notre état, comme ça on ne relance pas un autre "WaitAndGo" pendant qu'on attend
        state = WAITING;
        // dans l'animator, on change aussi le paramètre "state", pour qu'il lance l'animation qui va avec
        animator.SetInteger("state", state);


        // c'est là qu'on dit de faire pause pendant xx secondes
        yield return(new WaitForSeconds(Random.Range(2f, 6f)));

        // on charge déjà le point suivant
        agent.destination = destinations.GetRandomDestination();
        Debug.Log("New destination: " + agent.destination.ToString());

        // y'a parfois besoin d'un petit délai pour recalculer la distance
        yield return(new WaitForSeconds(0.5f));

        // quand c'est prêt, on change notre state, et le param de l'Animator
        state = WALKING;
        animator.SetInteger("state", state);

        SendMessage("Say", agent.destination.ToString());
        SendMessage("PlaySound", "go");
    }