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:
IEnumerator smoothMoveCamera()
{
float speed = 1 / duration;
float percent = 0;
while (true)
{
percent += speed*Time.deltaTime;
transform.position = new Vector3(Mathf.Lerp(xStart, xFinish, percent),1,-10);
yield return null;
if (percent >= 1) break;
}
}
We start by calculating the speed of the camera by dividing 1 by the duration. This is the speed per second, inside the loop we multiply by the deltaTime to adjust to the time it took to render the last frame.
Then we calculate the new position using the Lerp function using the percentage of the time completed to lerp between the starting point and the end point.
After the new position is set there's the yield statement which return the execution to the Unity engine so the frame get's rendered. After that we check to see if the percentage is 1 and if so the execution ends, because we are done.
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:
IEnumerator smoothMoveCamera()
{
float speed = 1 / duration;
float percent = 0;
while (true)
{
percent += speed*Time.deltaTime;
transform.position = new Vector3(Mathf.Lerp(xStart, xFinish, percent),1,-10);
yield return null;
if (percent >= 1) break;
}
}
We start by calculating the speed of the camera by dividing 1 by the duration. This is the speed per second, inside the loop we multiply by the deltaTime to adjust to the time it took to render the last frame.
Then we calculate the new position using the Lerp function using the percentage of the time completed to lerp between the starting point and the end point.
After the new position is set there's the yield statement which return the execution to the Unity engine so the frame get's rendered. After that we check to see if the percentage is 1 and if so the execution ends, because we are done.
Comentários
Enviar um comentário