Avançar para o conteúdo principal

Mensagens

A mostrar mensagens de outubro, 2018

Unity 3D Coroutines Part II

Coroutines are functions useful to implement tasks that need to happen across multiple frames. In this post we will see how to make the camera animate from one point to other across some time, the amount of time will be set in a variable so it can be changed. The function will be called when the user press the space bar. void Update () {         if (Input.GetKeyDown(KeyCode.Space))         {             if(func!=null)                 StopCoroutine(func);             func = StartCoroutine(smoothMoveCamera());// moveCamera());         } } We need some variables in the class:     public float duration = 2.0f;     public float xStart = -5.0f;     public float xFinish = 5.0f;     Coroutine func; The first variable sets the number of seconds the animation will endure. Second is the start point and the third the final point, both are points in the x axis. Then there's a reference to the coroutine so we can stop it if needed. The coroutine code is this:     IEnumer